Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Stick a small, resizable cache on all types
  • Loading branch information
brandtbucher committed Mar 23, 2023
commit c87e8bd0f377c049f9f8448b2f047cf4798dc6df
4 changes: 4 additions & 0 deletions Doc/includes/typestruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ typedef struct _typeobject {
destructor tp_finalize;
vectorcallfunc tp_vectorcall;

unsigned int _tp_cache_size;
unsigned int _tp_cache_used;
PyObject **_tp_cache;

/* bitset of which type-watchers care about this type */
char tp_watched;
} PyTypeObject;
4 changes: 4 additions & 0 deletions Include/cpython/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ struct _typeobject {
destructor tp_finalize;
vectorcallfunc tp_vectorcall;

unsigned int _tp_cache_size;
unsigned int _tp_cache_used;
PyObject **_tp_cache;

/* 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.

3 changes: 2 additions & 1 deletion Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ def _write_atomic(path, data, mode=0o666):
# 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 (Shrink the LOAD_ATTR caches))

# Python 3.13 will start with 3550

Expand All @@ -452,7 +453,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 = (3522).to_bytes(2, 'little') + b'\r\n'
MAGIC_NUMBER = (3523).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 @@ -407,7 +407,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 = 'P2nPI13Pl4Pn9Pn12PIPIIPc'
s = vsize('2P' + fmt)
check(int, s)
# class
Expand Down
8 changes: 8 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,10 @@ _PyStaticType_Dealloc(PyTypeObject *type)
static_builtin_state_clear(type);
/* We leave _Py_TPFLAGS_STATIC_BUILTIN set on tp_flags. */
}
type->_tp_cache_size = 0;
type->_tp_cache_used = 0;
PyMem_Free(type->_tp_cache);
type->_tp_cache = NULL;
}


Expand All @@ -4516,6 +4523,7 @@ type_dealloc(PyTypeObject *type)
Py_XDECREF(type->tp_mro);
Py_XDECREF(type->tp_cache);
clear_subclasses(type);
PyMem_Free(type->_tp_cache);

/* A type's tp_doc is heap allocated, unlike the tp_doc slots
* of most other objects. It's okay to cast it to char *.
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 @@ -2239,7 +2241,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 @@ -2252,27 +2254,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 @@ -2282,6 +2286,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