Closed
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirperformancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
I modified the json module to replace the private _PyUnicodeWriter C API with the public PyUnicodeWriter C API:
Problem: it made the json module slower. Let's investigate what's going on.
Linked PRs
- gh-133968: Use private unicode writer for json #133832
- gh-133968: Add fast path to PyUnicodeWriter_WriteStr() #133969
- [3.14] gh-133968: Add fast path to PyUnicodeWriter_WriteStr() (GH-133969) #133971
- gh-133968: Add PyUnicodeWriter_WriteASCII() function #133973
- [3.14] gh-133968: Add PyUnicodeWriter_WriteASCII() function (#133973) #134974
- gh-133968: Update PyUnicodeWriter_WriteASCII() documentation #135297
Metadata
Metadata
Assignees
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirperformancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Projects
- StatusShow more project fieldsDone
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…