diff options
author | Dino Viehland <dinoviehland@meta.com> | 2024-05-07 00:22:26 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-07 00:22:26 (GMT) |
commit | ff6cbb2503a8fe3fceeadd889e34fc9a8f308ecd (patch) | |
tree | ed2ab05ad2ca3f3819babe399933ffcb85c93ad4 /Include | |
parent | 723d4d2fe8e77b398f0ccffcfa541149caaac6a1 (diff) | |
download | cpython-ff6cbb2503a8fe3fceeadd889e34fc9a8f308ecd.zip cpython-ff6cbb2503a8fe3fceeadd889e34fc9a8f308ecd.tar.gz cpython-ff6cbb2503a8fe3fceeadd889e34fc9a8f308ecd.tar.bz2 |
gh-112075: use per-thread dict version pool (#118676)
use thread state set of dict versions
Diffstat (limited to 'Include')
-rw-r--r-- | Include/cpython/pystate.h | 1 | ||||
-rw-r--r-- | Include/internal/pycore_dict.h | 21 |
2 files changed, 20 insertions, 2 deletions
diff --git a/Include/cpython/pystate.h b/Include/cpython/pystate.h index 0611e29..2df9ecd 100644 --- a/Include/cpython/pystate.h +++ b/Include/cpython/pystate.h @@ -188,6 +188,7 @@ struct _ts { PyObject *previous_executor; + uint64_t dict_global_version; }; #ifdef Py_DEBUG diff --git a/Include/internal/pycore_dict.h b/Include/internal/pycore_dict.h index cb7d4c3..8d8d374 100644 --- a/Include/internal/pycore_dict.h +++ b/Include/internal/pycore_dict.h @@ -221,8 +221,25 @@ static inline PyDictUnicodeEntry* DK_UNICODE_ENTRIES(PyDictKeysObject *dk) { #define DICT_WATCHER_AND_MODIFICATION_MASK ((1 << (DICT_MAX_WATCHERS + DICT_WATCHED_MUTATION_BITS)) - 1) #ifdef Py_GIL_DISABLED -#define DICT_NEXT_VERSION(INTERP) \ - (_Py_atomic_add_uint64(&(INTERP)->dict_state.global_version, DICT_VERSION_INCREMENT) + DICT_VERSION_INCREMENT) + +#define THREAD_LOCAL_DICT_VERSION_COUNT 256 +#define THREAD_LOCAL_DICT_VERSION_BATCH THREAD_LOCAL_DICT_VERSION_COUNT * DICT_VERSION_INCREMENT + +static inline uint64_t +dict_next_version(PyInterpreterState *interp) +{ + PyThreadState *tstate = PyThreadState_GET(); + uint64_t cur_progress = (tstate->dict_global_version & + (THREAD_LOCAL_DICT_VERSION_BATCH - 1)); + if (cur_progress == 0) { + uint64_t next = _Py_atomic_add_uint64(&interp->dict_state.global_version, + THREAD_LOCAL_DICT_VERSION_BATCH); + tstate->dict_global_version = next; + } + return tstate->dict_global_version += DICT_VERSION_INCREMENT; +} + +#define DICT_NEXT_VERSION(INTERP) dict_next_version(INTERP) #else #define DICT_NEXT_VERSION(INTERP) \ |