X Tutup
Skip to content

gh-89013: Improve the performance of methodcaller (lazy version)#107201

Merged
corona10 merged 21 commits intopython:mainfrom
eendebakpt:fastmethodcaller_lazy_vectorcall
Aug 1, 2023
Merged

gh-89013: Improve the performance of methodcaller (lazy version)#107201
corona10 merged 21 commits intopython:mainfrom
eendebakpt:fastmethodcaller_lazy_vectorcall

Conversation

@eendebakpt
Copy link
Contributor

@eendebakpt eendebakpt commented Jul 24, 2023

This is a variation on #106960 where the allocation of the structures required for the vectorcall is delayed untill the first invocation of the methodcaller. The advantage is that that for methodcaller objects that are created, but never used, the creation time and amount of memory used is the same as current main.

Benchmark results:

call: Mean +- std dev: [main] 136 ns +- 1 ns -> [pr_lazy] 58.3 ns +- 1.4 ns: 2.34x faster
creation: Mean +- std dev: [main] 98.7 ns +- 3.4 ns -> [pr_lazy] 95.7 ns +- 3.2 ns: 1.03x faster
creation+call: Mean +- std dev: [main] 255 ns +- 9 ns -> [pr_lazy] 186 ns +- 6 ns: 1.37x faster
call kwarg: Mean +- std dev: [main] 217 ns +- 3 ns -> [pr_lazy] 85.9 ns +- 2.4 ns: 2.53x faster
creation kwarg: Mean +- std dev: [main] 166 ns +- 4 ns -> [pr_lazy] 170 ns +- 5 ns: 1.02x slower
creation+call kwarg: Mean +- std dev: [main] 386 ns +- 13 ns -> [pr_lazy] 339 ns +- 4 ns: 1.14x faster

Geometric mean: 1.45x faster
Benchmark script
import pyperf

setup = """
from operator import methodcaller as mc
arr = []
call = mc('sort')
call_kwarg = mc('sort', reverse=True)
"""

runner = pyperf.Runner()
runner.timeit(name="call", stmt="call(arr)", setup=setup)
runner.timeit(name="creation", stmt="call = mc('sort')", setup=setup)
runner.timeit(name="creation+call", stmt="call = mc('sort'); call(arr)", setup=setup)
runner.timeit(name="call kwarg", stmt="call_kwarg(arr)", setup=setup)
runner.timeit(name="creation kwarg", stmt="call = mc('sort', reverse=True)", setup=setup)
runner.timeit(name="creation+call kwarg", stmt="call = mc('sort', reverse=True); call(arr)", setup=setup)

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

X Tutup