Skip to content

Add fast path to json string encoding #135336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
nineteendo opened this issue Jun 10, 2025 · 0 comments
Open

Add fast path to json string encoding #135336

nineteendo opened this issue Jun 10, 2025 · 0 comments
Labels
extension-modules C modules in the Modules dir performance Performance or resource usage type-feature A feature request or enhancement

Comments

@nineteendo
Copy link
Contributor

nineteendo commented Jun 10, 2025

Feature or enhancement

Proposal:

Most JSON strings consist solely of ASCII characters that don’t require escaping, so adding a fast path for those would make a big difference. In these cases, we can avoid allocating a new string by writing the original string directly:

PyUnicodeWriter_WriteChar(writer, '"')
PyUnicodeWriter_WriteStr(writer, pystr) // original string
PyUnicodeWriter_WriteChar(writer, '"')

Benchmarks have shown that this roughly 2.2x faster, see PR.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

@nineteendo nineteendo added the type-feature A feature request or enhancement label Jun 10, 2025
@picnixz picnixz added performance Performance or resource usage extension-modules C modules in the Modules dir labels Jun 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir performance Performance or resource usage type-feature A feature request or enhancement
Projects
Status: No status
Development

No branches or pull requests

2 participants