Commit 41c679b
authored
perf: bypass ConcreteInstr validation in concrete_instructions (#201)
Two changes to the hot encode loop in _ConvertBytecodeToConcrete.concrete_instructions:
1. ConcreteInstr._from_trusted factory
ConcreteInstr(instr_name, c_arg, location=location) at the bottom of the
loop went through the full __init__ -> _check_arg -> _set chain on every
instruction, even though name/opcode/arg/location are all derived from
already-validated Instr objects. A new _from_trusted classmethod bypasses
_check_arg entirely and replicates only the size computation from _set,
using object.__new__ + direct slot assignment — the same pattern as the
existing _from_opcode fast path.
2. Direct private slot access
instr.location, instr.name, and instr.arg are properties whose bodies are
each a single "return self._xxx". Accessing instr._location, instr._name,
and instr._arg directly avoids the property descriptor lookup on every
iteration. This is safe because instr is a validated Instr instance at this
point (guarded by the assert isinstance check above).
Profile data
| Hotspot | Before | After |
|---|---|---|
| concrete_instructions own | 8.07% | 5.44% |
| concrete_instructions total | 11.71% | 9.44% |
| ConcreteInstr.__init__ (child) | 1.30% total | gone |
Throughput (Bytecode.from_code().to_code() on dis module, 30 runs each)
| | Avg | Stddev |
|---|---|---|
| Before | 223.7 r/s | ±4.7 |
| After | 246.1 r/s | ±3.3 |
+22.4 r/s / +10.0% throughput improvement.1 parent bd74da9 commit 41c679b
1 file changed
Lines changed: 31 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
199 | 199 | | |
200 | 200 | | |
201 | 201 | | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
202 | 226 | | |
203 | 227 | | |
204 | 228 | | |
| |||
1145 | 1169 | | |
1146 | 1170 | | |
1147 | 1171 | | |
1148 | | - | |
1149 | | - | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
| 1175 | + | |
1150 | 1176 | | |
1151 | | - | |
| 1177 | + | |
1152 | 1178 | | |
1153 | | - | |
| 1179 | + | |
1154 | 1180 | | |
1155 | 1181 | | |
1156 | 1182 | | |
| |||
1234 | 1260 | | |
1235 | 1261 | | |
1236 | 1262 | | |
1237 | | - | |
| 1263 | + | |
1238 | 1264 | | |
1239 | 1265 | | |
1240 | 1266 | | |
| |||
0 commit comments