-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
json: Fast path for string encoding #133239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
https://gist.github.com/methane/e080ec9783db2a313f40a2b9e1837e72
Benchmark hidden because not significant (10): json_dumps: List of 256 floats, json_dumps(ensure_ascii=False): List of 256 floats, json_loads: List of 256 booleans, json_loads: List of 256 ASCII strings, json_loads: List of 256 dicts with 1 int, json_loads: Medium complex object, json_loads: Complex object, json_loads: Dict with 256 lists of 256 dicts with 1 int, json_loads: List of 256 stringsensure_ascii=False, json_loads: Complex objectensure_ascii=False |
for (i = 0, output_size = 0; i < input_chars; i++) { | ||
Py_UCS4 c = PyUnicode_READ(kind, input, i); | ||
Py_ssize_t d; | ||
if (S_CHAR(c)) { | ||
d = 1; | ||
} | ||
else { | ||
switch(c) { | ||
case '\\': case '"': case '\b': case '\f': | ||
case '\n': case '\r': case '\t': | ||
d = 2; break; | ||
default: | ||
d = c >= 0x10000 ? 12 : 6; | ||
} | ||
} | ||
if (output_size > PY_SSIZE_T_MAX - d) { | ||
PyErr_SetString(PyExc_OverflowError, "string is too long to escape"); | ||
return NULL; | ||
} | ||
output_size += d; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we rewrite this to be faster? Because that's the only difference in the fast path between both PRs.
pyperformance (with
--enable-optimizations
)jsonyx-performance-tests (with
--enable-optimizations
)