Skip to content

perf: avoid redundant validation in copy() and __iter__#193

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

perf: avoid redundant validation in copy() and __iter__#193
MatthieuDartiailh merged 1 commit into
MatthieuDartiailh:mainfrom
P403n1x87:perf/avoid-repeated-check

Conversation

@P403n1x87
Copy link
Copy Markdown
Contributor

@P403n1x87 P403n1x87 commented May 8, 2026

BaseInstr.copy() previously reconstructed instructions via __init__, triggering the full _set → _check_arg validation chain on every copy even though the source instruction is already known to be valid. Replace with object.__new__ + direct slot assignment to bypass validation entirely.

_BaseBytecodeList.__iter__ ran _check_instr on every yielded item, but Bytecode.__iter__ (which calls this via super()) was already doing the same check — a redundant double-validation on every iteration.

The script used for validating the change does a round-trip decompile/recompile for 1 second:

from time import perf_counter_ns as time

def test_roundtrip(code_object):
    end = time() + 1e9  # 1 second
    c = 0
    while time() < end:
        c += 1
        Bytecode.from_code(code_object).to_code()
    print(f"{code_object.co_name!r}: {c} round-trips/s")

Profiling shows the following own CPU time figures:

Function Before After Relative change
Instr._check_arg 7.85% 3.13% −4.72%
BaseInstr._set 2.57% 1.37% −1.20%
_BaseBytecodeList.__iter__ 2.18% 0.0% -2.18%

Throughput (number of iteration within the 1 second duration) goes from ~90 to ~105.

@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.17%. Comparing base (66584dc) to head (40f07b3).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #193   +/-   ##
=======================================
  Coverage   95.17%   95.17%           
=======================================
  Files           7        7           
  Lines        2032     2032           
  Branches      449      448    -1     
=======================================
  Hits         1934     1934           
  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.

BaseInstr.copy() previously reconstructed instructions via __init__,
triggering the full _set → _check_arg validation chain on every copy
even though the source instruction is already known to be valid.
Replace with object.__new__ + direct slot assignment to bypass
validation entirely.

_BaseBytecodeList.__iter__ ran _check_instr on every yielded item, but
Bytecode.__iter__ (which calls this via super()) was already doing the
same check — a redundant double-validation on every iteration.

Profiling shows:
- Instr._check_arg own time: 7.85% → 3.13% (−4.72%)
- BaseInstr._set own time:   2.57% → 1.37% (−1.20%)
- _BaseBytecodeList.__iter__ eliminated (was 2.18%)
@P403n1x87 P403n1x87 force-pushed the perf/avoid-repeated-check branch from 86cf482 to 40f07b3 Compare May 8, 2026 11:41
@MatthieuDartiailh
Copy link
Copy Markdown
Owner

LGTM I will merge when you mark this as ready.

@P403n1x87 P403n1x87 marked this pull request as ready for review May 8, 2026 12:58
@P403n1x87
Copy link
Copy Markdown
Contributor Author

@MatthieuDartiailh open for review 🙏

@MatthieuDartiailh MatthieuDartiailh merged commit 8bf35e4 into MatthieuDartiailh:main May 8, 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