Skip to content

Add PyUnicodeWriter_WriteASCII() function #65

@vstinner

Description

@vstinner

Hi,

I propose adding the following function the Python 3.15 C API.

API: int PyUnicodeWriter_WriteASCII(PyUnicodeWriter *writer, const char *str, Py_ssize_t size)

Documentation:

Write the ASCII string str into writer.

size is the string length in bytes. If size is equal to -1, call strlen(str) to get the string length.

str must only contain ASCII characters. The behavior is undefined if str contains non-ASCII characters.

On success, return 0.
On error, set an exception, leave the writer unchanged, and return -1.

The function is up to 2.0x faster than PyUnicodeWriter_WriteUTF8().

There are 2 reasons to add this function:

  • Get the same performance when updating code using the old private _PyUnicodeWriter API to use the new public PyUnicodeWriter API.
  • Get the best performance for new code using the public PyUnicodeWriter API: make the API appealing in term of performance.

Background: Using the public PyUnicodeWriter C API made the json module slower.

Vote:

Metadata

Metadata

Assignees

No one assigned

    Labels

    voteThe WG is voting (or has voted)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions