Skip to content

gh-133968: Add fast path to PyUnicodeWriter_WriteStr() - #133969

Merged
vstinner merged 1 commit into
python:mainfrom
vstinner:write_str
May 13, 2025
Merged

gh-133968: Add fast path to PyUnicodeWriter_WriteStr()#133969
vstinner merged 1 commit into
python:mainfrom
vstinner:write_str

Conversation

@vstinner

@vstinnervstinner commented May 13, 2025

Copy link
Copy Markdown
Member

Don't call PyObject_Str() if the input type is str.

Don't call PyObject_Str() if the input type is str.
@vstinner

Copy link
Copy Markdown
MemberAuthor

Microbenchmark:

Mean +- std dev: [ref] 70.5 ns +- 1.1 ns -> [change] 58.3 ns +- 3.8 ns: 1.21x faster

from_testcapiimportPyUnicodeWriterimportpyperfrange_100=range(100)
defbench_write_str():
writer=PyUnicodeWriter(0)
for_inrange_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)

@vstinner

Copy link
Copy Markdown
MemberAuthor

JSON benchmark: #133832 (comment)

Benchmarkrefchange
encode 100 booleans9.52 us7.18 us: 1.33x faster
encode 100 integers13.9 us11.6 us: 1.20x faster
encode 100 floats25.1 us20.6 us: 1.22x faster
encode 100 "ascii" strings17.3 us13.2 us: 1.31x faster
encode ascii string len=100913 ns902 ns: 1.01x faster
encode escaped string len=1281.11 us1.10 us: 1.01x faster
encode Unicode string len=1001.09 us1.07 us: 1.02x faster
encode 1000 booleans59.6 us38.9 us: 1.53x faster
encode 1000 integers104 us82.7 us: 1.26x faster
encode 1000 floats210 us166 us: 1.27x faster
encode 1000 "ascii" strings132 us93.2 us: 1.42x faster
encode ascii string len=10003.48 us3.49 us: 1.00x slower
encode escaped string len=8964.12 us4.11 us: 1.00x faster
encode Unicode string len=10004.90 us4.91 us: 1.00x slower
encode 10000 booleans553 us343 us: 1.61x faster
encode 10000 integers1.00 ms805 us: 1.25x faster
encode 10000 floats2.07 ms1.62 ms: 1.28x faster
encode 10000 "ascii" strings1.27 ms868 us: 1.46x faster
encode ascii string len=1000028.4 us28.5 us: 1.00x slower
encode escaped string len=998438.5 us38.6 us: 1.00x slower
encode Unicode string len=1000042.3 us42.4 us: 1.00x slower
Geometric mean(ref)1.18x faster

Encoding booleans is now up to 1.61x faster which is quite appealing!

@vstinner
vstinner merged commit fe9f6e8 into python:mainMay 13, 2025
@vstinner
vstinner deleted the write_str branch May 13, 2025 13:31
@miss-islington-app

Copy link
Copy Markdown

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 13, 2025
…H-133969)
Don't call PyObject_Str() if the input type is str.
(cherry picked from commit fe9f6e8)
Co-authored-by: Victor Stinner <vstinner@python.org>
@bedevere-app

Copy link
Copy Markdown

GH-133971 is a backport of this pull request to the 3.14 branch.

@bedevere-appbedevere-appBot removed the needs backport to 3.14 bugs and security fixes label May 13, 2025
@vstinner

Copy link
Copy Markdown
MemberAuthor

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!

vstinner added a commit that referenced this pull request May 13, 2025
) (#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 <vstinner@python.org>
Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request Aug 4, 2025
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@vstinner