-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use dedicated opcodes to speed up calls/attribute lookups with super() as receiver #87729
Comments
Calling methods and lookup up attributes when receiver is
One option to improve it could be to make compiler/interpreter aware of super calls so they can be treated specially. Attached patch introduces two new opcodes LOAD_METHOD_SUPER and LOAD_ATTR_SUPER that are intended to be counterparts for LOAD_METHOD and LOAD_ATTR for cases when receiver is super with either zero or two arguments. Immediate argument for both LOAD_METHOD_SUPER and LOAD_ATTR_SUPER is a pair that consist of: Both LOAD_METHOD_SUPER and LOAD_ATTR_SUPER expect 3 elements on the stack: Result of LOAD_METHOD_SUPER is the same as LOAD_METHOD. In runtime both LOAD_METHOD_SUPER and LOAD_ATTR_SUPER will check if Benchmark results with patch: |
Why? Do you have any evidence that the overhead of super() is significant in real programs, or that the proposed change actually speeds up anything beyond your micro-benchmark? |
Currently, super() is decoupled from the core language. It is just a builtin that provides customized attribute lookup. This PR makes super() more tightly integrated with the core language, treating it as if it were a keyword and part of the grammar. Also note, users can currently create their own versions of super(), shadowing the builtin super(). |
This is true however:
I do see the non-negligible cost of allocation/initialization of |
This looks like a sensible idea to me. The safeguards to ensure that customized 'super' still works seem reasonable to me. I have to admit that I sometimes refrain from using super() where I should because of the expense, so this would be welcome. I do wonder -- is two-arg super() important enough to support it at all? Maybe the code would be somewhat simpler if the special opcodes were only generated for zero-arg super() calls. |
Numbers please. What is "non-negligible cost of allocation/initialization" mean as a fraction of runtime? |
Apologies for the delay in reply: in more concrete numbers for IG codebase enabling this optimization resulted in 0.2% CPU win. |
This speeds up `super()` (by around 85%, for a simple one-level `super().meth()` microbenchmark) by avoiding allocation of a new single-use `super()` object on each use.
* main: pythongh-87729: add LOAD_SUPER_ATTR instruction for faster super() (python#103497) pythongh-103791: Make contextlib.suppress also act on exceptions within an ExceptionGroup (python#103792)
* main: pythongh-104057: Fix direct invocation of test_support (pythonGH-104069) pythongh-87729: improve hit rate of LOAD_SUPER_ATTR specialization (python#104270) pythongh-101819: Fix inverted debug preprocessor check in winconsoleio.c (python#104388)
(cherry picked from commit 7fbac51) Co-authored-by: Carl Meyer <carl@oddbird.net>
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
The text was updated successfully, but these errors were encountered: