Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
vstinner merged 1 commit intopython:mainfromvstinner:write_str
May 13, 2025

Conversation

vstinner
Copy link
Member

@vstinnervstinner commentedMay 13, 2025
edited by bedevere-appbot
Loading

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

nineteendo, deronnax, and BoboTiG reacted with thumbs up emoji
Don't call PyObject_Str() if the input type is str.
@vstinner
Copy link
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
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 to1.61x faster which is quite appealing!

BoboTiG reacted with heart emoji

@vstinnervstinner merged commitfe9f6e8 intopython:mainMay 13, 2025
44 checks passed
@vstinnervstinner deleted the write_str branchMay 13, 2025 13:31
@miss-islington-app
Copy link

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 requestMay 13, 2025
…H-133969)Don't call PyObject_Str() if the input type is str.(cherry picked from commitfe9f6e8)Co-authored-by: Victor Stinner <vstinner@python.org>
@bedevere-app
Copy link

GH-133971 is a backport of this pull request to the3.14 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.14bugs and security fixes labelMay 13, 2025
@vstinner
Copy link
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!

BoboTiG reacted with hooray emoji

vstinner added a commit that referenced this pull requestMay 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 commitfe9f6e8)Co-authored-by: Victor Stinner <vstinner@python.org>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

1 participant
@vstinner

[8]ページ先頭

©2009-2025 Movatter.jp