GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-161335: Collapse runs of adjacent LITERAL opcodes into one _sre dispatch by MercylinPreethi · Pull Request #161332 · python/cpython

Conversation

@MercylinPreethi

@MercylinPreethi MercylinPreethi commented Nov 4, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

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).
  • Issue: Speed up _sre matching of runs of literal characters #161335

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@MercylinPreethi
Collapse runs of adjacent LITERAL opcodes into one _sre dispatch
52ab8b0
@python-cla-bot

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

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added the awaiting review label Nov 4, 2026
@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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@MercylinPreethi MercylinPreethi mentioned this pull request Nov 4, 2026
Speed up _sre matching of runs of literal characters #161335
Open
@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
@MercylinPreethi
pythongh-161335: Add NEWS entry
4377a9f
@picnixz

picnixz commented Nov 6, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

As asked:

The slowdown is still significantly slower, namely the description is not accurate and underestimates the consequences. Were these PGO/LTO results?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting review type-feature A feature request or enhancement

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@MercylinPreethi @picnixz
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.