Skip to content

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

Merged
vstinner merged 1 commit into
python:mainfrom
vstinner:writer_specialize
Oct 9, 2024
Merged

vstinner merged 1 commit into
python:mainfrom
vstinner:writer_specialize

Conversation

@vstinner

@vstinner vstinner commented Oct 9, 2024 •

Copy link
Copy Markdown
Member

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

vstinner commented Oct 9, 2024

Copy link
Copy Markdown
Member Author

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
@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

1 participant