Skip to content

Conversation

@vstinner
Copy link
Member

@vstinnervstinner commented Oct 9, 2024

@vstinner
Copy link
MemberAuthor

Benchmark:

importpyperfrunner=pyperf.Runner() runner.bench_func('repr([])', repr, []) runner.bench_func('repr([1,2,3])', repr, [1, 2, 3]) list3= ['abcdef']*10runner.bench_func('repr(list3)', repr, list3) list4= ['abcdef']*50runner.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
MemberAuthor

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

@vstinner
Copy link
MemberAuthor

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
MemberAuthor

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

@vstinnervstinner merged commit 1639d93 into python:mainOct 10, 2024
@vstinnervstinner deleted the writer_freelist branch October 10, 2024 10:11
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@vstinner