Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
0f606a63
Commit
0f606a63
authored
Mar 16, 2013
by
Serhiy Storchaka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #16564: Fixed a performance regression relative to Python 3.1 in the
caching of compiled regular expressions.
parent
de6849fc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
9 deletions
+28
-9
Lib/re.py
Lib/re.py
+25
-9
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Lib/re.py
View file @
0f606a63
...
@@ -215,8 +215,8 @@ def compile(pattern, flags=0):
...
@@ -215,8 +215,8 @@ def compile(pattern, flags=0):
def
purge
():
def
purge
():
"Clear the regular expression caches"
"Clear the regular expression caches"
_c
ompile_typed
.
cache_
clear
()
_c
ache
.
clear
()
_c
ompile_repl
.
cache_
clear
()
_c
ache_repl
.
clear
()
def
template
(
pattern
,
flags
=
0
):
def
template
(
pattern
,
flags
=
0
):
"Compile a template pattern, returning a pattern object"
"Compile a template pattern, returning a pattern object"
...
@@ -257,14 +257,19 @@ def escape(pattern):
...
@@ -257,14 +257,19 @@ def escape(pattern):
# --------------------------------------------------------------------
# --------------------------------------------------------------------
# internals
# internals
_cache
=
{}
_cache_repl
=
{}
_pattern_type
=
type
(
sre_compile
.
compile
(
""
,
0
))
_pattern_type
=
type
(
sre_compile
.
compile
(
""
,
0
))
def
_compile
(
pattern
,
flags
):
_MAXCACHE
=
512
return
_compile_typed
(
type
(
pattern
),
pattern
,
flags
)
@
functools
.
lru_cache
(
maxsize
=
500
)
def
_compile
(
pattern
,
flags
):
def
_compile_typed
(
text_bytes_type
,
pattern
,
flags
):
# internal: compile pattern
# internal: compile pattern
try
:
return
_cache
[
type
(
pattern
),
pattern
,
flags
]
except
KeyError
:
pass
if
isinstance
(
pattern
,
_pattern_type
):
if
isinstance
(
pattern
,
_pattern_type
):
if
flags
:
if
flags
:
raise
ValueError
(
raise
ValueError
(
...
@@ -272,12 +277,23 @@ def _compile_typed(text_bytes_type, pattern, flags):
...
@@ -272,12 +277,23 @@ def _compile_typed(text_bytes_type, pattern, flags):
return
pattern
return
pattern
if
not
sre_compile
.
isstring
(
pattern
):
if
not
sre_compile
.
isstring
(
pattern
):
raise
TypeError
(
"first argument must be string or compiled pattern"
)
raise
TypeError
(
"first argument must be string or compiled pattern"
)
return
sre_compile
.
compile
(
pattern
,
flags
)
p
=
sre_compile
.
compile
(
pattern
,
flags
)
if
len
(
_cache
)
>=
_MAXCACHE
:
_cache
.
clear
()
_cache
[
type
(
pattern
),
pattern
,
flags
]
=
p
return
p
@
functools
.
lru_cache
(
maxsize
=
500
)
def
_compile_repl
(
repl
,
pattern
):
def
_compile_repl
(
repl
,
pattern
):
# internal: compile replacement pattern
# internal: compile replacement pattern
return
sre_parse
.
parse_template
(
repl
,
pattern
)
try
:
return
_cache_repl
[
repl
,
pattern
]
except
KeyError
:
pass
p
=
sre_parse
.
parse_template
(
repl
,
pattern
)
if
len
(
_cache_repl
)
>=
_MAXCACHE
:
_cache_repl
.
clear
()
_cache_repl
[
repl
,
pattern
]
=
p
return
p
def
_expand
(
pattern
,
match
,
template
):
def
_expand
(
pattern
,
match
,
template
):
# internal: match.expand implementation hook
# internal: match.expand implementation hook
...
...
Misc/NEWS
View file @
0f606a63
...
@@ -233,6 +233,9 @@ Core and Builtins
...
@@ -233,6 +233,9 @@ Core and Builtins
Library
Library
-------
-------
- Issue #16564: Fixed a performance regression relative to Python 3.1 in the
caching of compiled regular expressions.
- Issue #17431: Fix missing import of BytesFeedParser in email.parser.
- Issue #17431: Fix missing import of BytesFeedParser in email.parser.
- Issue #1285086: Get rid of the refcounting hack and speed up
- Issue #1285086: Get rid of the refcounting hack and speed up
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment