Skip to content

perf: bypass ConcreteInstr validation in from_code via _from_opcode#197

Merged
MatthieuDartiailh merged 1 commit into
MatthieuDartiailh:mainfrom
P403n1x87:perf/concrete-instr-skip-check
May 11, 2026
Merged

perf: bypass ConcreteInstr validation in from_code via _from_opcode#197
MatthieuDartiailh merged 1 commit into
MatthieuDartiailh:mainfrom
P403n1x87:perf/concrete-instr-skip-check

Conversation

@P403n1x87
Copy link
Copy Markdown
Contributor

Add ConcreteInstr._from_opcode, a fast-path constructor for the from_code loop that skips all validation and sets the six slots directly via object.new + attribute assignment.

The checks are safe to skip because every instruction in this path comes from CPython's own co_code bytes:

  • name/opcode: _opcode.opname[byte] is always a valid opcode
  • arg: a raw byte (0-255), always satisfies _check_arg_int, or UNSET for no-argument opcodes (already gated by opcode_has_argument)
  • _size: always 2 — EXTENDED_ARG accumulation happens after this loop in _remove_extended_args, not during construction
  • _extended_args: always None in this path

CPU profiling data (where the impact is more visible):

Hotspot Before After
ConcreteInstr.__init__ own 3.11% eliminated
ConcreteInstr._check_arg own 1.82% eliminated
ConcreteBytecode.from_code own 6.21% 4.55%

Throughput (usual Bytecode.from_code().to_code() round-trip, 1 second timed window, 5 runs):

r/s range
Before ~143
After ~148

Add ConcreteInstr._from_opcode, a fast-path constructor for the
from_code loop that skips all validation and sets the six slots
directly via object.__new__ + attribute assignment.

The checks are safe to skip because every instruction in this path
comes from CPython's own co_code bytes:
- name/opcode: _opcode.opname[byte] is always a valid opcode
- arg: a raw byte (0-255), always satisfies _check_arg_int, or UNSET
  for no-argument opcodes (already gated by opcode_has_argument)
- _size: always 2 — EXTENDED_ARG accumulation happens after this loop
  in _remove_extended_args, not during construction
- _extended_args: always None in this path

CPU profiling data (where the impact is more visible):

| Hotspot | Before | After |
|---|---|---|
| `ConcreteInstr.__init__` own | 3.11% | eliminated |
| `ConcreteInstr._set` own | 1.68% | eliminated |
| `ConcreteInstr._check_arg` own | 1.82% | eliminated |
| `ConcreteInstr._from_opcode` own | — | 2.77% |
| `ConcreteBytecode.from_code` own | 6.21% | 4.55% |

Throughput (usual Bytecode.from_code().to_code() round-trip,
1 second timed window, 5 runs):

| | r/s range |
|---|---|
| Before | ~143 |
| After | ~148 |
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.19%. Comparing base (25bf1bc) to head (fd74bbf).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #197      +/-   ##
==========================================
+ Coverage   95.17%   95.19%   +0.02%     
==========================================
  Files           7        7              
  Lines        2051     2061      +10     
  Branches      446      446              
==========================================
+ Hits         1952     1962      +10     
  Misses         55       55              
  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 10, 2026 18:32
@MatthieuDartiailh MatthieuDartiailh merged commit b67adbf into MatthieuDartiailh:main May 11, 2026
10 checks passed
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