Skip to content

gh-161335: Collapse runs of adjacent LITERAL opcodes into one _sre dispatch - #161332

Open
MercylinPreethi wants to merge 2 commits into
python:mainfrom
MercylinPreethi:perf/sre-literal-run-dispatch-main
Open

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

Conversation

@MercylinPreethi

@MercylinPreethi MercylinPreethi commented Nov 4, 2026 •

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

What we gain

Each literal character in a pattern is one opcode, and before this change each one paid a full dispatch. Patterns with long literal runs ("Content-Length: ", (\d+) bytes received, foo|bar|baz) now match those runs in a tight loop. Patterns made of classes and repeats (\w+, .*?) are unaffected.

Measured

pyperformance regex benchmarks, before vs after, compared with pyperf compare_to:

Benchmark AMD Ryzen 7 9700X Intel Core Ultra 9 285K Apple Mac Air
regex_v8 4.22% faster (significant) 1.24% slower (significant) 10.37% faster (significant)
regex_effbot 3.88% faster (significant) 3.48% faster (significant) 9.16% faster (significant)
regex_dna 3.75% faster (significant) 2.17% faster (significant) 8.96% faster (significant)
  • AMD and Apple: faster on all three benchmarks, about 4% on AMD and 9–10% on Apple.
  • Intel: faster on regex_effbot (+3.5%) and regex_dna (+2.2%), but 1.2% slower on regex_v8. regex_v8 uses many short patterns with few literal runs, so the extra opcode read in the new loop isn't paid back there on this CPU.
  • Overall, 8 of 9 results are significant gains, and the one slowdown is small.

Test plan

  • Regex tests: test_re passes on latest main with the patch.
  • Accuracy: identical match results on 87,222 regex cases, before vs after.
  • Timing: pyperformance regex_v8, regex_effbot, regex_dna on all three machines (table above).

@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.

@MercylinPreethi MercylinPreethi changed the title Collapse runs of adjacent LITERAL opcodes into one _sre dispatch gh-161335: Collapse runs of adjacent LITERAL opcodes into one _sre dispatch Nov 4, 2026
@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant