Skip to content

Speed up _sre matching of runs of literal characters #161335

Open

Description

@MercylinPreethi

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

Activity

  1. added a commit that references this issue on Nov 4, 2026
  2. picnixz commented on Nov 4, 2026

    @picnixz
    Member

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

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions