]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-152033: Optimize category escapes outside character sets (GH-152035)
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 24 Jun 2026 05:49:14 +0000 (08:49 +0300)
committerGitHub <noreply@github.com>
Wed, 24 Jun 2026 05:49:14 +0000 (08:49 +0300)
commitfde4cf862c80d0b8a8da5dd12c8d7d311f80355b
tree5f5b00c1cd03d623f9ff166f2dd5888d782a93c4
parentfcda96fbf399d069d11f7e874352ad03273cf0b7
gh-152033: Optimize category escapes outside character sets (GH-152035)

Character class escapes (``\d``, ``\D``, ``\s``, ``\S``, ``\w`` and
``\W``) that occur outside a character set are now compiled directly to a
single CATEGORY opcode instead of being wrapped in an IN block.  This
removes the IN wrapper (three code words) and an indirect charset() call,
and makes such an escape a simple repeatable unit so that, for example,
``\d+`` uses the REPEAT_ONE fast path; a CATEGORY case is added to
SRE(count).

The transformation preserves behaviour exactly.  For category-heavy
patterns the compiled byte code is about 20% smaller and matching is up
to ~2x faster, with no effect on patterns that do not use bare category
escapes.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Doc/whatsnew/3.16.rst
Lib/re/_compiler.py
Lib/re/_parser.py
Misc/NEWS.d/next/Library/2026-06-23-22-15-00.gh-issue-152033.Ct1Egy.rst [new file with mode: 0644]
Modules/_sre/sre.c
Modules/_sre/sre_lib.h