Skip to content

gh-125196: Add a free list to PyUnicodeWriter - #125227

Merged
vstinner merged 2 commits into
python:mainfrom
vstinner:writer_freelist
Oct 10, 2024
Merged

vstinner merged 2 commits into
python:mainfrom
vstinner:writer_freelist

Conversation

@vstinner

@vstinner vstinner commented Oct 9, 2024 •

Copy link
Copy Markdown
Member
@vstinner

vstinner commented Oct 9, 2024

Copy link
Copy Markdown
Member Author

Benchmark:

import pyperf
runner = pyperf.Runner()

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

Results, Python built with clang -flto=thin, CPU isolation, benchmark run with --rigorous:

+----------------+---------+-----------------------+
| Benchmark      | main    | freelist              |
+================+=========+=======================+
| repr([])       | 61.9 ns | 56.3 ns: 1.10x faster | -5.6 ns
+----------------+---------+-----------------------+
| repr([1,2,3])  | 189 ns  | 179 ns: 1.06x faster  | -10 ns
+----------------+---------+-----------------------+
| repr(list3)    | 873 ns  | 859 ns: 1.02x faster  | -14 ns
+----------------+---------+-----------------------+
| repr(list4)    | 3.12 us | 3.02 us: 1.03x faster | -0.1 us
+----------------+---------+-----------------------+
| Geometric mean | (ref)   | 1.05x faster          |
+----------------+---------+-----------------------+
@vstinner

vstinner commented Oct 9, 2024

Copy link
Copy Markdown
Member Author

@pitrou: That's one way to reduce the cost of the PyUnicodeWriter_Create() memory allocation.

@vstinner

vstinner commented Oct 9, 2024

Copy link
Copy Markdown
Member Author

cc @serhiy-storchaka

I started with a free list of 1 item. We may enlarge it later for nested function calls, like repr() of a list which contains lists (which may contain lists as well, etc.).

@vstinner

Copy link
Copy Markdown
Member Author

Another benchmark on repr(tuple) from gh-125242:

+----------------+---------+-----------------------+
| Benchmark      | change  | freelist              |
+================+=========+=======================+
| repr(())       | 55.9 ns | 57.9 ns: 1.03x slower | +2 ns
+----------------+---------+-----------------------+
| repr(('abc',)) | 239 ns  | 232 ns: 1.03x faster  | -7 ns
+----------------+---------+-----------------------+
| repr((1,2,3))  | 189 ns  | 182 ns: 1.04x faster  | -7 ns
+----------------+---------+-----------------------+
| repr(tuple4)   | 870 ns  | 846 ns: 1.03x faster  | -24 ns
+----------------+---------+-----------------------+
| repr(tuple5)   | 3.02 us | 3.00 us: 1.01x faster | -20 ns
+----------------+---------+-----------------------+
| Geometric mean | (ref)   | 1.01x faster          |
+----------------+---------+-----------------------+
@vstinner
vstinner merged commit 1639d93 into python:main Oct 10, 2024
@vstinner
vstinner deleted the writer_freelist branch October 10, 2024 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

1 participant