perf: bypass ConcreteInstr validation in from_code via _from_opcode#197
Merged
MatthieuDartiailh merged 1 commit intoMay 11, 2026
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
MatthieuDartiailh
approved these changes
May 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
CPU profiling data (where the impact is more visible):
ConcreteInstr.__init__ownConcreteInstr._check_argownConcreteBytecode.from_codeownThroughput (usual Bytecode.from_code().to_code() round-trip, 1 second timed window, 5 runs):