Skip to content

Commit fa7ed79

Browse files
committed
Inline the per-type method cache lookup on the default build
pythongh-145685 (per-type method cache) moved the type attribute cache lookup into an out-of-line function returning a struct, on the hot path of _PyType_LookupStackRefAndVersion. With the GIL no lock-free reader protocol is needed, so probe heap types' caches inline. Static builtin types and the free-threaded build keep using _PyTypeCache_Lookup().
1 parent 6af40a6 commit fa7ed79

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

‎Include/internal/pycore_typecache.h‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,39 @@ struct _PyTypeCacheLookupResult {
3636
};
3737

3838

39+
#ifndef Py_GIL_DISABLED
40+
/* Inline lookup for the default (GIL) build, used on the hot path in
41+
* _PyType_LookupStackRefAndVersion(). With the GIL no lock-free reader
42+
* protocol is needed, so this is the same probe as _PyTypeCache_Lookup()
43+
* without the out-of-line call and struct return. */
44+
static inline struct _PyTypeCacheLookupResult
45+
_PyTypeCache_LookupInCache(struct type_cache *cache, PyTypeObject *type, PyObject *name)
46+
{
47+
struct _PyTypeCacheLookupResult miss = {PyStackRef_NULL, 0, 0};
48+
if (cache == NULL) {
49+
return miss;
50+
}
51+
Py_hash_t hash = PyUnstable_Unicode_GET_CACHED_HASH(name);
52+
uint32_t index = (uint32_t)hash & cache->mask;
53+
for (;;) {
54+
PyObject *entry_name = cache->hashtable[index].name;
55+
if (entry_name == name) {
56+
break;
57+
}
58+
if (entry_name == NULL) {
59+
return miss;
60+
}
61+
index = (index + 1) & cache->mask;
62+
}
63+
if (cache->version_tag != type->tp_version_tag) {
64+
return miss;
65+
}
66+
PyObject *v = cache->hashtable[index].value;
67+
_PyStackRef out_ref = v ? PyStackRef_FromPyObjectNew(v) : PyStackRef_NULL;
68+
return (struct _PyTypeCacheLookupResult){out_ref, 1, cache->version_tag};
69+
}
70+
#endif
71+
3972
extern void _PyTypeCache_InitType(PyTypeObject *type);
4073
extern void _PyTypeCache_Insert(PyTypeObject *type, PyObject *name, PyObject *value);
4174
PyAPI_FUNC(struct _PyTypeCacheLookupResult) _PyTypeCache_Lookup(PyTypeObject *type, PyObject *name);

‎Objects/typeobject.c‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6215,7 +6215,16 @@ _PyType_LookupStackRefAndVersion(PyTypeObject *type, PyObject *name, _PyStackRef
62156215
{
62166216
int cacheable = MCACHE_CACHEABLE_NAME(name);
62176217
if (cacheable) {
6218-
struct _PyTypeCacheLookupResult r = _PyTypeCache_Lookup(type, name);
6218+
struct _PyTypeCacheLookupResult r;
6219+
#ifndef Py_GIL_DISABLED
6220+
if (!(type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN)) {
6221+
r = _PyTypeCache_LookupInCache((struct type_cache *)type->_tp_cache, type, name);
6222+
}
6223+
else
6224+
#endif
6225+
{
6226+
r = _PyTypeCache_Lookup(type, name);
6227+
}
62196228
if (r.cache_hit) {
62206229
OBJECT_STAT_INC_COND(type_cache_hits, !is_dunder_name(name));
62216230
OBJECT_STAT_INC_COND(type_cache_dunder_hits, is_dunder_name(name));

0 commit comments

Comments
 (0)