FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-152056: Compile single-category character sets to a bare CATEGORY opcode by eendebakpt · Pull Request #152057 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .rst  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
10 changes: 6 additions & 4 deletions Doc/whatsnew/3.16.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,12 @@ re
--

* Character class escapes (``\d``, ``\D``, ``\s``, ``\S``, ``\w`` and ``\W``)
outside a character set are now compiled to a single ``CATEGORY`` opcode
instead of being wrapped in an ``IN`` block. This speeds up matching of
patterns such as ``\d+`` and reduces the size of the compiled byte code.
(Contributed by Serhiy Storchaka in :gh:`152033`.)
outside a character set, and character sets containing a single such escape
(such as ``[\d]`` or ``[^\s]``), are now compiled to a single ``CATEGORY``
opcode instead of being wrapped in an ``IN`` block. This speeds up matching
of patterns such as ``\d+`` and reduces the size of the compiled byte code.
(Contributed by Serhiy Storchaka in :gh:`152033` and Pieter Eendebak in
:gh:`152056`.)

module_name
-----------
Expand Down
6 changes: 6 additions & 0 deletions Lib/re/_parser.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,12 @@ def _parse(source, state, verbose, nested, first=False):
subpatternappend((NOT_LITERAL, set[0][1]))
else:
subpatternappend(set[0])
elif _len(set) == 1 and set[0][0] is CATEGORY:
# optimization: a lone category like [\d] or [^\d]
if negate:
subpatternappend((CATEGORY, CH_NEGATE[set[0][1]]))
else:
subpatternappend(set[0])
else:
if negate:
set.insert(0, (NEGATE, None))
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Optimize matching of a character set that contains a single character
category, such as ``[\d]`` or ``[^\s]``: it is now compiled to a single
``CATEGORY`` opcode, the same as the corresponding ``\d`` or ``\S`` escape,
instead of being wrapped in an ``IN`` block. This speeds up matching and
reduces the size of the compiled byte code. Patch by Pieter Eendebak.
Loading

Back | FazBrowse Home | New Git URL