gh-148276: Optimize object creation and method calls in the JIT by resolving __init__ at trace optimization time (GH-148277)

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Ken Jin <kenjin4096@gmail.com>
This commit is contained in:
Pieter Eendebak 2026-04-11 16:22:42 +02:00 committed by GitHub
parent a71b043356
commit 1c89817f51
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 180 additions and 6 deletions

View file

@ -134,6 +134,20 @@ dummy_func(void) {
assert(!PyJitRef_IsUnique(value));
}
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);
} else {
PyTypeObject *probable_type = sym_get_probable_type(owner);
if (probable_type->tp_version_tag == type_version && sym_set_type_version(owner, type_version)) {
// Promote the probable type version to a known one.
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)probable_type);
_Py_BloomFilter_Add(dependencies, probable_type);
}
}
}
op(_STORE_ATTR_INSTANCE_VALUE, (offset/1, value, owner -- o)) {
(void)offset;
(void)value;
@ -1043,9 +1057,27 @@ dummy_func(void) {
}
op(_CHECK_AND_ALLOCATE_OBJECT, (type_version/2, callable, self_or_null, args[oparg] -- callable, self_or_null, args[oparg])) {
(void)type_version;
(void)args;
callable = sym_new_not_null(ctx);
PyObject *probable_callable = sym_get_probable_value(callable);
assert(probable_callable != NULL);
assert(PyType_Check(probable_callable));
PyTypeObject *tp = (PyTypeObject *)probable_callable;
if (tp->tp_version_tag == type_version) {
// If the type version has not changed since we last saw it,
// then we know this __init__ is definitely the same one as in the cache.
// We can promote callable to a known constant. This does not need a
// type watcher, as we do not remove this _CHECK_AND_ALLOCATE_OBJECT guard.
// TODO: split up _CHECK_AND_ALLOCATE_OBJECT to the check then alloate, so we can
// eliminate the check.
PyHeapTypeObject *cls = (PyHeapTypeObject *)probable_callable;
PyObject *init = cls->_spec_cache.init;
assert(init != NULL);
assert(PyFunction_Check(init));
callable = sym_new_const(ctx, init);
}
else {
callable = sym_new_not_null(ctx);
}
self_or_null = sym_new_not_null(ctx);
}