-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
gh-133968: Add fast path to PyUnicodeWriter_WriteStr() #133969
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
Conversation
Don't call PyObject_Str() if the input type is str.
Microbenchmark: Mean +- std dev: [ref] 70.5 ns +- 1.1 ns -> [change] 58.3 ns +- 3.8 ns: 1.21x faster from _testcapi import PyUnicodeWriter
import pyperf
range_100 = range(100)
def bench_write_str():
writer = PyUnicodeWriter(0)
for _ in range_100:
writer.write_str("true")
writer.write_str("true")
writer.write_str("true")
writer.write_str("true")
writer.write_str("true")
writer.write_str("true")
writer.write_str("true")
writer.write_str("true")
writer.write_str("true")
writer.write_str("true")
runner = pyperf.Runner()
runner.bench_func('write_str', bench_write_str, inner_loops=1_000) |
JSON benchmark: #133832 (comment)
Encoding booleans is now up to 1.61x faster which is quite appealing! |
Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14. |
…H-133969) Don't call PyObject_Str() if the input type is str. (cherry picked from commit fe9f6e8) Co-authored-by: Victor Stinner <[email protected]>
GH-133971 is a backport of this pull request to the 3.14 branch. |
When I wrote PyUnicodeWriter_WriteStr(), I skipped this fast path since PyObject_Str() already has a fast path. But it seems like adding one in PyUnicodeWriter_WriteStr() makes a big difference on microbenchmarks! |
) (#133971) gh-133968: Add fast path to PyUnicodeWriter_WriteStr() (GH-133969) Don't call PyObject_Str() if the input type is str. (cherry picked from commit fe9f6e8) Co-authored-by: Victor Stinner <[email protected]>
Don't call PyObject_Str() if the input type is str.