Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
enable re-use of tuples in the free-threading build
  • Loading branch information
eendebakpt committed Aug 27, 2024
commit 755a862c7e560b1b31c95a25e8bbe937389acc75
20 changes: 20 additions & 0 deletions Include/refcount.h
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,26 @@ static inline PyObject* _Py_XNewRef(PyObject *obj)
# define Py_XNewRef(obj) _Py_XNewRef(obj)
#endif

#ifdef Py_GIL_DISABLED
static inline int _Py_Reuse_Immutable_Object(PyObject *ob) {
/* Function to check whether an immutable object such as a tuple can be
* modified inplace by the owning method
*/
uint32_t local = _Py_atomic_load_uint32_relaxed(&ob->ob_ref_local);
if (local != 1)
return 0;
// maybe we need to check on Py_ARITHMETIC_RIGHT_SHIFT(Py_ssize_t, shared, _Py_REF_SHARED_SHIFT) ?
Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&ob->ob_ref_shared);
if (shared != 0) {
return 0;
}
return ob->ob_tid == _Py_ThreadId();
}
#else
static inline int _Py_Reuse_Immutable_Object(PyObject *ob) {
return Py_REFCNT(ob) == 1;
}
#endif

#ifdef __cplusplus
}
Expand Down
7 changes: 2 additions & 5 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2971,11 +2971,8 @@ zip_next(zipobject *lz)

if (tuplesize == 0)
return NULL;
#ifdef Py_GIL_DISABLED
int reuse_tuple = 0;
#else
int reuse_tuple = Py_REFCNT(result) == 1;
#endif

int reuse_tuple = _Py_Reuse_Immutable_Object(result);
if (reuse_tuple) {
Py_INCREF(result);
for (i=0 ; i < tuplesize ; i++) {
Expand Down