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: Use PyUnicodeWriter for repr(dict) #125270

Merged
merged 1 commit into from
Oct 10, 2024

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Oct 10, 2024

@vstinner
Copy link
Member Author

vstinner commented Oct 10, 2024

Benchmark:

import pyperf
runner = pyperf.Runner()

runner.bench_func('repr({})', repr, {})
runner.bench_func("repr({'a': 'A'})", repr, {'a': 'A'})
runner.bench_func("repr({'a': 'A', ...})", repr, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D', 'e': 'E'})
runner.bench_func("repr({'a': 1})", repr, {'a': 1})
runner.bench_func("repr({'a': 1, ...})", repr, {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5})

Results, Python built with gcc -O3, CPU isolation, --rigorous:

+-----------------------+--------+----------------------+
| Benchmark             | ref    | change               |
+=======================+========+======================+
| repr({})              | 106 ns | 107 ns: 1.01x slower | +1 ns
+-----------------------+--------+----------------------+
| repr({'a': 'A'})      | 260 ns | 255 ns: 1.02x faster | -5 ns
+-----------------------+--------+----------------------+
| repr({'a': 'A', ...}) | 717 ns | 728 ns: 1.01x slower | +11 ns
+-----------------------+--------+----------------------+
| repr({'a': 1, ...})   | 802 ns | 755 ns: 1.06x faster | -47 ns
+-----------------------+--------+----------------------+
| Geometric mean        | (ref)  | 1.01x faster         |
+-----------------------+--------+----------------------+

Benchmark hidden because not significant (1): repr({'a': 1})

There is basically no significan effect on performance. Only repr({'a': 1, ...}) is a little bit faster thanks to the integer fast-path.

UPDATE: I reran benchmarks with --rigorous.

@vstinner vstinner merged commit bb594e8 into python:main Oct 10, 2024
39 checks passed
@vstinner vstinner deleted the writer_dict branch October 10, 2024 18:41
@vstinner
Copy link
Member Author

Merged, thanks for the review @methane.

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.

2 participants