GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-81057: Move the global Dict-Related Versions to _PyRuntimeState by ericsnowcurrently · Pull Request #99497 · python/cpython

gh-81057: Move the global Dict-Related Versions to _PyRuntimeState #99497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
ericsnowcurrently merged 4 commits into python:main from ericsnowcurrently:consolidate-obj-version-globals
Nov 16, 2022

Uh oh!

There was an error while loading. Please reload this page.

Merged

gh-81057: Move the global Dict-Related Versions to _PyRuntimeState #99497

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
4 commits
Select commit Hold shift + click to select a range
c00d1e0
Move next_dict_keys_version to _PyRuntimeState.
ericsnowcurrently Nov 14, 2022
a937251
Move _pydict_global_version to _PyRuntimeState.
ericsnowcurrently Nov 14, 2022
ba9e889
Move next_func_version to _PyRuntimeState.
ericsnowcurrently Nov 14, 2022
f684dcd
Make "struct _Py_dict_state" a little more compact.
ericsnowcurrently Nov 16, 2022
File filter
Filter by extension


Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

28 changes: 5 additions & 23 deletions 28 Include/internal/pycore_dict.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ extern "C" {
# error "this header requires Py_BUILD_CORE define"
#endif

#include "pycore_dict_state.h"
#include "pycore_runtime.h" // _PyRuntime


/* runtime lifecycle */

Expand All @@ -17,25 +20,6 @@ extern void _PyDict_Fini(PyInterpreterState *interp);

/* other API */

#ifndef WITH_FREELISTS
// without freelists
# define PyDict_MAXFREELIST 0
#endif

#ifndef PyDict_MAXFREELIST
# define PyDict_MAXFREELIST 80
#endif

struct _Py_dict_state {
#if PyDict_MAXFREELIST > 0
/* Dictionary reuse scheme to save calls to malloc and free */
PyDictObject *free_list[PyDict_MAXFREELIST];
int numfree;
PyDictKeysObject *keys_free_list[PyDict_MAXFREELIST];
int keys_numfree;
#endif
};

typedef struct {
/* Cached hash code of me_key. */
Py_hash_t me_hash;
Expand Down Expand Up @@ -152,13 +136,11 @@ struct _dictvalues {
(PyDictUnicodeEntry*)(&((int8_t*)((dk)->dk_indices))[(size_t)1 << (dk)->dk_log2_index_bytes]))
#define DK_IS_UNICODE(dk) ((dk)->dk_kind != DICT_KEYS_GENERAL)

extern uint64_t _pydict_global_version;

#define DICT_MAX_WATCHERS 8
#define DICT_VERSION_INCREMENT (1 << DICT_MAX_WATCHERS)
#define DICT_VERSION_MASK (DICT_VERSION_INCREMENT - 1)

#define DICT_NEXT_VERSION() (_pydict_global_version += DICT_VERSION_INCREMENT)
#define DICT_NEXT_VERSION() \
(_PyRuntime.dict_state.global_version += DICT_VERSION_INCREMENT)

void
_PyDict_SendEvent(int watcher_bits,
Expand Down
47 changes: 47 additions & 0 deletions 47 Include/internal/pycore_dict_state.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#ifndef Py_INTERNAL_DICT_STATE_H
#define Py_INTERNAL_DICT_STATE_H
#ifdef __cplusplus
extern "C" {
#endif

#ifndef Py_BUILD_CORE
# error "this header requires Py_BUILD_CORE define"
#endif


struct _Py_dict_runtime_state {
/*Global counter used to set ma_version_tag field of dictionary.
* 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;
};


#ifndef WITH_FREELISTS
// without freelists
# define PyDict_MAXFREELIST 0
#endif

#ifndef PyDict_MAXFREELIST
# define PyDict_MAXFREELIST 80
#endif

#define DICT_MAX_WATCHERS 8

struct _Py_dict_state {
#if PyDict_MAXFREELIST > 0
/* Dictionary reuse scheme to save calls to malloc and free */
PyDictObject *free_list[PyDict_MAXFREELIST];
PyDictKeysObject *keys_free_list[PyDict_MAXFREELIST];
int numfree;
int keys_numfree;
#endif
PyDict_WatchCallback watchers[DICT_MAX_WATCHERS];
};


#ifdef __cplusplus
}
#endif
#endif /* !Py_INTERNAL_DICT_STATE_H */
4 changes: 4 additions & 0 deletions 4 Include/internal/pycore_function.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ extern "C" {
# error "this header requires Py_BUILD_CORE define"
#endif

struct _py_func_runtime_state {
uint32_t next_version;
};

extern PyFunctionObject* _PyFunction_FromConstructor(PyFrameConstructor *constr);

extern uint32_t _PyFunction_GetVersionForCurrentState(PyFunctionObject *func);
Expand Down
4 changes: 1 addition & 3 deletions 4 Include/internal/pycore_interp.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extern "C" {
#include "pycore_ast_state.h" // struct ast_state
#include "pycore_code.h" // struct callable_cache
#include "pycore_context.h" // struct _Py_context_state
#include "pycore_dict.h" // struct _Py_dict_state
#include "pycore_dict_state.h" // struct _Py_dict_state
#include "pycore_exceptions.h" // struct _Py_exc_state
#include "pycore_floatobject.h" // struct _Py_float_state
#include "pycore_genobject.h" // struct _Py_async_gen_state
Expand Down Expand Up @@ -171,8 +171,6 @@ struct _is {
// Initialized to _PyEval_EvalFrameDefault().
_PyFrameEvalFunction eval_frame;

PyDict_WatchCallback dict_watchers[DICT_MAX_WATCHERS];

Py_ssize_t co_extra_user_count;
freefunc co_extra_freefuncs[MAX_CO_EXTRA_USERS];

Expand Down
4 changes: 4 additions & 0 deletions 4 Include/internal/pycore_runtime.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ extern "C" {
#endif

#include "pycore_atomic.h" /* _Py_atomic_address */
#include "pycore_dict_state.h" // struct _Py_dict_runtime_state
#include "pycore_dtoa.h" // struct _dtoa_runtime_state
#include "pycore_floatobject.h" // struct _Py_float_runtime_state
#include "pycore_function.h" // struct _func_runtime_state
#include "pycore_gil.h" // struct _gil_runtime_state
#include "pycore_global_objects.h" // struct _Py_global_objects
#include "pycore_import.h" // struct _import_runtime_state
Expand Down Expand Up @@ -151,6 +153,8 @@ typedef struct pyruntimestate {

struct _Py_float_runtime_state float_state;
struct _Py_unicode_runtime_state unicode_state;
struct _Py_dict_runtime_state dict_state;
struct _py_func_runtime_state func_state;

struct {
/* Used to set PyTypeObject.tp_version_tag */
Expand Down
6 changes: 6 additions & 0 deletions 6 Include/internal/pycore_runtime_init.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ extern "C" {
.float_format = _py_float_format_unknown, \
.double_format = _py_float_format_unknown, \
}, \
.dict_state = { \
.next_keys_version = 2, \
}, \
.func_state = { \
.next_version = 1, \
}, \
.types = { \
.next_version_tag = 1, \
}, \
Expand Down
1 change: 1 addition & 0 deletions 1 Makefile.pre.in
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1627,6 +1627,7 @@ PYTHON_HEADERS= \
$(srcdir)/Include/internal/pycore_condvar.h \
$(srcdir)/Include/internal/pycore_context.h \
$(srcdir)/Include/internal/pycore_dict.h \
$(srcdir)/Include/internal/pycore_dict_state.h \
$(srcdir)/Include/internal/pycore_descrobject.h \
$(srcdir)/Include/internal/pycore_dtoa.h \
$(srcdir)/Include/internal/pycore_exceptions.h \
Expand Down
1 change: 1 addition & 0 deletions 1 Modules/_functoolsmodule.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Python.h"
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_dict.h" // _PyDict_Pop_KnownHash()
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_object.h" // _PyObject_GC_TRACK
Expand Down
1 change: 1 addition & 0 deletions 1 Objects/call.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Python.h"
#include "pycore_call.h" // _PyObject_CallNoArgsTstate()
#include "pycore_ceval.h" // _Py_EnterRecursiveCallTstate()
#include "pycore_dict.h" // _PyDict_FromItems()
#include "pycore_object.h" // _PyCFunctionWithKeywords_TrampolineCall()
#include "pycore_pyerrors.h" // _PyErr_Occurred()
#include "pycore_pystate.h" // _PyThreadState_GET()
Expand Down
21 changes: 7 additions & 14 deletions 21 Objects/dictobject.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,6 @@ static int dictresize(PyDictObject *mp, uint8_t log_newsize, int unicode);

static PyObject* dict_iter(PyDictObject *dict);

/*Global counter used to set ma_version_tag field of dictionary.
* It is incremented each time that a dictionary is created and each
* time that a dictionary is modified. */
uint64_t _pydict_global_version = 0;

#include "clinic/dictobject.c.h"


Expand Down Expand Up @@ -5661,17 +5656,15 @@ _PyDictKeys_DecRef(PyDictKeysObject *keys)
dictkeys_decref(keys);
}

static uint32_t next_dict_keys_version = 2;

uint32_t _PyDictKeys_GetVersionForCurrentState(PyDictKeysObject *dictkeys)
{
if (dictkeys->dk_version != 0) {
return dictkeys->dk_version;
}
if (next_dict_keys_version == 0) {
if (_PyRuntime.dict_state.next_keys_version == 0) {
return 0;
}
uint32_t v = next_dict_keys_version++;
uint32_t v = _PyRuntime.dict_state.next_keys_version++;
dictkeys->dk_version = v;
return v;
}
Expand All @@ -5683,7 +5676,7 @@ validate_watcher_id(PyInterpreterState *interp, int watcher_id)
PyErr_Format(PyExc_ValueError, "Invalid dict watcher ID %d", watcher_id);
return -1;
}
if (!interp->dict_watchers[watcher_id]) {
if (!interp->dict_state.watchers[watcher_id]) {
PyErr_Format(PyExc_ValueError, "No dict watcher set for ID %d", watcher_id);
return -1;
}
Expand Down Expand Up @@ -5726,8 +5719,8 @@ PyDict_AddWatcher(PyDict_WatchCallback callback)
PyInterpreterState *interp = _PyInterpreterState_GET();

for (int i = 0; i < DICT_MAX_WATCHERS; i++) {
if (!interp->dict_watchers[i]) {
interp->dict_watchers[i] = callback;
if (!interp->dict_state.watchers[i]) {
interp->dict_state.watchers[i] = callback;
return i;
}
}
Expand All @@ -5743,7 +5736,7 @@ PyDict_ClearWatcher(int watcher_id)
if (validate_watcher_id(interp, watcher_id)) {
return -1;
}
interp->dict_watchers[watcher_id] = NULL;
interp->dict_state.watchers[watcher_id] = NULL;
return 0;
}

Expand All @@ -5757,7 +5750,7 @@ _PyDict_SendEvent(int watcher_bits,
PyInterpreterState *interp = _PyInterpreterState_GET();
for (int i = 0; i < DICT_MAX_WATCHERS; i++) {
if (watcher_bits & 1) {
PyDict_WatchCallback cb = interp->dict_watchers[i];
PyDict_WatchCallback cb = interp->dict_state.watchers[i];
if (cb && (cb(event, (PyObject*)mp, key, value) < 0)) {
// some dict modification paths (e.g. PyDict_Clear) can't raise, so we
// can't propagate exceptions from dict watchers.
Expand Down
6 changes: 2 additions & 4 deletions 6 Objects/funcobject.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include "pycore_pyerrors.h" // _PyErr_Occurred()
#include "structmember.h" // PyMemberDef

static uint32_t next_func_version = 1;

PyFunctionObject *
_PyFunction_FromConstructor(PyFrameConstructor *constr)
{
Expand Down Expand Up @@ -129,10 +127,10 @@ uint32_t _PyFunction_GetVersionForCurrentState(PyFunctionObject *func)
if (func->vectorcall != _PyFunction_Vectorcall) {
return 0;
}
if (next_func_version == 0) {
if (_PyRuntime.func_state.next_version == 0) {
return 0;
}
uint32_t v = next_func_version++;
uint32_t v = _PyRuntime.func_state.next_version++;
func->func_version = v;
return v;
}
Expand Down
1 change: 1 addition & 0 deletions 1 Objects/typeobject.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "pycore_call.h"
#include "pycore_code.h" // CO_FAST_FREE
#include "pycore_compile.h" // _Py_Mangle()
#include "pycore_dict.h" // _PyDict_KeysSize()
#include "pycore_initconfig.h" // _PyStatus_OK()
#include "pycore_moduleobject.h" // _PyModule_GetDef()
#include "pycore_object.h" // _PyType_HasFeature()
Expand Down
2 changes: 2 additions & 0 deletions 2 PCbuild/pythoncore.vcxproj
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@
<ClInclude Include="..\Include\internal\pycore_condvar.h" />
<ClInclude Include="..\Include\internal\pycore_context.h" />
<ClInclude Include="..\Include\internal\pycore_descrobject.h" />
<ClInclude Include="..\Include\internal\pycore_dict.h" />
<ClInclude Include="..\Include\internal\pycore_dict_state.h" />
<ClInclude Include="..\Include\internal\pycore_dtoa.h" />
<ClInclude Include="..\Include\internal\pycore_exceptions.h" />
<ClInclude Include="..\Include\internal\pycore_fileutils.h" />
Expand Down
6 changes: 6 additions & 0 deletions 6 PCbuild/pythoncore.vcxproj.filters
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,12 @@
<ClInclude Include="..\Include\internal\pycore_descrobject.h">
<Filter>Include\internal</Filter>
</ClInclude>
<ClInclude Include="..\Include\internal\pycore_dict.h">
<Filter>Include\internal</Filter>
</ClInclude>
<ClInclude Include="..\Include\internal\pycore_dict_state.h">
<Filter>Include\internal</Filter>
</ClInclude>
<ClInclude Include="..\Include\internal\pycore_dtoa.h">
<Filter>Include\internal</Filter>
</ClInclude>
Expand Down
2 changes: 1 addition & 1 deletion 2 Python/pystate.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
Py_CLEAR(interp->interpreter_trampoline);

for (int i=0; i < DICT_MAX_WATCHERS; i++) {
interp->dict_watchers[i] = NULL;
interp->dict_state.watchers[i] = NULL;
}

// XXX Once we have one allocator per interpreter (i.e.
Expand Down
3 changes: 0 additions & 3 deletions 3 Tools/c-analyzer/cpython/globals-to-fix.tsv
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,6 @@ Objects/unicodeobject.c - ucnhash_capi -
Python/suggestions.c levenshtein_distance buffer -

# other
Objects/dictobject.c - _pydict_global_version -
Objects/dictobject.c - next_dict_keys_version -
Objects/funcobject.c - next_func_version -
Objects/object.c - _Py_RefTotal -
Python/perf_trampoline.c - perf_status -
Python/perf_trampoline.c - extra_code_index -
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.