[Python-checkins] bpo-34636: Use fast path for more chars in SRE category macros. (GH-9170)

Miss Islington (bot) webhook-mailer at python.org
Tue Sep 11 18:48:02 EDT 2018


https://github.com/python/cpython/commit/ec014a101a7f6243b95dfc08acfe1542b9fa5d39
commit: ec014a101a7f6243b95dfc08acfe1542b9fa5d39
branch: master
author: Sergey Fedoseev <fedoseev.sergey at gmail.com>
committer: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
date: 2018-09-11T15:47:59-07:00
summary:

bpo-34636: Use fast path for more chars in SRE category macros. (GH-9170)

When handling \s, \d, or \w (and their inverse) escapes in bytes regexes this a small but measurable performance improvement.

<!-- issue-number: [bpo-34636](https://www.bugs.python.org/issue34636) -->
https://bugs.python.org/issue34636
<!-- /issue-number -->

files:
A Misc/NEWS.d/next/Library/2018-09-11-15-04-05.bpo-34636.capCmt.rst
M Modules/_sre.c

diff --git a/Misc/NEWS.d/next/Library/2018-09-11-15-04-05.bpo-34636.capCmt.rst b/Misc/NEWS.d/next/Library/2018-09-11-15-04-05.bpo-34636.capCmt.rst
new file mode 100644
index 000000000000..c982b0a4cda0
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-09-11-15-04-05.bpo-34636.capCmt.rst
@@ -0,0 +1,2 @@
+Speed up re scanning of many non-matching characters for \s \w and \d within
+bytes objects. (microoptimization)
diff --git a/Modules/_sre.c b/Modules/_sre.c
index d67083037e51..483cf5e9ff9c 100644
--- a/Modules/_sre.c
+++ b/Modules/_sre.c
@@ -87,13 +87,13 @@ static const char copyright[] =
 /* search engine state */
 
 #define SRE_IS_DIGIT(ch)\
-    ((ch) < 128 && Py_ISDIGIT(ch))
+    ((ch) <= '9' && Py_ISDIGIT(ch))
 #define SRE_IS_SPACE(ch)\
-    ((ch) < 128 && Py_ISSPACE(ch))
+    ((ch) <= ' ' && Py_ISSPACE(ch))
 #define SRE_IS_LINEBREAK(ch)\
     ((ch) == '\n')
 #define SRE_IS_WORD(ch)\
-    ((ch) < 128 && (Py_ISALNUM(ch) || (ch) == '_'))
+    ((ch) <= 'z' && (Py_ISALNUM(ch) || (ch) == '_'))
 
 static unsigned int sre_lower_ascii(unsigned int ch)
 {



More information about the Python-checkins mailing list