Skip to content

gh-125196: Use PyUnicodeWriter for repr(dict) - #125270

Merged
vstinner merged 1 commit into
python:mainfrom
vstinner:writer_dict
Oct 10, 2024
Merged

vstinner merged 1 commit into
python:mainfrom
vstinner:writer_dict

Conversation

@vstinner

@vstinner vstinner commented Oct 10, 2024 •

Copy link
Copy Markdown
Member
@vstinner

vstinner commented Oct 10, 2024 •

Copy link
Copy Markdown
Member Author

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
@vstinner
vstinner deleted the writer_dict branch October 10, 2024 18:41
@vstinner

Copy link
Copy Markdown
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

2 participants