Skip to content

GH-93533: Shrink the LOAD_ATTR caches #103014

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Doc/includes/typestruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ typedef struct _typeobject {
destructor tp_finalize;
vectorcallfunc tp_vectorcall;

unsigned int _tp_cache_used;
PyObject *_tp_cache[_TP_CACHE_SIZE];

/* bitset of which type-watchers care about this type */
char tp_watched;
} PyTypeObject;
5 changes: 5 additions & 0 deletions Include/cpython/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ typedef struct {
* backwards-compatibility */
typedef Py_ssize_t printfunc;

#define _TP_CACHE_SIZE (1 << 4)

// If this structure is modified, Doc/includes/typestruct.h should be updated
// as well.
struct _typeobject {
Expand Down Expand Up @@ -226,6 +228,9 @@ struct _typeobject {
destructor tp_finalize;
vectorcallfunc tp_vectorcall;

unsigned int _tp_cache_used;
PyObject *_tp_cache[_TP_CACHE_SIZE];

/* bitset of which type-watchers care about this type */
char tp_watched;
};
Expand Down
2 changes: 1 addition & 1 deletion Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ typedef struct {
uint16_t counter;
uint16_t type_version[2];
uint16_t keys_version[2];
uint16_t descr[4];
uint16_t index;
} _PyLoadMethodCache;


Expand Down
2 changes: 1 addition & 1 deletion Include/internal/pycore_opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,9 @@ def _write_atomic(path, data, mode=0o666):
# Python 3.12a6 3519 (Modify SEND instruction)
# Python 3.12a6 3520 (Remove PREP_RERAISE_STAR, add CALL_INTRINSIC_2)
# Python 3.12a7 3521 (Shrink the LOAD_GLOBAL caches)
# Python 3.12a7 3522 (Removed JUMP_IF_FALSE_OR_POP/JUMP_IF_TRUE_OR_POP)
# Python 3.12a7 3523 (Convert COMPARE_AND_BRANCH back to COMPARE_OP)
# Python 3.12a7 3524 (Shrink the LOAD_ATTR caches)

# Python 3.13 will start with 3550

Expand All @@ -452,7 +454,7 @@ def _write_atomic(path, data, mode=0o666):
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
# in PC/launcher.c must also be updated.

MAGIC_NUMBER = (3523).to_bytes(2, 'little') + b'\r\n'
MAGIC_NUMBER = (3524).to_bytes(2, 'little') + b'\r\n'

_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c

Expand Down
2 changes: 1 addition & 1 deletion Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ def pseudo_op(name, op, real_ops):
"counter": 1,
"version": 2,
"keys_version": 2,
"descr": 4,
"index": 1,
},
"STORE_ATTR": {
"counter": 1,
Expand Down
8 changes: 4 additions & 4 deletions Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def bug42562():

%3d LOAD_GLOBAL 0 (Exception)
CHECK_EXC_MATCH
POP_JUMP_IF_FALSE 23 (to 80)
POP_JUMP_IF_FALSE 20 (to 74)
STORE_FAST 0 (e)

%3d LOAD_FAST 0 (e)
Expand Down Expand Up @@ -1128,7 +1128,7 @@ def test_load_attr_specialize(self):

1 2 LOAD_CONST 0 ('a')
4 LOAD_ATTR_SLOT 0 (__class__)
24 RETURN_VALUE
18 RETURN_VALUE
"""
co = compile("'a'.__class__", "", "eval")
self.code_quicken(lambda: exec(co, {}, {}))
Expand Down Expand Up @@ -1193,8 +1193,8 @@ def test_show_caches(self):
caches = list(self.get_cached_values(quickened, adaptive))
for cache in caches:
self.assertRegex(cache, pattern)
total_caches = 22
empty_caches = 7
total_caches = 19
empty_caches = 4
self.assertEqual(caches.count(""), empty_caches)
self.assertEqual(len(caches), total_caches)

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -1545,7 +1545,7 @@ def delx(self): del self.__x
check((1,2,3), vsize('') + 3*self.P)
# type
# static type: PyTypeObject
fmt = 'P2nPI13Pl4Pn9Pn12PIPc'
fmt = 'P2nPI13Pl4Pn9Pn12PIPI16Pc'
s = vsize('2P' + fmt)
check(int, s)
# class
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Reduce the number of inline :opcode:`CACHE` entries for :opcode:`LOAD_ATTR`
instructions by adding a shared array of cached methods to the
:c:type:`PyTypeObject` struct.
4 changes: 4 additions & 0 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ PyType_Modified(PyTypeObject *type)

type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;
type->tp_version_tag = 0; /* 0 is not a valid version tag */
type->_tp_cache_used = 0;
}

static void
Expand Down Expand Up @@ -563,6 +564,7 @@ type_mro_modified(PyTypeObject *type, PyObject *bases) {
clear:
type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;
type->tp_version_tag = 0; /* 0 is not a valid version tag */
type->_tp_cache_used = 0;
}

static int
Expand All @@ -586,6 +588,7 @@ assign_version_tag(PyTypeObject *type)
}
type->tp_version_tag = next_version_tag++;
assert (type->tp_version_tag != 0);
assert(type->_tp_cache_used == 0);

PyObject *bases = type->tp_bases;
Py_ssize_t n = PyTuple_GET_SIZE(bases);
Expand Down Expand Up @@ -4493,6 +4496,7 @@ _PyStaticType_Dealloc(PyTypeObject *type)
static_builtin_state_clear(type);
/* We leave _Py_TPFLAGS_STATIC_BUILTIN set on tp_flags. */
}
type->_tp_cache_used = 0;
}


Expand Down
63 changes: 31 additions & 32 deletions Programs/test_frozenmain.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 17 additions & 12 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1413,7 +1413,7 @@ dummy_func(
LOAD_ATTR_METHOD_LAZY_DICT,
};

inst(LOAD_ATTR, (unused/9, owner -- res2 if (oparg & 1), res)) {
inst(LOAD_ATTR, (unused/6, owner -- res2 if (oparg & 1), res)) {
#if ENABLE_SPECIALIZATION
_PyAttrCache *cache = (_PyAttrCache *)next_instr;
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
Expand Down Expand Up @@ -1462,7 +1462,7 @@ dummy_func(
}
}

inst(LOAD_ATTR_INSTANCE_VALUE, (unused/1, type_version/2, index/1, unused/5, owner -- res2 if (oparg & 1), res)) {
inst(LOAD_ATTR_INSTANCE_VALUE, (unused/1, type_version/2, index/1, unused/2, owner -- res2 if (oparg & 1), res)) {
assert(cframe.use_tracing == 0);
PyTypeObject *tp = Py_TYPE(owner);
assert(type_version != 0);
Expand All @@ -1479,7 +1479,7 @@ dummy_func(
DECREF_INPUTS();
}

inst(LOAD_ATTR_MODULE, (unused/1, type_version/2, index/1, unused/5, owner -- res2 if (oparg & 1), res)) {
inst(LOAD_ATTR_MODULE, (unused/1, type_version/2, index/1, unused/2, owner -- res2 if (oparg & 1), res)) {
assert(cframe.use_tracing == 0);
DEOPT_IF(!PyModule_CheckExact(owner), LOAD_ATTR);
PyDictObject *dict = (PyDictObject *)((PyModuleObject *)owner)->md_dict;
Expand All @@ -1496,7 +1496,7 @@ dummy_func(
DECREF_INPUTS();
}

inst(LOAD_ATTR_WITH_HINT, (unused/1, type_version/2, index/1, unused/5, owner -- res2 if (oparg & 1), res)) {
inst(LOAD_ATTR_WITH_HINT, (unused/1, type_version/2, index/1, unused/2, owner -- res2 if (oparg & 1), res)) {
assert(cframe.use_tracing == 0);
PyTypeObject *tp = Py_TYPE(owner);
assert(type_version != 0);
Expand Down Expand Up @@ -1527,7 +1527,7 @@ dummy_func(
DECREF_INPUTS();
}

inst(LOAD_ATTR_SLOT, (unused/1, type_version/2, index/1, unused/5, owner -- res2 if (oparg & 1), res)) {
inst(LOAD_ATTR_SLOT, (unused/1, type_version/2, index/1, unused/2, owner -- res2 if (oparg & 1), res)) {
assert(cframe.use_tracing == 0);
PyTypeObject *tp = Py_TYPE(owner);
assert(type_version != 0);
Expand All @@ -1541,7 +1541,7 @@ dummy_func(
DECREF_INPUTS();
}

inst(LOAD_ATTR_CLASS, (unused/1, type_version/2, unused/2, descr/4, cls -- res2 if (oparg & 1), res)) {
inst(LOAD_ATTR_CLASS, (unused/1, type_version/2, unused/2, index/1, cls -- res2 if (oparg & 1), res)) {
assert(cframe.use_tracing == 0);

DEOPT_IF(!PyType_Check(cls), LOAD_ATTR);
Expand All @@ -1551,19 +1551,20 @@ dummy_func(

STAT_INC(LOAD_ATTR, hit);
res2 = NULL;
res = descr;
res = ((PyTypeObject *)cls)->_tp_cache[index];
assert(res != NULL);
Py_INCREF(res);
DECREF_INPUTS();
}

inst(LOAD_ATTR_PROPERTY, (unused/1, type_version/2, func_version/2, fget/4, owner -- unused if (oparg & 1), unused)) {
inst(LOAD_ATTR_PROPERTY, (unused/1, type_version/2, func_version/2, index/1, owner -- unused if (oparg & 1), unused)) {
assert(cframe.use_tracing == 0);
DEOPT_IF(tstate->interp->eval_frame, LOAD_ATTR);

PyTypeObject *cls = Py_TYPE(owner);
DEOPT_IF(cls->tp_version_tag != type_version, LOAD_ATTR);
assert(type_version != 0);
PyObject *fget = cls->_tp_cache[index];
assert(Py_IS_TYPE(fget, &PyFunction_Type));
PyFunctionObject *f = (PyFunctionObject *)fget;
assert(func_version != 0);
Expand All @@ -1583,12 +1584,13 @@ dummy_func(
DISPATCH_INLINED(new_frame);
}

inst(LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN, (unused/1, type_version/2, func_version/2, getattribute/4, owner -- unused if (oparg & 1), unused)) {
inst(LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN, (unused/1, type_version/2, func_version/2, index/1, owner -- unused if (oparg & 1), unused)) {
assert(cframe.use_tracing == 0);
DEOPT_IF(tstate->interp->eval_frame, LOAD_ATTR);
PyTypeObject *cls = Py_TYPE(owner);
DEOPT_IF(cls->tp_version_tag != type_version, LOAD_ATTR);
assert(type_version != 0);
PyObject *getattribute = cls->_tp_cache[index];
assert(Py_IS_TYPE(getattribute, &PyFunction_Type));
PyFunctionObject *f = (PyFunctionObject *)getattribute;
assert(func_version != 0);
Expand Down Expand Up @@ -2214,7 +2216,7 @@ dummy_func(
exc_info->exc_value = Py_NewRef(new_exc);
}

inst(LOAD_ATTR_METHOD_WITH_VALUES, (unused/1, type_version/2, keys_version/2, descr/4, self -- res2 if (oparg & 1), res)) {
inst(LOAD_ATTR_METHOD_WITH_VALUES, (unused/1, type_version/2, keys_version/2, index/1, self -- res2 if (oparg & 1), res)) {
/* Cached method object */
assert(cframe.use_tracing == 0);
PyTypeObject *self_cls = Py_TYPE(self);
Expand All @@ -2227,27 +2229,29 @@ dummy_func(
DEOPT_IF(self_heap_type->ht_cached_keys->dk_version !=
keys_version, LOAD_ATTR);
STAT_INC(LOAD_ATTR, hit);
PyObject *descr = self_cls->_tp_cache[index];
assert(descr != NULL);
res2 = Py_NewRef(descr);
assert(_PyType_HasFeature(Py_TYPE(res2), Py_TPFLAGS_METHOD_DESCRIPTOR));
res = self;
assert(oparg & 1);
}

inst(LOAD_ATTR_METHOD_NO_DICT, (unused/1, type_version/2, unused/2, descr/4, self -- res2 if (oparg & 1), res)) {
inst(LOAD_ATTR_METHOD_NO_DICT, (unused/1, type_version/2, unused/2, index/1, self -- res2 if (oparg & 1), res)) {
assert(cframe.use_tracing == 0);
PyTypeObject *self_cls = Py_TYPE(self);
DEOPT_IF(self_cls->tp_version_tag != type_version, LOAD_ATTR);
assert(self_cls->tp_dictoffset == 0);
STAT_INC(LOAD_ATTR, hit);
PyObject *descr = self_cls->_tp_cache[index];
assert(descr != NULL);
assert(_PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR));
res2 = Py_NewRef(descr);
res = self;
assert(oparg & 1);
}

inst(LOAD_ATTR_METHOD_LAZY_DICT, (unused/1, type_version/2, unused/2, descr/4, self -- res2 if (oparg & 1), res)) {
inst(LOAD_ATTR_METHOD_LAZY_DICT, (unused/1, type_version/2, unused/2, index/1, self -- res2 if (oparg & 1), res)) {
assert(cframe.use_tracing == 0);
PyTypeObject *self_cls = Py_TYPE(self);
DEOPT_IF(self_cls->tp_version_tag != type_version, LOAD_ATTR);
Expand All @@ -2257,6 +2261,7 @@ dummy_func(
/* This object has a __dict__, just not yet created */
DEOPT_IF(dict != NULL, LOAD_ATTR);
STAT_INC(LOAD_ATTR, hit);
PyObject *descr = self_cls->_tp_cache[index];
assert(descr != NULL);
assert(_PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR));
res2 = Py_NewRef(descr);
Expand Down
Loading