Skip to content
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

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

Merged
merged 2 commits into from
Oct 10, 2024

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Oct 9, 2024

@vstinner
Copy link
Member Author

vstinner commented Oct 9, 2024

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
Copy link
Member Author

vstinner commented Oct 9, 2024

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

@vstinner
Copy link
Member Author

vstinner commented Oct 9, 2024

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
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
34 checks passed
@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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant