You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Speed up _sre matching of runs of literal characters #161335
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.
Activity
pythongh-161335: Add NEWS entry
picnixz commentedon Nov 4, 2026
The slowdown is still significantly slower. Were these PGO/LTO results?