Skip to content

Collapse runs of adjacent LITERAL opcodes into one _sre dispatch - #161318

Closed
MercylinPreethi wants to merge 1 commit into
python:mainfrom
MercylinPreethi:perf/sre-literal-run-dispatch-main
Closed

MercylinPreethi wants to merge 1 commit into
python:mainfrom
MercylinPreethi:perf/sre-literal-run-dispatch-main

Conversation

@MercylinPreethi

Copy link
Copy Markdown

Summary

Collapse runs of adjacent LITERAL opcodes into one _sre dispatch.

In SRE(match), SRE_OP_LITERAL and SRE_OP_LITERAL_IGNORE each matched one character and then went back to the computed-goto dispatch. This change loops while the next opcode is the same literal opcode, so a run of N literal characters costs one dispatch instead of N.

Matching behavior is unchanged. The loop reads the next opcode right after each literal, which the old code did not do. That read stays in bounds because every compiled SRE pattern ends with SUCCESS or FAILURE.

Files: Modules/_sre/sre_lib.h

Measured

Text Processing workload, gain in % (positive means faster).

Vendor CPU OS Compiler Single thread All threads
AMD Ryzen 7 9700X (8 cores, 16 threads) Windows clang-cl 22.1 +2.1 +2.2
Intel Core Ultra 9 285K (24 cores) Windows clang-cl 22.1 -1.2 -1.7
Apple Mac Air macOS clang +5.8 +4.3

Test plan

  • Accuracy: same 3,914 tokens as upstream (identical digest).
  • Unit test: Text Processing unit test passes on 3.13.1 and latest release, with and without the patch.
  • Timing: single core and multi core on all three machines (table above).
  • Regex tests: test_re passes on 3.13.1 and latest with the patch.

@python-cla-bot

python-cla-bot Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app

bedevere-app Bot commented Nov 4, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@picnixz picnixz closed this Nov 4, 2026
@picnixz

picnixz commented Nov 4, 2026

Copy link
Copy Markdown
Member

This requires an issue before as well as a justification of the change. The benchmarks aren't clear on what we actually gain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants