Skip to content

Commit b3c3977

Browse files
committed
pythongh-131525: Cache the result of tuple_hash
1 parent ce79274 commit b3c3977

File tree

100 files changed

+1194
-161
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+1194
-161
lines changed

Include/cpython/tupleobject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
typedef struct {
66
PyObject_VAR_HEAD
7+
/* Cached hash. Initially set to -1. */
8+
Py_uhash_t ob_hash;
79
/* ob_item contains space for 'ob_size' elements.
810
Items must normally not be NULL, except during construction when
911
the tuple is not yet visible outside the function that builds it. */

Include/internal/pycore_tuple.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11+
#include "pycore_gc.h" // _PyObject_GC_IS_TRACKED
1112
#include "pycore_structs.h" // _PyStackRef
1213

1314
extern void _PyTuple_MaybeUntrack(PyObject *);
@@ -32,6 +33,20 @@ typedef struct {
3233
PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
3334
} _PyTupleIterObject;
3435

36+
#define _PyTuple_RESET_HASH_CACHE(op) (_PyTuple_CAST(op)->ob_hash = -1)
37+
38+
/*
39+
bpo-42536: If reusing a tuple object, this should be called to re-track it
40+
with the garbage collector and reset its hash cache. */
41+
static inline void
42+
_PyTuple_Recycle(PyObject *op)
43+
{
44+
_PyTuple_RESET_HASH_CACHE(op);
45+
if (!_PyObject_GC_IS_TRACKED(op)) {
46+
_PyObject_GC_TRACK(op);
47+
}
48+
}
49+
3550
#ifdef __cplusplus
3651
}
3752
#endif

0 commit comments

Comments
 (0)