]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-100227: Move next_keys_version to PyInterpreterState (gh-102335)
authorEric Snow <ericsnowcurrently@gmail.com>
Thu, 9 Mar 2023 01:04:16 +0000 (18:04 -0700)
committerGitHub <noreply@github.com>
Thu, 9 Mar 2023 01:04:16 +0000 (18:04 -0700)
https://github.com/python/cpython/issues/100227

Include/internal/pycore_dict.h
Include/internal/pycore_dict_state.h
Include/internal/pycore_runtime_init.h
Objects/dictobject.c
Python/specialize.c

index 1af5e59a677a9a14b0d51cdf80efe1ca7f2f3773..12c3c708e29b955e11e489a22b38c239510fa7d6 100644 (file)
@@ -37,7 +37,8 @@ extern PyObject *_PyDict_FromKeys(PyObject *, PyObject *, PyObject *);
 
 /* Gets a version number unique to the current state of the keys of dict, if possible.
  * Returns the version number, or zero if it was not possible to get a version number. */
-extern uint32_t _PyDictKeys_GetVersionForCurrentState(PyDictKeysObject *dictkeys);
+extern uint32_t _PyDictKeys_GetVersionForCurrentState(
+        PyInterpreterState *interp, PyDictKeysObject *dictkeys);
 
 extern size_t _PyDict_KeysSize(PyDictKeysObject *keys);
 
index 77375ea8beb8777b2eaae948c2655859b2a28c2f..c5142ee6763a86f8f0e150e3a95f917045e3f036 100644 (file)
@@ -14,7 +14,6 @@ struct _Py_dict_runtime_state {
      * It is incremented each time that a dictionary is created and each
      * time that a dictionary is modified. */
     uint64_t global_version;
-    uint32_t next_keys_version;
 };
 
 
@@ -30,6 +29,8 @@ struct _Py_dict_runtime_state {
 #define DICT_MAX_WATCHERS 8
 
 struct _Py_dict_state {
+    uint32_t next_keys_version;
+
 #if PyDict_MAXFREELIST > 0
     /* Dictionary reuse scheme to save calls to malloc and free */
     PyDictObject *free_list[PyDict_MAXFREELIST];
@@ -37,6 +38,7 @@ struct _Py_dict_state {
     int numfree;
     int keys_numfree;
 #endif
+
     PyDict_WatchCallback watchers[DICT_MAX_WATCHERS];
 };
 
index aeabcfd2b9056ca22e0b897996f0c3f494258593..efc82b43a61dab177ab5701df457cd8ec0fa68cc 100644 (file)
@@ -65,9 +65,6 @@ extern PyTypeObject _PyExc_MemoryError;
             .float_format = _py_float_format_unknown, \
             .double_format = _py_float_format_unknown, \
         }, \
-        .dict_state = { \
-            .next_keys_version = 2, \
-        }, \
         .types = { \
             .next_version_tag = 1, \
         }, \
@@ -113,6 +110,9 @@ extern PyTypeObject _PyExc_MemoryError;
             }, \
         }, \
         .dtoa = _dtoa_state_INIT(&(INTERP)), \
+        .dict_state = { \
+            .next_keys_version = 2, \
+        }, \
         .func_state = { \
             .next_version = 1, \
         }, \
index a60f275742a5341510787fdb566e891db51dd2f0..b58e93f4320d425a114e118d6c030ce5c4cbd4b1 100644 (file)
@@ -5655,15 +5655,16 @@ _PyDictKeys_DecRef(PyDictKeysObject *keys)
     dictkeys_decref(keys);
 }
 
-uint32_t _PyDictKeys_GetVersionForCurrentState(PyDictKeysObject *dictkeys)
+uint32_t _PyDictKeys_GetVersionForCurrentState(PyInterpreterState *interp,
+                                               PyDictKeysObject *dictkeys)
 {
     if (dictkeys->dk_version != 0) {
         return dictkeys->dk_version;
     }
-    if (_PyRuntime.dict_state.next_keys_version == 0) {
+    if (interp->dict_state.next_keys_version == 0) {
         return 0;
     }
-    uint32_t v = _PyRuntime.dict_state.next_keys_version++;
+    uint32_t v = interp->dict_state.next_keys_version++;
     dictkeys->dk_version = v;
     return v;
 }
index 3405d2b0ab068039d6f2ffbafa7ab3c49dd8508e..0a7af8991eec83c792b36d6cfedfa1d8fc83db87 100644 (file)
@@ -512,7 +512,8 @@ specialize_module_load_attr(
                             SPEC_FAIL_OUT_OF_RANGE);
         return -1;
     }
-    uint32_t keys_version = _PyDictKeys_GetVersionForCurrentState(dict->ma_keys);
+    uint32_t keys_version = _PyDictKeys_GetVersionForCurrentState(
+            _PyInterpreterState_GET(), dict->ma_keys);
     if (keys_version == 0) {
         SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OUT_OF_VERSIONS);
         return -1;
@@ -1063,7 +1064,8 @@ PyObject *descr, DescriptorClassification kind)
             SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_SHADOWED);
             return 0;
         }
-        uint32_t keys_version = _PyDictKeys_GetVersionForCurrentState(keys);
+        uint32_t keys_version = _PyDictKeys_GetVersionForCurrentState(
+                _PyInterpreterState_GET(), keys);
         if (keys_version == 0) {
             SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_OUT_OF_VERSIONS);
             return 0;
@@ -1134,12 +1136,14 @@ _Py_Specialize_LoadGlobal(
         SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_EXPECTED_ERROR);
         goto fail;
     }
+    PyInterpreterState *interp = _PyInterpreterState_GET();
     if (index != DKIX_EMPTY) {
         if (index != (uint16_t)index) {
             SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_RANGE);
             goto fail;
         }
-        uint32_t keys_version = _PyDictKeys_GetVersionForCurrentState(globals_keys);
+        uint32_t keys_version = _PyDictKeys_GetVersionForCurrentState(
+                interp, globals_keys);
         if (keys_version == 0) {
             SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_VERSIONS);
             goto fail;
@@ -1167,12 +1171,14 @@ _Py_Specialize_LoadGlobal(
         SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_RANGE);
         goto fail;
     }
-    uint32_t globals_version = _PyDictKeys_GetVersionForCurrentState(globals_keys);
+    uint32_t globals_version = _PyDictKeys_GetVersionForCurrentState(
+            interp, globals_keys);
     if (globals_version == 0) {
         SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_VERSIONS);
         goto fail;
     }
-    uint32_t builtins_version = _PyDictKeys_GetVersionForCurrentState(builtin_keys);
+    uint32_t builtins_version = _PyDictKeys_GetVersionForCurrentState(
+            interp, builtin_keys);
     if (builtins_version == 0) {
         SPECIALIZATION_FAIL(LOAD_GLOBAL, SPEC_FAIL_OUT_OF_VERSIONS);
         goto fail;