You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Using the public PyUnicodeWriter C API made the json module slower #133968
Could you try overoptimizing PyUnicodeWriter_WriteUTF8 and benchmarking that, to see how fast the existing function can “theoretically” be?
I'm thinking something like:
in the JSON module, force the constants ("null" etc) to be size_t-aligned
add private (ABI-only) function _PyUnicodeWriter_WriteUTF8_SmallAligned, which requires size_t-aligned input and size <= sizeof(size_t)
make PyUnicodeWriter_WriteUTF8 a macro that calls _PyUnicodeWriter_WriteUTF8_SmallAligned if the need are met, and PyUnicodeWriter_WriteUTF8 otherwise
in the JSON module, force the constants ("null" etc) to be size_t-aligned
I don't know how to guarantee that.
add private (ABI-only) function _PyUnicodeWriter_WriteUTF8_SmallAligned, which requires size_t-aligned input and size <= sizeof(size_t)
Why do you want to limit the size to sizeof(size_t)? Which kind of optimization are you thinking of? find_first_nonascii() already takes care of the different cases with different optimizations.
Indeed it is! After playing around a bit, I could only optimize short-ASCII-strings at the expense of other strings, or a lot of code duplication.
Sorry for the noise!
I don't know how to guarantee that.
For the record: alignas; in old C it could be union { char my_string[N]; size_t _align_me_please; }
Activity
vstinner commentedon May 13, 2025
See also #133186.
pythongh-133968: Add fast path to PyUnicodeWriter_WriteStr()
gh-133968: Add fast path to PyUnicodeWriter_WriteStr() (#133969)
pythongh-133968: Add fast path to PyUnicodeWriter_WriteStr() (pythonG…
[3.14] gh-133968: Add fast path to PyUnicodeWriter_WriteStr() (GH-133969
pythongh-133968: Add PyUnicodeWriter_WriteASCII() function
pythongh-133968: Add PyUnicodeWriter_WriteASCII() function
encukou commentedon May 15, 2025
Could you try overoptimizing
PyUnicodeWriter_WriteUTF8and benchmarking that, to see how fast the existing function can “theoretically” be?I'm thinking something like:
"null"etc) to besize_t-aligned_PyUnicodeWriter_WriteUTF8_SmallAligned, which requiressize_t-aligned input and size <= sizeof(size_t)PyUnicodeWriter_WriteUTF8a macro that calls_PyUnicodeWriter_WriteUTF8_SmallAlignedif the need are met, andPyUnicodeWriter_WriteUTF8otherwisevstinner commentedon May 15, 2025
I don't know how to guarantee that.
Why do you want to limit the size to sizeof(size_t)? Which kind of optimization are you thinking of? find_first_nonascii() already takes care of the different cases with different optimizations.
ascii_decode() is already highly optimized.
encukou commentedon May 17, 2025
Indeed it is! After playing around a bit, I could only optimize short-ASCII-strings at the expense of other strings, or a lot of code duplication.
Sorry for the noise!
For the record:
alignas; in old C it could beunion { char my_string[N]; size_t _align_me_please; }6 remaining items
gh-133968: Update PyUnicodeWriter_WriteASCII() documentation (#135297)
vstinner commentedon Jun 9, 2025
I close the issue. Multiple changes optimized the
jsonmodule which should be "less slow" than 3.13 in 3.14 and main (3.15) branches now.If someone has more optimization ideas, I suggest opening a new issue.
pythongh-133968: Update PyUnicodeWriter_WriteASCII() documentation (p…
pythongh-133968: Add fast path to PyUnicodeWriter_WriteStr() (python#…
pythongh-133968: Add PyUnicodeWriter_WriteASCII() function (python#13…
pythongh-133968: Create the Unicode writer on demand in json (python#…
pythongh-133968: Update PyUnicodeWriter_WriteASCII() documentation (p…
pythongh-133968: Add fast path to PyUnicodeWriter_WriteStr() (python#…
pythongh-133968: Add PyUnicodeWriter_WriteASCII() function (python#13…
pythongh-133968: Create the Unicode writer on demand in json (python#…
pythongh-133968: Update PyUnicodeWriter_WriteASCII() documentation (p…
pythongh-133968: Update PyUnicodeWriter_WriteASCII() documentation (p…
pythongh-133968: Add PyUnicodeWriter_WriteASCII() function (python#13…