Skip to content

Commit 57ba52d

Browse files
committed
Document _PyType_CacheInitForSpecialization
1 parent 4c1ad6c commit 57ba52d

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

Include/internal/pycore_object.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -822,8 +822,18 @@ extern bool _PyObject_TryGetInstanceAttribute(PyObject *obj, PyObject *name,
822822
PyObject **attr);
823823
extern PyObject *_PyType_LookupRefAndVersion(PyTypeObject *, PyObject *,
824824
unsigned int *);
825-
extern int _PyType_CacheInitForSpecialization(PyTypeObject *, PyObject *,
826-
unsigned int);
825+
826+
// Cache the provided init method in the specialization cache of type if the
827+
// provided type version matches the current version of the type.
828+
//
829+
// The cached value is borrowed and is only valid if guarded by a type
830+
// version check. In free-threaded builds the init method must also use
831+
// deferred reference counting.
832+
//
833+
// Returns 1 if the value was cached or 0 otherwise.
834+
extern int _PyType_CacheInitForSpecialization(PyHeapTypeObject *type,
835+
PyObject *init,
836+
unsigned int tp_version);
827837

828838
#ifdef Py_GIL_DISABLED
829839
# define MANAGED_DICT_OFFSET (((Py_ssize_t)sizeof(PyObject *))*-1)

Objects/typeobject.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5658,23 +5658,21 @@ _PyType_Lookup(PyTypeObject *type, PyObject *name)
56585658
return res;
56595659
}
56605660

5661-
56625661
int
5663-
_PyType_CacheInitForSpecialization(PyTypeObject *type, PyObject *init,
5662+
_PyType_CacheInitForSpecialization(PyHeapTypeObject *type, PyObject *init,
56645663
unsigned int tp_version)
56655664
{
56665665
if (!init || !tp_version) {
56675666
return 0;
56685667
}
56695668
int can_cache;
56705669
BEGIN_TYPE_LOCK();
5671-
can_cache = type->tp_version_tag == tp_version;
5670+
can_cache = ((PyTypeObject*)type)->tp_version_tag == tp_version;
56725671
#ifdef Py_GIL_DISABLED
56735672
can_cache = can_cache && _PyObject_HasDeferredRefcount(init);
56745673
#endif
56755674
if (can_cache) {
5676-
PyHeapTypeObject *ht = (PyHeapTypeObject*) type;
5677-
FT_ATOMIC_STORE_PTR_RELAXED(ht->_spec_cache.init, init);
5675+
FT_ATOMIC_STORE_PTR_RELAXED(type->_spec_cache.init, init);
56785676
}
56795677
END_TYPE_LOCK();
56805678
return can_cache;

Python/specialize.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1992,7 +1992,8 @@ specialize_class_call(PyObject *callable, _Py_CODEUNIT *instr, int nargs)
19921992
Py_XDECREF(init);
19931993
return -1;
19941994
}
1995-
if (init != NULL && _PyType_CacheInitForSpecialization(tp, init, tp_version)) {
1995+
if (init != NULL && _PyType_CacheInitForSpecialization(
1996+
(PyHeapTypeObject *)tp, init, tp_version)) {
19961997
_PyCallCache *cache = (_PyCallCache *)(instr + 1);
19971998
write_u32(cache->func_version, tp_version);
19981999
specialize(instr, CALL_ALLOC_AND_ENTER_INIT);

0 commit comments

Comments
 (0)