gh-148276: Optimize object creation and method calls in the JIT by resolving __init__ at trace optimization time#148277
gh-148276: Optimize object creation and method calls in the JIT by resolving __init__ at trace optimization time#148277eendebakpt wants to merge 4 commits intopython:mainfrom
Conversation
…nt type guards - _CHECK_AND_ALLOCATE_OBJECT: resolve __init__ from type's _spec_cache so the optimizer can follow into __init__ bodies - _GUARD_TYPE_VERSION_LOCKED: add optimizer handler to track type version and NOP redundant guards on the same object - Add test_guard_type_version_locked_removed Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Fidget-Spinner
left a comment
There was a problem hiding this comment.
LGTM, just two comments on wording.
Lib/test/test_capi/test_opt.py
Outdated
| enabling the optimizer to trace into the init frame and eliminate | ||
| redundant function version and arg count checks. |
There was a problem hiding this comment.
This has nothing to do with tracing into the init frame. We already do that. it's more of propagating information through the frame
| op(_GUARD_TYPE_VERSION_LOCKED, (type_version/2, owner -- owner)) { | ||
| assert(type_version); | ||
| if (sym_matches_type_version(owner, type_version)) { | ||
| ADD_OP(_NOP, 0, 0); |
There was a problem hiding this comment.
We should not be removing this as we are moving towards FT compatibility. This uop unlocks objects on FT as well, so we need to keep it around as it's side effecting.
Instead, you should break out the _GUARD_TYPE_VERSION_LOCKED into _GUARD_TYPE_VERSION + UNLOCK. See for example the _LOCK_OBJECT op.
There was a problem hiding this comment.
I do not fully understand. The unlock only happens when the type version doesn't match. If that cannot happen, there is no need to keep the unlock part or is there?
There was a problem hiding this comment.
I'm renaming the opcode references from _GUARD_TYPE_VERSION_LOCKED to _GUARD_TYPE_VERSION but I'm not sure how to add the unlock part. I would like some help with that part.
There was a problem hiding this comment.
I do not fully understand. The unlock only happens when the type version doesn't match. If that cannot happen, there is no need to keep the unlock part or is there?
There still is. Some op previously might have LOCK_OBJECT this uop (it probably did). So you still need the matching UNLOCK_OBJECT in FT.
Co-authored-by: Ken Jin <kenjin4096@gmail.com>
|
@MazinSharaf could you please not try to overtake someone else's PR without their permission? I'm going to hide your comments as they're disruptive and not very helpful. Pieter can do the rename themself just fine. |
Optimize object creation and method calls in the JIT by resolving
__init__at trace compile time and eliminating redundant type guards. The idea was picked up when experimenting with the ideas in #144388 using Claude Code.Changes
_CHECK_AND_ALLOCATE_OBJECT: resolve the__init__function to a constant via_spec_cache.init, allowing the optimizer to eliminate_CHECK_FUNCTION_VERSIONand_CHECK_FUNCTION_EXACT_ARGSfor the init call_GUARD_TYPE_VERSION_LOCKED: propagate type version info so repeated guards on the same type within a trace are NOPedBenchmark (release JIT, x86_64) on
Point(x, y)p.translate().dist()v.scale().add().dot()Object creation + method chains are 1.2-1.3x faster. Simple method calls and descriptors are unchanged.
Details
<.summary>__init__at trace compile time #148276