Skip to content

Using the public PyUnicodeWriter C API made the json module slower #133968

Activity

  1. added
    performancePerformance or resource usage
    stdlibStandard Library Python modules in the Lib/ directory
    on May 13, 2025
  2. vstinner commented on May 13, 2025

    @vstinner
    MemberAuthor

    See also #133186.

  3. added 2 commits that reference this issue on May 13, 2025
  4. added a commit that references this issue on May 13, 2025
  5. added 3 commits that reference this issue on May 13, 2025
  6. encukou commented on May 15, 2025

    @encukou
    Member

    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
  7. vstinner commented on May 15, 2025

    @vstinner
    MemberAuthor

    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.

    ascii_decode() is already highly optimized.

  8. encukou commented on May 17, 2025

    @encukou
    Member

    ascii_decode() is already highly optimized.

    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; }

  9. 6 remaining items

  10. added a commit that references this issue on Jun 9, 2025
  11. vstinner commented on Jun 9, 2025

    @vstinner
    MemberAuthor

    I close the issue. Multiple changes optimized the json module 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.

  12. added a commit that references this issue on Jun 19, 2025
  13. added 4 commits that reference this issue on Jul 12, 2025
  14. added 4 commits that reference this issue on Aug 4, 2025
  15. added a commit that references this issue on Aug 19, 2025
  16. added a commit that references this issue on Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirperformancePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions