Uh oh!
There was an error while loading. Please reload this page.
gh-135336: Add fast path to json string encoding - #133239
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 |
nineteendo
commented
May 9, 2025
@mdboom do you have the results of the Faster CPython infrastructure? |
mdboom
commented
May 10, 2025
Sorry, forgot to come back to them. Confirmed 14% faster on json_dumps benchmark. In the noise for the others (as one would expect). |
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as resolved.
This comment was marked as resolved.
ZeroIntensity
left a comment
There was a problem hiding this comment.
Some very high level comments. I haven't dove too deep into the actual implementation yet.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
ZeroIntensity
commented
Jun 10, 2025
It would also be good to make an issue explaining the rationale and whatnot, and a blurb entry containing the performance increase. |
methane
commented
Jun 10, 2025
Before merging this, we need to decide using private _PyUnicodeWriter APIs or not. |
serhiy-storchaka
left a comment
There was a problem hiding this comment.
This adds quite a bit of code. Could not it be shared between py_encode_basestring and write_escaped_unicode?
nineteendo
commented
Jun 10, 2025
I've created an issue and re-used shared code, but https://blurb-it.herokuapp.com is down |
I ran my benchmark #133832 (comment) on this PR. I rebased the PR on the main branch. Encoding a list of ASCII strings is up to 1.7x faster, it's impressive! Sadly, encoding a long ASCII string is always slower (between 1.05x and 1.09x slower).
Benchmark hidden because not significant (2): encode 1000 integers, encode 1000 floats UPDATE: I had to re-run the benchmark since my first attempt was on debug builds :-( |
vstinner
commented
Jun 11, 2025
Whenever possible, I would prefer to use the public |
vstinner
commented
Jun 11, 2025
You can install the blurb tool (pip install blurb) and run it locally in a terminal to add a NEWS entry. |
serhiy-storchaka
commented
Jun 11, 2025
This is not what I had in mind, although it does speed up a common case. Currently, encoding is two-pass. First we calculate the size of the encoded string, then create the Unicode object of such size and fill it char by char. This PR uses the first step to determine whether we can get rid of the intermediate Unicode object (if there are no characters that need escaping). This helps for booleand, numbers, and many simple strings. But we can get rid of the intermediate Unicode object in all cases -- just reserve space in PyUnicodeWriter and write the encoded string directly there. For performance, we should not use high-level API like |
This not exposed through the public API. You could maybe try to use |
nineteendo
commented
Jun 12, 2025
Not sure why but calling |
nineteendo
commented
Jul 24, 2025
Is there anything that still needs to happen before this can be merged? |
nineteendo
commented
Jul 26, 2025
@serhiy-storchaka could this be merged? We can always improve this further in a followup PR. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
vstinner
commented
Aug 7, 2025
Merged. Thanks @nineteendo! |
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Victor Stinner <vstinner@python.org>
pyperformance (with
--enable-optimizationsand--with-lto)jsonyx-performance-tests (with
--enable-optimizationsand--with-lto)