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
Prev Previous commit
Next Next commit
Try a fixed cache size
  • Loading branch information
brandtbucher committed Mar 23, 2023
commit bde6bec49a139ad2ea6f78790c9b9cd0889777af
3 changes: 1 addition & 2 deletions Doc/includes/typestruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ typedef struct _typeobject {
destructor tp_finalize;
vectorcallfunc tp_vectorcall;

unsigned int _tp_cache_size;
unsigned int _tp_cache_used;
PyObject **_tp_cache;
PyObject *_tp_cache[_TP_CACHE_SIZE];

/* bitset of which type-watchers care about this type */
char tp_watched;
Expand Down
5 changes: 3 additions & 2 deletions Include/cpython/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ typedef struct {
* backwards-compatibility */
typedef Py_ssize_t printfunc;

#define _TP_CACHE_SIZE (1 << 3)

// If this structure is modified, Doc/includes/typestruct.h should be updated
// as well.
struct _typeobject {
Expand Down Expand Up @@ -226,9 +228,8 @@ struct _typeobject {
destructor tp_finalize;
vectorcallfunc tp_vectorcall;

unsigned int _tp_cache_size;
unsigned int _tp_cache_used;
PyObject **_tp_cache;
PyObject *_tp_cache[_TP_CACHE_SIZE];

/* bitset of which type-watchers care about this type */
char tp_watched;
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 = 'P2nPI13Pl4Pn9Pn12PIPIIPc'
fmt = 'P2nPI13Pl4Pn9Pn12PIPI8Pc'
s = vsize('2P' + fmt)
check(int, s)
# class
Expand Down
4 changes: 0 additions & 4 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4496,10 +4496,7 @@ _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 @@ -4523,7 +4520,6 @@ 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
14 changes: 4 additions & 10 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,17 +489,11 @@ insert_into_cache(PyTypeObject *type, PyObject *descr)
return i;
}
}
if (type->_tp_cache_used == type->_tp_cache_size) {
type->_tp_cache_size = Py_MAX(type->_tp_cache_size << 1, 8);
PyMem_Resize(type->_tp_cache, PyObject *, type->_tp_cache_size);
if (type->_tp_cache == NULL) {
type->_tp_cache_size = 0;
type->_tp_cache_used = 0;
return -1;
}
if (type->_tp_cache_used < _TP_CACHE_SIZE) {
type->_tp_cache[type->_tp_cache_used] = descr;
return type->_tp_cache_used++;
}
type->_tp_cache[type->_tp_cache_used] = descr;
return type->_tp_cache_used++;
return -1;
}

static int
Expand Down