Skip to content

Commit beeaa4a

Browse files
DinoVdiegorusso
authored andcommitted
pythongh-112075: Accessing a single element should optimistically avoid locking (python#115109)
Makes accessing a single element thread safe and typically lock free
1 parent 97f461b commit beeaa4a

3 files changed

Lines changed: 496 additions & 175 deletions

File tree

Include/internal/pycore_dict.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ static inline PyDictUnicodeEntry* DK_UNICODE_ENTRIES(PyDictKeysObject *dk) {
211211
#define DICT_WATCHER_MASK ((1 << DICT_MAX_WATCHERS) - 1)
212212
#define DICT_WATCHER_AND_MODIFICATION_MASK ((1 << (DICT_MAX_WATCHERS + DICT_WATCHED_MUTATION_BITS)) - 1)
213213

214+
#define DICT_VALUES_SIZE(values) ((uint8_t *)values)[-1]
215+
214216
#ifdef Py_GIL_DISABLED
215217
#define DICT_NEXT_VERSION(INTERP) \
216218
(_Py_atomic_add_uint64(&(INTERP)->dict_state.global_version, DICT_VERSION_INCREMENT) + DICT_VERSION_INCREMENT)
@@ -256,7 +258,7 @@ _PyDictValues_AddToInsertionOrder(PyDictValues *values, Py_ssize_t ix)
256258
assert(ix < SHARED_KEYS_MAX_SIZE);
257259
uint8_t *size_ptr = ((uint8_t *)values)-2;
258260
int size = *size_ptr;
259-
assert(size+2 < ((uint8_t *)values)[-1]);
261+
assert(size+2 < DICT_VALUES_SIZE(values));
260262
size++;
261263
size_ptr[-size] = (uint8_t)ix;
262264
*size_ptr = size;

Objects/clinic/dictobject.c.h

Lines changed: 1 addition & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)