bpo-46072: Improve LOAD_METHOD stats (GH-31104)

This commit is contained in:
Mark Shannon 2022-02-07 14:30:34 +00:00 committed by GitHub
parent 4cce1352bb
commit 062460e8fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 3 deletions

View File

@ -503,6 +503,10 @@ initial_counter_value(void) {
#define SPEC_FAIL_BUILTIN_CLASS_METHOD 17 #define SPEC_FAIL_BUILTIN_CLASS_METHOD 17
#define SPEC_FAIL_CLASS_METHOD_OBJ 18 #define SPEC_FAIL_CLASS_METHOD_OBJ 18
#define SPEC_FAIL_OBJECT_SLOT 19 #define SPEC_FAIL_OBJECT_SLOT 19
#define SPEC_FAIL_HAS_DICT 20
#define SPEC_FAIL_HAS_MANAGED_DICT 21
#define SPEC_FAIL_INSTANCE_ATTRIBUTE 22
#define SPEC_FAIL_METACLASS_ATTRIBUTE 23
/* Binary subscr */ /* Binary subscr */
@ -973,7 +977,7 @@ load_method_fail_kind(DescriptorClassification kind)
case NON_DESCRIPTOR: case NON_DESCRIPTOR:
return SPEC_FAIL_NOT_DESCRIPTOR; return SPEC_FAIL_NOT_DESCRIPTOR;
case ABSENT: case ABSENT:
return SPEC_FAIL_EXPECTED_ERROR; return SPEC_FAIL_INSTANCE_ATTRIBUTE;
} }
Py_UNREACHABLE(); Py_UNREACHABLE();
} }
@ -994,6 +998,16 @@ specialize_class_load_method(PyObject *owner, _Py_CODEUNIT *instr, PyObject *nam
cache2->obj = descr; cache2->obj = descr;
*instr = _Py_MAKECODEUNIT(LOAD_METHOD_CLASS, _Py_OPARG(*instr)); *instr = _Py_MAKECODEUNIT(LOAD_METHOD_CLASS, _Py_OPARG(*instr));
return 0; return 0;
#ifdef Py_STATS
case ABSENT:
if (_PyType_Lookup(Py_TYPE(owner), name) != NULL) {
SPECIALIZATION_FAIL(LOAD_METHOD, SPEC_FAIL_METACLASS_ATTRIBUTE);
}
else {
SPECIALIZATION_FAIL(LOAD_METHOD, SPEC_FAIL_EXPECTED_ERROR);
}
return -1;
#endif
default: default:
SPECIALIZATION_FAIL(LOAD_METHOD, load_method_fail_kind(kind)); SPECIALIZATION_FAIL(LOAD_METHOD, load_method_fail_kind(kind));
return -1; return -1;
@ -1043,7 +1057,7 @@ _Py_Specialize_LoadMethod(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name,
if (owner_cls->tp_flags & Py_TPFLAGS_MANAGED_DICT) { if (owner_cls->tp_flags & Py_TPFLAGS_MANAGED_DICT) {
PyObject **owner_dictptr = _PyObject_ManagedDictPointer(owner); PyObject **owner_dictptr = _PyObject_ManagedDictPointer(owner);
if (*owner_dictptr) { if (*owner_dictptr) {
SPECIALIZATION_FAIL(LOAD_METHOD, SPEC_FAIL_IS_ATTR); SPECIALIZATION_FAIL(LOAD_METHOD, SPEC_FAIL_HAS_MANAGED_DICT);
goto fail; goto fail;
} }
PyDictKeysObject *keys = ((PyHeapTypeObject *)owner_cls)->ht_cached_keys; PyDictKeysObject *keys = ((PyHeapTypeObject *)owner_cls)->ht_cached_keys;
@ -1065,7 +1079,7 @@ _Py_Specialize_LoadMethod(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name,
*instr = _Py_MAKECODEUNIT(LOAD_METHOD_NO_DICT, _Py_OPARG(*instr)); *instr = _Py_MAKECODEUNIT(LOAD_METHOD_NO_DICT, _Py_OPARG(*instr));
} }
else { else {
SPECIALIZATION_FAIL(LOAD_METHOD, SPEC_FAIL_IS_ATTR); SPECIALIZATION_FAIL(LOAD_METHOD, SPEC_FAIL_HAS_DICT);
goto fail; goto fail;
} }
} }