Skip to content

Commit 9265a87

Browse files
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 8a758f5) Co-authored-by: Marco Paolini <mpaolini@users.noreply.github.com>
1 parent 8b50e3e commit 9265a87

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make json.loads faster for long strings. (Patch by Marco Paolini)

Modules/_json.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next
439439
if (c == '"' || c == '\\') {
440440
break;
441441
}
442-
else if (strict && c <= 0x1f) {
442+
else if (c <= 0x1f && strict) {
443443
raise_errmsg("Invalid control character at", pystr, next);
444444
goto bail;
445445
}

0 commit comments

Comments
 (0)