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 8a758f5b99)

Co-authored-by: Marco Paolini <mpaolini@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2019-07-30 07:37:28 -07:00 committed by GitHub
parent 8b50e3e272
commit 9265a87742
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 2 additions and 1 deletions

View file

@ -0,0 +1 @@
Make json.loads faster for long strings. (Patch by Marco Paolini)

View 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;
}