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 — Speed up _sre matching of runs of literal characters · Issue #161335 · python/cpython

Speed up _sre matching of runs of literal characters #161335

New issue
Copy link
New issue
Copy link
Open
Open
Speed up _sre matching of runs of literal characters#161335
Copy link
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirperformancePerformance or resource usagePerformance or resource usagetopic-regextype-featureA feature request or enhancementA feature request or enhancement

Description

@MercylinPreethi
MercylinPreethi
opened on Nov 4, 2026
Issue body actions

Feature or enhancement

Proposal

In SRE(match) (Modules/_sre/sre_lib.h), SRE_OP_LITERAL and SRE_OP_LITERAL_IGNORE match one character and then return to the opcode dispatch. A literal string in a pattern compiles to one LITERAL opcode per character, so "Content-Length: " costs 16 dispatches.

This proposes looping inside the LITERAL / LITERAL_IGNORE handler 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 extra read of the next opcode stays in bounds, because every compiled pattern ends with SUCCESS or FAILURE.

This is an internal speed-up only: no API or behavior change.

Benchmarks

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)

8 of 9 results are significant gains. The one slowdown (Intel regex_v8, -1.2%) is small.

Testing

  • test_re passes with the change.
  • Match results are identical before and after on 87,222 regex cases.

Implementation: #161332

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

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

Activity

  1. MercylinPreethi
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Nov 4, 2026
  2. MercylinPreethi
    added a commit that references this issue on Nov 4, 2026

    pythongh-161335: Add NEWS entry

    4377a9f
  3. terryjreedy
    added
    topic-regex
    performancePerformance or resource usagePerformance or resource usage
    on Nov 4, 2026
  4. picnixz commented on Nov 4, 2026

    @picnixz
    picnixz
    on Nov 4, 2026
    Member
    More actions

    The slowdown is still significantly slower. Were these PGO/LTO results?

  5. aisk
    added
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    on Nov 5, 2026
  6. MercylinPreethi commented on Nov 12, 2026

    @MercylinPreethi
    MercylinPreethi
    on Nov 12, 2026
    Author
    More actions

    No, It's no PGO/LTO results

  7. picnixz commented on Nov 12, 2026

    @picnixz
    picnixz
    on Nov 12, 2026
    Member
    More actions

    Are they at least release builds?

  8. MercylinPreethi commented on Nov 12, 2026

    @MercylinPreethi
    MercylinPreethi
    on Nov 12, 2026
    Author
    More actions

    Yes, it's a release build

  9. picnixz commented on Nov 12, 2026

    @picnixz
    picnixz
    on Nov 12, 2026
    Member
    More actions

    Can you check with PGO/LTO builds as well? there might have some differences.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirC modules in the Modules dirperformancePerformance or resource usagePerformance or resource usagetopic-regextype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      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.