GH-89987: Shrink the BINARY_SUBSCR caches (GH-103022)

This commit is contained in:
Brandt Bucher 2023-03-29 15:53:30 -07:00 committed by GitHub
parent e647dbaded
commit 121057aa36
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 272 additions and 250 deletions

View file

@ -292,7 +292,7 @@ dummy_func(
BINARY_SUBSCR_TUPLE_INT,
};
inst(BINARY_SUBSCR, (unused/4, container, sub -- res)) {
inst(BINARY_SUBSCR, (unused/1, container, sub -- res)) {
#if ENABLE_SPECIALIZATION
_PyBinarySubscrCache *cache = (_PyBinarySubscrCache *)next_instr;
if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) {
@ -339,7 +339,7 @@ dummy_func(
ERROR_IF(err, error);
}
inst(BINARY_SUBSCR_LIST_INT, (unused/4, list, sub -- res)) {
inst(BINARY_SUBSCR_LIST_INT, (unused/1, list, sub -- res)) {
assert(cframe.use_tracing == 0);
DEOPT_IF(!PyLong_CheckExact(sub), BINARY_SUBSCR);
DEOPT_IF(!PyList_CheckExact(list), BINARY_SUBSCR);
@ -356,7 +356,7 @@ dummy_func(
Py_DECREF(list);
}
inst(BINARY_SUBSCR_TUPLE_INT, (unused/4, tuple, sub -- res)) {
inst(BINARY_SUBSCR_TUPLE_INT, (unused/1, tuple, sub -- res)) {
assert(cframe.use_tracing == 0);
DEOPT_IF(!PyLong_CheckExact(sub), BINARY_SUBSCR);
DEOPT_IF(!PyTuple_CheckExact(tuple), BINARY_SUBSCR);
@ -373,7 +373,7 @@ dummy_func(
Py_DECREF(tuple);
}
inst(BINARY_SUBSCR_DICT, (unused/4, dict, sub -- res)) {
inst(BINARY_SUBSCR_DICT, (unused/1, dict, sub -- res)) {
assert(cframe.use_tracing == 0);
DEOPT_IF(!PyDict_CheckExact(dict), BINARY_SUBSCR);
STAT_INC(BINARY_SUBSCR, hit);
@ -389,14 +389,16 @@ dummy_func(
DECREF_INPUTS();
}
inst(BINARY_SUBSCR_GETITEM, (unused/1, type_version/2, func_version/1, container, sub -- unused)) {
inst(BINARY_SUBSCR_GETITEM, (unused/1, container, sub -- unused)) {
PyTypeObject *tp = Py_TYPE(container);
DEOPT_IF(tp->tp_version_tag != type_version, BINARY_SUBSCR);
assert(tp->tp_flags & Py_TPFLAGS_HEAPTYPE);
PyObject *cached = ((PyHeapTypeObject *)tp)->_spec_cache.getitem;
DEOPT_IF(!PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE), BINARY_SUBSCR);
PyHeapTypeObject *ht = (PyHeapTypeObject *)tp;
PyObject *cached = ht->_spec_cache.getitem;
DEOPT_IF(cached == NULL, BINARY_SUBSCR);
assert(PyFunction_Check(cached));
PyFunctionObject *getitem = (PyFunctionObject *)cached;
DEOPT_IF(getitem->func_version != func_version, BINARY_SUBSCR);
uint32_t cached_version = ht->_spec_cache.getitem_version;
DEOPT_IF(getitem->func_version != cached_version, BINARY_SUBSCR);
PyCodeObject *code = (PyCodeObject *)getitem->func_code;
assert(code->co_argcount == 2);
DEOPT_IF(!_PyThreadState_HasStackSpace(tstate, code->co_framesize), BINARY_SUBSCR);

File diff suppressed because it is too large Load diff

View file

@ -731,13 +731,13 @@ const struct opcode_metadata _PyOpcode_opcode_metadata[256] = {
[BINARY_OP_INPLACE_ADD_UNICODE] = { true, INSTR_FMT_IX },
[BINARY_OP_ADD_FLOAT] = { true, INSTR_FMT_IXC },
[BINARY_OP_ADD_INT] = { true, INSTR_FMT_IXC },
[BINARY_SUBSCR] = { true, INSTR_FMT_IXC000 },
[BINARY_SUBSCR] = { true, INSTR_FMT_IXC },
[BINARY_SLICE] = { true, INSTR_FMT_IX },
[STORE_SLICE] = { true, INSTR_FMT_IX },
[BINARY_SUBSCR_LIST_INT] = { true, INSTR_FMT_IXC000 },
[BINARY_SUBSCR_TUPLE_INT] = { true, INSTR_FMT_IXC000 },
[BINARY_SUBSCR_DICT] = { true, INSTR_FMT_IXC000 },
[BINARY_SUBSCR_GETITEM] = { true, INSTR_FMT_IXC000 },
[BINARY_SUBSCR_LIST_INT] = { true, INSTR_FMT_IXC },
[BINARY_SUBSCR_TUPLE_INT] = { true, INSTR_FMT_IXC },
[BINARY_SUBSCR_DICT] = { true, INSTR_FMT_IXC },
[BINARY_SUBSCR_GETITEM] = { true, INSTR_FMT_IXC },
[LIST_APPEND] = { true, INSTR_FMT_IB },
[SET_ADD] = { true, INSTR_FMT_IB },
[STORE_SUBSCR] = { true, INSTR_FMT_IXC },

View file

@ -1330,16 +1330,16 @@ _Py_Specialize_BinarySubscr(
SPECIALIZATION_FAIL(BINARY_SUBSCR, SPEC_FAIL_WRONG_NUMBER_ARGUMENTS);
goto fail;
}
assert(cls->tp_version_tag != 0);
write_u32(cache->type_version, cls->tp_version_tag);
int version = _PyFunction_GetVersionForCurrentState(func);
if (version == 0 || version != (uint16_t)version) {
SPECIALIZATION_FAIL(BINARY_SUBSCR, version == 0 ?
SPEC_FAIL_OUT_OF_VERSIONS : SPEC_FAIL_OUT_OF_RANGE);
uint32_t version = _PyFunction_GetVersionForCurrentState(func);
if (version == 0) {
SPECIALIZATION_FAIL(BINARY_SUBSCR, SPEC_FAIL_OUT_OF_VERSIONS);
goto fail;
}
cache->func_version = version;
((PyHeapTypeObject *)container_type)->_spec_cache.getitem = descriptor;
PyHeapTypeObject *ht = (PyHeapTypeObject *)container_type;
// This pointer is invalidated by PyType_Modified (see the comment on
// struct _specialization_cache):
ht->_spec_cache.getitem = descriptor;
ht->_spec_cache.getitem_version = version;
instr->op.code = BINARY_SUBSCR_GETITEM;
goto success;
}