bpo-46564: Optimize super().meth() calls via adaptive superinstructions by Fidget-Spinner · Pull Request #30992 · python/cpython (original) (raw)

Microbenchmarks show that super() has sped up by more than 2.2x. This is faster than that other attempt because there's also speedups from the LOAD_METHOD_CACHED:
(Extremely unscientific, I'm short on time to set up pyperf right now)

import timeit

setup = """ class A: def f(self): pass class B(A): def g(self): super().f() def h(self): self.f()

b = B() """

super() call

print(timeit.timeit("b.g()", setup=setup, number=20_000_000))

reference

print(timeit.timeit("b.h()", setup=setup, number=20_000_000))

Results:

# Main
5.796037399995839
2.4094066999969073

# This branch
2.4578273000006448
2.3718886000060593

So super().meth() is now only ~10% slowly than the corresponding self.meth() call whereas it was nearly 2x as slow previously. If I manage to incorporate your suggestions correctly, this will effectively just be a competition between LOAD_GLOBAL_BUILTIN (super) and LOAD_FAST (self).