gh-148276: Optimize object creation and method calls in the JIT by resolving __init__ at trace optimization time#148277
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
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. |
Python/optimizer_bytecodes.c
Outdated
| (void)args; | ||
| callable = sym_new_not_null(ctx); | ||
| PyTypeObject *type = _PyType_LookupByVersion(type_version); |
There was a problem hiding this comment.
We want to reduce usage of this as it's going to be slow to make thread-safe on FT. Also it's quite fragile and redundant, considering we can already record types in the JIT tracer. I will push a change to use recorded values instead of reverse-lookup ones here.
Lib/test/test_capi/test_opt.py
Outdated
| # __init__ resolution eliminates function version and arg checks | ||
| self.assertNotIn("_CHECK_FUNCTION_VERSION", uops) | ||
| self.assertNotIn("_CHECK_FUNCTION_EXACT_ARGS", uops) |
There was a problem hiding this comment.
@eendebakpt I think the LLM generated this, but this is wrong, there is no function being called in the __init__, so this won't show up.
Fidget-Spinner
left a comment
There was a problem hiding this comment.
Thanks a lot for doing this @eendebakpt ! It's a really awesome optimization.
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