Skip to content

Commit c1c47c1

Browse files
authored
bpo-31671: re: Convert RegexFlag to int before compile (GH-3862)
sre_compile does bit test (e.g. `flags & SRE_FLAG_IGNORECASE`) in loop. `IntFlag.__and__` and `IntFlag.__new__` made it slower. So this commit convert it to normal int before passing flags to `sre_compile()`.
1 parent af810b3 commit c1c47c1

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

Doc/whatsnew/3.7.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,11 @@ Optimizations
326326
expressions <re>`. Searching some patterns can now be up to 20 times faster.
327327
(Contributed by Serhiy Storchaka in :issue:`30285`.)
328328

329+
* :func:`re.compile` now converts ``flags`` parameter to int object if
330+
it is ``RegexFlag``. It is now as fast as Python 3.5, and faster than
331+
Python 3.6 about 10% depending on the pattern.
332+
(Contributed by INADA Naoki in :issue:`31671`.)
333+
329334
* :meth:`selectors.EpollSelector.modify`, :meth:`selectors.PollSelector.modify`
330335
and :meth:`selectors.DevpollSelector.modify` may be around 10% faster under
331336
heavy loads. (Contributed by Giampaolo Rodola' in :issue:`30014`)

Lib/re.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ def escape(pattern):
275275
_MAXCACHE = 512
276276
def _compile(pattern, flags):
277277
# internal: compile pattern
278+
if isinstance(flags, RegexFlag):
279+
flags = flags.value
278280
try:
279281
return _cache[type(pattern), pattern, flags]
280282
except KeyError:
@@ -331,6 +333,8 @@ def _pickle(p):
331333
class Scanner:
332334
def __init__(self, lexicon, flags=0):
333335
from sre_constants import BRANCH, SUBPATTERN
336+
if isinstance(flags, RegexFlag):
337+
flags = flags.value
334338
self.lexicon = lexicon
335339
# combine phrases into a compound pattern
336340
p = []
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Now ``re.compile()`` converts passed RegexFlag to normal int object before
2+
compiling. bm_regex_compile benchmark shows 14% performance improvements.

0 commit comments

Comments
 (0)