Skip to content

gh-xxxxx: Add type-local type cache #130135

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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 Include/cpython/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ struct _specialization_cache {
PyObject *getitem;
uint32_t getitem_version;
PyObject *init;
#ifdef Py_GIL_DISABLED
struct local_type_cache *local_type_cache;
#endif
};

/* The *real* layout of a type object when allocated on the heap */
Expand Down
28 changes: 28 additions & 0 deletions Include/internal/pycore_typeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,31 @@ struct type_cache {
struct type_cache_entry hashtable[1 << MCACHE_SIZE_EXP];
};

#ifdef Py_GIL_DISABLED

// Type attribute lookup cache which is type-specific. Only used
// for heap types where we store a small additional cache in free-threaded
// builds which can be accessed without any locking.
#define LOCAL_TYPE_CACHE_SIZE 64
#define LOCAL_TYPE_CACHE_MAX_ENTRIES 48
#define LOCAL_TYPE_CACHE_PROBE 3

struct local_type_cache_entry {
PyObject *name; // reference to exactly a str or NULL
PyObject *value; // owned reference or NULL
};

struct local_type_cache {
unsigned int tp_version_tag;
unsigned int cache_count;
struct local_type_cache_entry entries[LOCAL_TYPE_CACHE_SIZE];
#if 0
int hits[LOCAL_TYPE_CACHE_SIZE], probes[LOCAL_TYPE_CACHE_SIZE], miss[LOCAL_TYPE_CACHE_SIZE];
#endif
};

#endif

typedef struct {
PyTypeObject *type;
int isbuiltin;
Expand All @@ -85,6 +110,9 @@ typedef struct {
are also some diagnostic uses for the list of weakrefs,
so we still keep it. */
PyObject *tp_weaklist;
#ifdef Py_GIL_DISABLED
struct local_type_cache local_cache;
#endif
} managed_static_type_state;

#define TYPE_VERSION_CACHE_SIZE (1<<12) /* Must be a power of 2 */
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -1731,6 +1731,7 @@ def delx(self): del self.__x
s = vsize(fmt)
check(int, s)
typeid = 'n' if support.Py_GIL_DISABLED else ''
local_type_cache = 'P' if support.Py_GIL_DISABLED else ''
# class
s = vsize(fmt + # PyTypeObject
'4P' # PyAsyncMethods
Expand All @@ -1741,6 +1742,7 @@ def delx(self): del self.__x
'7P'
'1PIP' # Specializer cache
+ typeid # heap type id (free-threaded only)
+ local_type_cache # local type cache (free-threaded only)
)
class newstyleclass(object): pass
# Separate block for PyDictKeysObject with 8 keys and 5 entries
Expand Down
Loading
Loading