8000 Rename functions and types · python/cpython@77a8144 · GitHub
[go: up one dir, main page]

Skip to content

Commit 77a8144

Browse files
committed
Rename functions and types
1 parent 0e1b00c commit 77a8144

File tree

9 files changed

+109
-108
lines changed

9 files changed

+109
-108
lines changed

Include/internal/pycore_interp.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ extern "C" {
3535
#include "pycore_qsbr.h" // struct _qsbr_state
3636
#include "pycore_tstate.h" // _PyThreadStateImpl
3737
#include "pycore_tuple.h" // struct _Py_tuple_state
38-
#include "pycore_uniqueid.h" // struct _Py_type_id_pool
38+
#include "pycore_uniqueid.h" // struct _Py_unique_id_pool
3939
#include "pycore_typeobject.h" // struct types_state
4040
#include "pycore_unicodeobject.h" // struct _Py_unicode_state
4141
#include "pycore_warnings.h" // struct _warnings_runtime_state
@@ -221,7 +221,7 @@ struct _is {
221221
#if defined(Py_GIL_DISABLED)
222222
struct _mimalloc_interp_state mimalloc;
223223
struct _brc_state brc; // biased reference counting state
224-
struct _Py_type_id_pool type_ids;
224+
struct _Py_unique_id_pool unique_ids; // object ids for per-thread refcounts
225225
PyMutex weakref_locks[NUM_WEAKREF_LIST_LOCKS];
226226
#endif
227227

Include/internal/pycore_object.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,12 +335,12 @@ _Py_INCREF_TYPE(PyTypeObject *type)
335335
// Unsigned comparison so that `unique_id=-1`, which indicates that
336336
// per-thread refcounting has been disabled on this type, is handled by
337337
// the "else".
338-
if ((size_t)ht->unique_id < (size_t)tstate->types.size) {
338+
if ((size_t)ht->unique_id < (size_t)tstate->refcounts.size) {
339339
# ifdef Py_REF_DEBUG
340340
_Py_INCREF_IncRefTotal();
341341
# endif
342342
_Py_INCREF_STAT_INC();
343-
tstate->types.refcounts[ht->unique_id]++;
343+
tstate->refcounts.values[ht->unique_id]++;
344344
}
345345
else {
346346
// The slow path resizes the thread-local refcount array if necessary.
@@ -368,12 +368,12 @@ _Py_DECREF_TYPE(PyTypeObject *type)
368368
// Unsigned comparison so that `unique_id=-1`, which indicates that
369369
// per-thread refcounting has been disabled on this type, is handled by
370370
// the "else".
371-
if ((size_t)ht->unique_id < (size_t)tstate->types.size) {
371+
if ((size_t)ht->unique_id < (size_t)tstate->refcounts.size) {
372372
# ifdef Py_REF_DEBUG
373373
_Py_DECREF_DecRefTotal();
374374
# endif
375375
_Py_DECREF_STAT_INC();
376-
tstate->types.refcounts[ht->unique_id]--;
376+
tstate->refcounts.values[ht->unique_id]--;
377377
}
378378
else {
379379
// Directly decref the type if the type id is not assigned or if

Include/internal/pycore_tstate.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ typedef struct _PyThreadStateImpl {
3232
struct _Py_freelists freelists;
3333
struct _brc_thread_state brc;
3434
struct {
35-
// The thread-local refcounts for heap type objects
36-
Py_ssize_t *refcounts;
35+
// The per-thread refcounts
36+
Py_ssize_t *values;
3737

3838
// Size of the refcounts array.
3939
Py_ssize_t size;
4040

41-
// If set, don't use thread-local refcounts
41+
// If set, don't use per-thread refcounts
4242
int is_finalized;
43-
} types;
43+
} refcounts;
4444
#endif
4545

4646
#if defined(Py_REF_DEBUG) && defined(Py_GIL_DISABLED)

Include/internal/pycore_uniqueid.h

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,61 +10,58 @@ extern "C" {
1010

1111
#ifdef Py_GIL_DISABLED
1212

13-
// This contains code for allocating unique ids to heap type objects
14-
// and re-using those ids when the type is deallocated.
13+
// This contains code for allocating unique ids to objects for
14+
// per-thread reference counting.
1515
//
16-
// The type ids are used to implement per-thread reference counts of
17-
// heap type objects to avoid contention on the reference count fields
18-
// of heap type objects. Static type objects are immortal, so contention
19-
// is not an issue for those types.
16+
// Per-thread reference counting is used along with deferred reference
17+
// counting to avoid scaling bottlenecks due to reference count contention.
2018
//
21-
// Type id of -1 is used to indicate a type doesn't use thread-local
22-
// refcounting. This value is used when a type object is finalized by the GC
23-
// and during interpreter shutdown to allow the type object to be
19+
// An id of -1 is used to indicate that an object doesn't use per-thread
20+
// refcounting. This value is used when the object is finalized by the GC
21+
// and during interpreter shutdown to allow the object to be
2422
// deallocated promptly when the object's refcount reaches zero.
2523
//
26-
// Each entry implicitly represents a type id based on it's offset in the
24+
// Each entry implicitly represents a unique id based on its offset in the
2725
// table. Non-allocated entries form a free-list via the 'next' pointer.
28-
// Allocated entries store the corresponding PyTypeObject.
29-
typedef union _Py_type_id_entry {
26+
// Allocated entries store the corresponding PyObject.
27+
typedef union _Py_unique_id_entry {
3028
// Points to the next free type id, when part of the freelist
31-
union _Py_type_id_entry *next;
29+
union _Py_unique_id_entry *next;
3230

33-
// Stores the type object when the id is assigned
34-
PyHeapTypeObject *type;
35-
} _Py_type_id_entry;
31+
// Stores the object when the id is assigned
32+
PyObject *obj;
33+
} _Py_unique_id_entry;
3634

37-
struct _Py_type_id_pool {
35+
struct _Py_unique_id_pool {
3836
PyMutex mutex;
3937

40-
// combined table of types with allocated type ids and unallocated
41-
// type ids.
42-
_Py_type_id_entry *table;
38+
// combined table of object with allocated unique ids and unallocated ids.
39+
_Py_unique_id_entry *table;
4340

4441
// Next 10000 entry to allocate inside 'table' or NULL
45-
_Py_type_id_entry *freelist;
42+
_Py_unique_id_entry *freelist;
4643

4744
// size of 'table'
4845
Py_ssize_t size;
4946
};
5047

51-
// Assigns the next id from the pool of type ids.
52-
extern void _PyType_AssignId(PyHeapTypeObject *type);
48+
// Assigns the next id from the pool of ids.
49+
extern Py_ssize_t _PyObject_AssignUniqueId(PyObject *obj);
5350

54-
// Releases the allocated type id back to the pool.
55-
extern void _PyType_ReleaseId(PyHeapTypeObject *type);
51+
// Releases the allocated id back to the pool.
52+
extern void _PyObject_ReleaseUniqueId(Py_ssize_t unique_id);
5653

57-
// Merges the thread-local reference counts into the corresponding types.
58-
extern void _PyType_MergeThreadLocalRefcounts(_PyThreadStateImpl *tstate);
54+
// Merges the per-thread reference counts into the corresponding objects.
55+
extern void _PyObject_MergePerThreadRefcounts(_PyThreadStateImpl *tstate);
5956

60-
// Like _PyType_MergeThreadLocalRefcounts, but also frees the thread-local
57+
// Like _PyObject_MergePerThreadRefcounts, but also frees the per-thread
6158
// array of refcounts.
62-
extern void _PyType_FinalizeThreadLocalRefcounts(_PyThreadStateImpl *tstate);
59+
extern void _PyObject_FinalizePerThreadRefcounts(_PyThreadStateImpl *tstate);
6360

6461
// Frees the interpreter's pool of type ids.
65-
extern void _PyType_FinalizeIdPool(PyInterpreterState *interp);
62+
extern void _PyObject_FinalizeUniqueIdPool(PyInterpreterState *interp);
6663

67-
// Increfs the type, resizing the thread-local refcount array if necessary.
64+
// Increfs the type, resizing the per-thread refcount array if necessary.
6865
PyAPI_FUNC(void) _PyType_IncrefSlow(PyHeapTypeObject *type);
6966

7067
#endif /* Py_GIL_DISABLED */

Objects/typeobject.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3932,7 +3932,7 @@ type_new_alloc(type_new_ctx *ctx)
39323932
et->ht_token = NULL;
39333933

39343934
#ifdef Py_GIL_DISABLED
3935-
_PyType_AssignId(et);
3935+
et->unique_id = _PyObject_AssignUniqueId((PyObject *)et);
39363936
#endif
39373937

39383938
return type;
@@ -5026,7 +5026,7 @@ PyType_FromMetaclass(
50265026

50275027
#ifdef Py_GIL_DISABLED
50285028
// Assign a type id to enable thread-local refcounting
5029-
_PyType_AssignId(res);
5029+
res->unique_id = _PyObject_AssignUniqueId((PyObject *)res);
50305030
#endif
50315031

50325032
/* Ready the type (which includes inheritance).
@@ -6080,7 +6080,7 @@ type_dealloc(PyObject *self)
60806080
Py_XDECREF(et->ht_module);
60816081
PyMem_Free(et->_ht_tpname);
60826082
#ifdef Py_GIL_DISABLED
6083-
_PyType_ReleaseId(et);
6083+
_PyObject_ReleaseUniqueId(et->unique_id);
60846084
#endif
60856085
et->ht_token = NULL;
60866086
Py_TYPE(type)->tp_free((PyObject *)type);

Python/gc_free_threading.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,12 @@ disable_deferred_refcounting(PyObject *op)
217217
merge_refcount(op, 0);
218218
}
219219

220-
// Heap types also use thread-local refcounting -- disable it here.
220+
// Heap types also use per-thread refcounting -- disable it here.
221221
if (PyType_Check(op)) {
222-
// Disable thread-local refcounting for heap types
223-
PyTypeObject *type = (PyTypeObject *)op;
224-
if (PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE)) {
225-
_PyType_ReleaseId((PyHeapTypeObject *)op);
222+
if (PyType_HasFeature((PyTypeObject *)op, Py_TPFLAGS_HEAPTYPE)) {
223+
PyHeapTypeObject *ht = (PyHeapTypeObject *)op;
224+
_PyObject_ReleaseUniqueId(ht->unique_id);
225+
ht->unique_id = -1;
226226
}
227227
}
228228

@@ -1221,7 +1221,7 @@ gc_collect_internal(PyInterpreterState *interp, struct collection_state *state,
12211221
_PyThreadStateImpl *tstate = (_PyThreadStateImpl *)p;
12221222

12231223
// merge per-thread refcount for types into the type's actual refcount
1224-
_PyType_MergeThreadLocalRefcounts(tstate);
1224+
_PyObject_MergePerThreadRefcounts(tstate);
12251225

12261226
// merge refcounts for all queued objects
12271227
merge_queued_objects(tstate, state);

Python/pylifecycle.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1834,7 +1834,7 @@ finalize_interp_types(PyInterpreterState *interp)
18341834

18351835
_PyTypes_Fini(interp);
18361836
#ifdef Py_GIL_DISABLED
1837-
_PyType_FinalizeIdPool(interp);
1837+
_PyObject_FinalizeUniqueIdPool(interp);
18381838
#endif
18391839

18401840
_PyCode_Fini(interp);

Python/pystate.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,7 +1745,7 @@ PyThreadState_Clear(PyThreadState *tstate)
17451745

17461746
// Merge our thread-local refcounts into the type's own refcount and
17471747
// free our local refcount array.
1748-
_PyType_FinalizeThreadLocalRefcounts((_PyThreadStateImpl *)tstate);
1748+
_PyObject_FinalizePerThreadRefcounts((_PyThreadStateImpl *)tstate);
17491749

17501750
// Remove ourself from the biased reference counting table of threads.
17511751
_Py_brc_remove_thread(tstate);
@@ -1805,7 +1805,7 @@ tstate_delete_common(PyThreadState *tstate, int release_gil)
18051805
_PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)tstate;
18061806
tstate->interp->object_state.reftotal += tstate_impl->reftotal;
18071807
tstate_impl->reftotal = 0;
1808-
assert(tstate_impl->types.refcounts == NULL);
1808+
assert(tstate_impl->refcounts.values == NULL);
18091809
#endif
18101810

18111811
HEAD_UNLOCK(runtime);

0 commit comments

Comments
 (0)
0