Skip to content
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.

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

Matthias/prototype for less realloc #14

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions Include/cpython/dictobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ typedef struct {
PyDictValues *ma_values;
} PyDictObject;

/*
* A 'finger' to allow deletion of oldest entries in amortised O(1).
* TODO: Does this need to be even more encapsulated and hidden?
*/
typedef struct {
PyDictKeysObject *sentinel;
Py_ssize_t skip_empty;
} PyDictFinger;

PyAPI_FUNC(PyObject *) _PyDict_GetAndPushBack_KnownHash(
PyObject *op, PyObject *key, Py_hash_t hash);
PyAPI_FUNC(PyObject *) _PyDict_GetItem_KnownHash(PyObject *mp, PyObject *key,
Py_hash_t hash);
PyAPI_FUNC(PyObject *) _PyDict_GetItemWithError(PyObject *dp, PyObject *key);
Expand All @@ -42,6 +53,8 @@ PyAPI_FUNC(int) _PyDict_DelItem_KnownHash(PyObject *mp, PyObject *key,
Py_hash_t hash);
PyAPI_FUNC(int) _PyDict_DelItemIf(PyObject *mp, PyObject *key,
int (*predicate)(PyObject *value));
PyAPI_FUNC(PyDictFinger) _PyDict_NewFinger(void);
PyAPI_FUNC(int) _PyDict_DelOldest(PyDictObject *mp, PyDictFinger *finger);
PyAPI_FUNC(int) _PyDict_Next(
PyObject *mp, Py_ssize_t *pos, PyObject **key, PyObject **value, Py_hash_t *hash);

Expand Down
5 changes: 5 additions & 0 deletions Include/internal/pycore_dict.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ struct _dictkeysobject {
/* Number of used entries in dk_entries. */
Py_ssize_t dk_nentries;

// Prototype to avoid the finger.
// TODO: set to 0 on resize and when creating the _dictkeysobject
// DONE: set when creating.
Py_ssize_t skip_empty;

/* Actual hash table of dk_size entries. It holds indices in dk_entries,
or DKIX_EMPTY(-1) or DKIX_DUMMY(-2).

Expand Down