]> granicus.if.org Git - python/commitdiff
bpo-37587: Make json.loads faster for long strings (GH-14752)
authorMarco Paolini <mpaolini@users.noreply.github.com>
Tue, 30 Jul 2019 14:16:34 +0000 (15:16 +0100)
committerNick Coghlan <ncoghlan@gmail.com>
Tue, 30 Jul 2019 14:16:34 +0000 (00:16 +1000)
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).

Misc/NEWS.d/next/Library/2019-07-13-16-02-48.bpo-37587.fd-1aF.rst [new file with mode: 0644]
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 (file)
index 0000000..80a89fe
--- /dev/null
@@ -0,0 +1 @@
+Make json.loads faster for long strings. (Patch by Marco Paolini)
index 38beb6f50d2ec0f68043298e859cde05bbcffe28..76da1d345e9df42a55dee480a6d522e8c5113063 100644 (file)
@@ -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;
             }