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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Doc/whatsnew/3.7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@ Optimizations
expressions <re>`. Searching some patterns can now be up to 20 times faster.
(Contributed by Serhiy Storchaka in :issue:`30285`.)

* :func:`re.compile` now converts ``flags`` parameter to int object if
it is ``RegexFlag``. It is now as fast as Python 3.5, and faster than
Python 3.6 about 10% depending on the pattern.
(Contributed by INADA Naoki in :issue:`31671`.)

* :meth:`selectors.EpollSelector.modify`, :meth:`selectors.PollSelector.modify`
and :meth:`selectors.DevpollSelector.modify` may be around 10% faster under
heavy loads. (Contributed by Giampaolo Rodola' in :issue:`30014`)
Expand Down
4 changes: 4 additions & 0 deletions Lib/re.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ def escape(pattern):
_MAXCACHE = 512
def _compile(pattern, flags):
# internal: compile pattern
if isinstance(flags, RegexFlag):
flags = flags.value
try:
return _cache[type(pattern), pattern, flags]
except KeyError:
Expand Down Expand Up @@ -330,6 +332,8 @@ def _pickle(p):
class Scanner:
def __init__(self, lexicon, flags=0):
from sre_constants import BRANCH, SUBPATTERN
if isinstance(flags, RegexFlag):
flags = flags.value
self.lexicon = lexicon
# combine phrases into a compound pattern
p = []
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Now ``re.compile()`` converts passed RegexFlag to normal int object before
compiling. bm_regex_compile benchmark shows 14% performance improvements.