We improve the performance of methodcaller by using the vector protocol. The performance improvement depends on the use case, but for simple calls the performance doubles. Creation is a bit slower (because the vector call arguments have to be constructed), but the net gain is positive already after a single invocation of the methodcaller
Note: this is a continuation of #27782. Changes with respect to that PR
The methodcaller_call was removed in favor of the vector call, but it is still needed for some builds. It is now restored, but less efficient
Store the full positional arguments as xargs instead of of a slice to avoid creation of an additional tuple
Additional storage in the methodcallerobject compared to main is now 2 variables PyObject* and a tuple holding the keyword argument names.
To avoid construction of the vector call arguments during the definition of the methodcaller, one can also defer the construction to the first invocation of the methodcaller. A branch with this approach is main...eendebakpt:cpython:fastmethodcaller_lazy_vectorcall
eendebakpt
changed the title
Draft: gh-27782: Improve the performance of methodcaller
Draft: gh-89013: Improve the performance of methodcaller
Jul 21, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We improve the performance of methodcaller by using the vector protocol. The performance improvement depends on the use case, but for simple calls the performance doubles. Creation is a bit slower (because the vector call arguments have to be constructed), but the net gain is positive already after a single invocation of the methodcaller
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+call", stmt="call = mc('sort'); call(arr)", setup=setup) runner.timeit(name="call kwarg", stmt="call_kwarg(arr)", setup=setup) runner.timeit(name="creation+call kwarg", stmt="call = mc('sort', reverse=True); call(arr)", setup=setup)Note: this is a continuation of #27782. Changes with respect to that PR