Commit 7b483f5
committed
perf: bypass validation for trusted InstrLocation and Instr construction
Add two fast-path factory methods that skip validation by using
object.__new__ + direct slot assignment, for call sites where the
inputs are already known to be valid:
**InstrLocation._from_tuple** — replaces InstrLocation(...) at four
internal sites where positions come from trusted sources (existing
InstrLocation.lineno, SetLineno.lineno, first_lineno):
- ConcreteBytecode.to_bytecode (fallback lineno-only location)
- ConcreteBytecode._pack_location (propagated from existing location)
- _ConvertBytecodeToConcrete.concrete_instructions (first_lineno seed
and SetLineno-derived locations)
**BaseInstr._from_trusted** — replaces Instr(name, arg, location=loc)
in ConcreteBytecode.to_bytecode, where name/opcode/arg/location are all
derived from already-validated ConcreteInstr objects.
CPU own-time profile data:
| Hotspot | Before | After |
|---|---|---|
| `ConcreteBytecode.to_bytecode` | 5.98% | 5.07% |
| `Instr._check_arg` | 2.87% | eliminated |
| `BaseInstr._set` (via to_bytecode) | 1.48% | eliminated |
| `BaseInstr._from_trusted` | — | <1% (not in top 20) |
Throughput (Bytecode.from_code().to_code() on dis module's code object,
1 second timed window, 5 runs):
| | r/s range |
|---|---|
| Before | 103–108 |
| After | 109–114 |1 parent 8bf35e4 commit 7b483f5
2 files changed
Lines changed: 45 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
575 | 575 | | |
576 | 576 | | |
577 | 577 | | |
578 | | - | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
579 | 581 | | |
580 | 582 | | |
581 | 583 | | |
| |||
930 | 932 | | |
931 | 933 | | |
932 | 934 | | |
933 | | - | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
934 | 938 | | |
935 | 939 | | |
936 | 940 | | |
937 | 941 | | |
938 | 942 | | |
939 | 943 | | |
940 | | - | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
941 | 947 | | |
942 | 948 | | |
943 | 949 | | |
| |||
1030 | 1036 | | |
1031 | 1037 | | |
1032 | 1038 | | |
1033 | | - | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
1034 | 1042 | | |
1035 | 1043 | | |
1036 | 1044 | | |
| |||
1086 | 1094 | | |
1087 | 1095 | | |
1088 | 1096 | | |
1089 | | - | |
| 1097 | + | |
1090 | 1098 | | |
1091 | 1099 | | |
1092 | 1100 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
621 | 621 | | |
622 | 622 | | |
623 | 623 | | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
624 | 640 | | |
625 | 641 | | |
626 | 642 | | |
| |||
819 | 835 | | |
820 | 836 | | |
821 | 837 | | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
822 | 854 | | |
823 | 855 | | |
824 | 856 | | |
| |||
0 commit comments