Skip to content

perf: bypass validation for trusted InstrLocation and Instr construction#196

Merged
MatthieuDartiailh merged 1 commit into
MatthieuDartiailh:mainfrom
P403n1x87:perf/avoid-location-revalidation
May 8, 2026
Merged

perf: bypass validation for trusted InstrLocation and Instr construction#196
MatthieuDartiailh merged 1 commit into
MatthieuDartiailh:mainfrom
P403n1x87:perf/avoid-location-revalidation

Conversation

@P403n1x87
Copy link
Copy Markdown
Contributor

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%
BaseInstr._set (via to_bytecode) 1.48% eliminated

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

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 |
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 8, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.21%. Comparing base (8bf35e4) to head (7b483f5).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #196      +/-   ##
==========================================
+ Coverage   95.17%   95.21%   +0.03%     
==========================================
  Files           7        7              
  Lines        2032     2048      +16     
  Branches      448      448              
==========================================
+ Hits         1934     1950      +16     
  Misses         54       54              
  Partials       44       44              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@P403n1x87 P403n1x87 marked this pull request as ready for review May 8, 2026 17:50
@MatthieuDartiailh MatthieuDartiailh merged commit 9de3e78 into MatthieuDartiailh:main May 8, 2026
10 checks passed
@P403n1x87 P403n1x87 deleted the perf/avoid-location-revalidation branch May 8, 2026 18:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants