Skip to content

gh-125196: Add fast-path for int in PyUnicodeWriter_WriteStr() #125214

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

Merged
merged 1 commit into from
Oct 9, 2024

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Oct 9, 2024

PyUnicodeWriter_WriteStr() and PyUnicodeWriter_WriteRepr() now call directly _PyLong_FormatWriter() if the object is an int.

PyUnicodeWriter_WriteStr() and PyUnicodeWriter_WriteRepr() now call
directly _PyLong_FormatWriter() if the object is an int.
@vstinner
Copy link
Member Author

vstinner commented Oct 9, 2024

Benchmark using PR gh-125202 as reference:

import pyperf
runner = pyperf.Runner()

runner.bench_func('repr([1,2,3])', repr, [1, 2, 3])
list2 = [123]*10
runner.bench_func('repr(list2)', repr, list2)
list3 = [123]*50
runner.bench_func('repr(list3)', repr, list3)

Comparison without / with this change:

+----------------+---------+-----------------------+
| Benchmark      | ref     | change                |
+================+=========+=======================+
| repr([1,2,3])  | 291 ns  | 189 ns: 1.54x faster  |
+----------------+---------+-----------------------+
| repr(list2)    | 822 ns  | 498 ns: 1.65x faster  |
+----------------+---------+-----------------------+
| repr(list3)    | 3.01 us | 1.56 us: 1.93x faster |
+----------------+---------+-----------------------+
| Geometric mean | (ref)   | 1.70x faster          |
+----------------+---------+-----------------------+

@vstinner vstinner merged commit ee3167b into python:main Oct 9, 2024
37 checks passed
@vstinner vstinner deleted the writer_specialize branch October 9, 2024 22:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant