[Python-checkins] bpo-37587: Make json.loads faster for long strings (GH-14752)

Miss Islington (bot) webhook-mailer at python.org
Tue Jul 30 10:37:51 EDT 2019


https://github.com/python/cpython/commit/9265a877426af4fa5c44cc8482e0198806889350
commit: 9265a877426af4fa5c44cc8482e0198806889350
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-07-30T07:37:28-07:00
summary:

bpo-37587: Make json.loads faster for long strings (GH-14752)


When scanning the string, most characters are valid, so
checking for invalid characters first means never needing
to check the value of strict on valid strings, and only
needing to check it on invalid characters when doing
non-strict parsing of invalid strings.

This provides a measurable reduction in per-character
processing time (~11% in the pre-merge patch testing).
(cherry picked from commit 8a758f5b99c5fc3fd32edeac049d7d4a4b7cc163)

Co-authored-by: Marco Paolini <mpaolini at users.noreply.github.com>

files:
A Misc/NEWS.d/next/Library/2019-07-13-16-02-48.bpo-37587.fd-1aF.rst
M Modules/_json.c

diff --git a/Misc/NEWS.d/next/Library/2019-07-13-16-02-48.bpo-37587.fd-1aF.rst b/Misc/NEWS.d/next/Library/2019-07-13-16-02-48.bpo-37587.fd-1aF.rst
new file mode 100644
index 000000000000..80a89feab0ce
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-07-13-16-02-48.bpo-37587.fd-1aF.rst
@@ -0,0 +1 @@
+Make json.loads faster for long strings. (Patch by Marco Paolini)
diff --git a/Modules/_json.c b/Modules/_json.c
index e3aa997598fc..048a9654ce18 100644
--- a/Modules/_json.c
+++ b/Modules/_json.c
@@ -439,7 +439,7 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next
             if (c == '"' || c == '\\') {
                 break;
             }
-            else if (strict && c <= 0x1f) {
+            else if (c <= 0x1f && strict) {
                 raise_errmsg("Invalid control character at", pystr, next);
                 goto bail;
             }



More information about the Python-checkins mailing list