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

AtomicDict now uses regular linear probing instead of Robin Hood hashing #38

Open
wants to merge 9 commits into
base: dev
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ Python3_add_library(_cereggii MODULE
"cereggii/atomic_dict/meta.c"
"cereggii/atomic_dict/migrate.c"
"cereggii/atomic_dict/node_ops.c"
"cereggii/atomic_dict/node_sizes_table.c"
"cereggii/atomic_dict/robin_hood.c"
"cereggii/atomic_int/atomic_int.c"
"cereggii/atomic_int/handle.c"
"cereggii/atomic_event.c"
Expand Down
1 change: 0 additions & 1 deletion src/cereggii/_cereggii.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,6 @@ class AtomicDict:
`AtomicDict`. This can greatly reduce contention when the keys in the input are repeated.
"""

def compact(self) -> None: ...
def _debug(self) -> dict:
"""
Provide some debugging information.
Expand Down
14 changes: 14 additions & 0 deletions src/cereggii/atomic_dict/accessor_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ AtomicDict_GetOrCreateAccessorStorage(AtomicDict *self)
storage->reservation_buffer.tail = 0;
storage->reservation_buffer.count = 0;
memset(storage->reservation_buffer.reservations, 0, sizeof(AtomicDict_EntryLoc) * RESERVATION_BUFFER_SIZE);
storage->meta = (AtomicDict_Meta *) AtomicRef_Get(self->metadata);

int set = PyThread_tss_set(self->accessor_key, storage);
if (set != 0)
Expand Down Expand Up @@ -55,6 +56,18 @@ AtomicDict_GetAccessorStorage(Py_tss_t *accessor_key)
return storage;
}

inline AtomicDict_Meta *
AtomicDict_GetMeta(AtomicDict *self, AtomicDict_AccessorStorage *storage)
{
if (self->metadata->reference == (PyObject *) storage->meta)
return storage->meta;

Py_CLEAR(storage->meta);
storage->meta = (AtomicDict_Meta *) AtomicRef_Get(self->metadata);
assert(storage->meta != NULL);
return storage->meta;
}

void
AtomicDict_BeginSynchronousOperation(AtomicDict *self)
{
Expand Down Expand Up @@ -86,6 +99,7 @@ AtomicDict_EndSynchronousOperation(AtomicDict *self)
void
AtomicDict_AccessorStorage_dealloc(AtomicDict_AccessorStorage *self)
{
Py_CLEAR(self->meta);
Py_TYPE(self)->tp_free((PyObject *) self);
}

Expand Down
45 changes: 14 additions & 31 deletions src/cereggii/atomic_dict/atomic_dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ PyObject *
AtomicDict_new(PyTypeObject *type, PyObject *Py_UNUSED(args), PyObject *Py_UNUSED(kwds))
{
AtomicDict *self = NULL;
self = PyObject_GC_New(AtomicDict, &AtomicDict_Type);
self = PyObject_GC_New(AtomicDict, type);
if (self != NULL) {
self->metadata = NULL;
self->metadata = (AtomicRef *) AtomicRef_new(&AtomicRef_Type, NULL, NULL);
Expand Down Expand Up @@ -267,7 +267,6 @@ AtomicDict_init(AtomicDict *self, PyObject *args, PyObject *kwargs)
}

Py_DECREF(meta); // so that the only meta's refcount depends only on AtomicRef
assert(Py_REFCNT(meta) == 1);
return 0;
fail:
Py_XDECREF(meta);
Expand Down Expand Up @@ -327,27 +326,18 @@ AtomicDict_UnsafeInsert(AtomicDict_Meta *meta, Py_hash_t hash, uint64_t pos)
.index = pos,
.tag = hash,
};
uint64_t ix = AtomicDict_Distance0Of(hash, meta);
const uint64_t d0 = AtomicDict_Distance0Of(hash, meta);

for (int probe = 0; probe < meta->max_distance; probe++) {
AtomicDict_ReadNodeAt((ix + probe) % meta->size, &temp, meta);
for (uint64_t distance = 0; distance < SIZE_OF(meta); distance++) {
AtomicDict_ReadNodeAt((d0 + distance) & (SIZE_OF(meta) - 1), &temp, meta);

if (temp.node == 0) {
node.distance = probe;
AtomicDict_WriteNodeAt((ix + probe) % meta->size, &node, meta);
AtomicDict_WriteNodeAt((d0 + distance) & (SIZE_OF(meta) - 1), &node, meta);
goto done;
}

if (temp.distance < probe) {
// non-atomic robin hood
node.distance = probe;
AtomicDict_WriteNodeAt((ix + probe) % meta->size, &node, meta);
ix = ix + probe - temp.distance;
probe = temp.distance;
node = temp;
}
}
// probes exhausted

// full
return -1;
done:
return 0;
Expand Down Expand Up @@ -381,7 +371,11 @@ PyObject *
AtomicDict_LenBounds(AtomicDict *self)
{
AtomicDict_Meta *meta = NULL;
meta = (AtomicDict_Meta *) AtomicRef_Get(self->metadata);
AtomicDict_AccessorStorage *storage = AtomicDict_GetOrCreateAccessorStorage(self);
if (storage == NULL)
goto fail;

meta = AtomicDict_GetMeta(self, storage);
if (meta == NULL)
goto fail;

Expand All @@ -407,7 +401,6 @@ AtomicDict_LenBounds(AtomicDict *self)
// visit the grb
found += AtomicDict_CountKeysInBlock(grb, meta);
}
Py_DECREF(meta);
meta = NULL;

if (supposedly_full_blocks < 0) {
Expand Down Expand Up @@ -438,7 +431,6 @@ AtomicDict_LenBounds(AtomicDict *self)
return Py_BuildValue("(ll)", lower + found, upper + found);

fail:
Py_XDECREF(meta);
return NULL;
}

Expand Down Expand Up @@ -548,18 +540,9 @@ AtomicDict_Debug(AtomicDict *self)
PyObject *block_info = NULL;

meta = (AtomicDict_Meta *) AtomicRef_Get(self->metadata);
metadata = Py_BuildValue("{sOsOsOsOsOsOsOsOsOsOsOsOsOsOsO}",
metadata = Py_BuildValue("{sOsOsOsOsOsO}",
"log_size\0", Py_BuildValue("B", meta->log_size),
"generation\0", Py_BuildValue("n", (Py_ssize_t) meta->generation),
"node_size\0", Py_BuildValue("B", meta->node_size),
"distance_size\0", Py_BuildValue("B", meta->distance_size),
"tag_size\0", Py_BuildValue("B", meta->tag_size),
"node_mask\0", Py_BuildValue("k", meta->node_mask),
"index_mask\0", Py_BuildValue("k", meta->index_mask),
"distance_mask\0", Py_BuildValue("k", meta->distance_mask),
"tag_mask\0", Py_BuildValue("k", meta->tag_mask),
"tombstone\0", Py_BuildValue("k", meta->tombstone.node),
"is_compact\0", Py_BuildValue("B", meta->is_compact),
"inserting_block\0", Py_BuildValue("l", meta->inserting_block),
"greatest_allocated_block\0", Py_BuildValue("l", meta->greatest_allocated_block),
"greatest_deleted_block\0", Py_BuildValue("l", meta->greatest_deleted_block),
Expand All @@ -572,7 +555,7 @@ AtomicDict_Debug(AtomicDict *self)
goto fail;

AtomicDict_Node node;
for (uint64_t i = 0; i < meta->size; i++) {
for (uint64_t i = 0; i < SIZE_OF(meta); i++) {
AtomicDict_ReadNodeAt(i, &node, meta);
PyObject *n = Py_BuildValue("k", node.node);
if (n == NULL)
Expand Down
14 changes: 7 additions & 7 deletions src/cereggii/atomic_dict/blocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ AtomicDictBlock_traverse(AtomicDict_Block *self, visitproc visit, void *arg)
for (int i = 0; i < ATOMIC_DICT_ENTRIES_IN_BLOCK; ++i) {
entry = self->entries[i];

if (entry.value == NULL || entry.flags & ENTRY_FLAGS_TOMBSTONE || entry.flags & ENTRY_FLAGS_SWAPPED)
if (entry.value == NULL)
continue;

Py_VISIT(entry.key);
Expand All @@ -50,7 +50,7 @@ AtomicDictBlock_clear(AtomicDict_Block *self)
for (int i = 0; i < ATOMIC_DICT_ENTRIES_IN_BLOCK; ++i) {
entry = self->entries[i];

if (entry.flags & ENTRY_FLAGS_TOMBSTONE || entry.flags & ENTRY_FLAGS_SWAPPED)
if (entry.value == NULL)
continue;

self->entries[i].key = NULL;
Expand Down Expand Up @@ -108,10 +108,10 @@ AtomicDict_GetEmptyEntry(AtomicDict *self, AtomicDict_Meta *meta, AtomicDict_Res
CereggiiAtomic_CompareExchangeInt64(&meta->inserting_block, inserting_block, inserting_block + 1);
goto reserve_in_inserting_block; // even if the above CAS fails
}
if (greatest_allocated_block + 1 >= meta->size >> ATOMIC_DICT_LOG_ENTRIES_IN_BLOCK) {
if (greatest_allocated_block + 1 >= SIZE_OF(meta) >> ATOMIC_DICT_LOG_ENTRIES_IN_BLOCK) {
return 0; // must grow
}
assert(greatest_allocated_block + 1 <= meta->size >> ATOMIC_DICT_LOG_ENTRIES_IN_BLOCK);
assert(greatest_allocated_block + 1 <= SIZE_OF(meta) >> ATOMIC_DICT_LOG_ENTRIES_IN_BLOCK);

AtomicDict_Block *block = NULL;
block = AtomicDictBlock_New(meta);
Expand All @@ -121,7 +121,7 @@ AtomicDict_GetEmptyEntry(AtomicDict *self, AtomicDict_Meta *meta, AtomicDict_Res
block->entries[0].flags = ENTRY_FLAGS_RESERVED;

if (CereggiiAtomic_CompareExchangePtr((void **) &meta->blocks[greatest_allocated_block + 1], NULL, block)) {
if (greatest_allocated_block + 2 < meta->size >> ATOMIC_DICT_LOG_ENTRIES_IN_BLOCK) {
if (greatest_allocated_block + 2 < SIZE_OF(meta) >> ATOMIC_DICT_LOG_ENTRIES_IN_BLOCK) {
CereggiiAtomic_StorePtr((void **) &meta->blocks[greatest_allocated_block + 2], NULL);
}
CereggiiAtomic_CompareExchangeInt64(&meta->greatest_allocated_block,
Expand All @@ -144,7 +144,7 @@ AtomicDict_GetEmptyEntry(AtomicDict *self, AtomicDict_Meta *meta, AtomicDict_Res
done:
assert(entry_loc->entry != NULL);
assert(entry_loc->entry->key == NULL);
assert(entry_loc->location < meta->size);
assert(entry_loc->location < SIZE_OF(meta));
return 1;
fail:
entry_loc->entry = NULL;
Expand Down Expand Up @@ -178,7 +178,7 @@ AtomicDict_ReadEntry(AtomicDict_Entry *entry_p, AtomicDict_Entry *entry)
{
entry->flags = entry_p->flags;
entry->value = entry_p->value;
if (entry->value == NULL || entry->flags & ENTRY_FLAGS_TOMBSTONE || entry->flags & ENTRY_FLAGS_SWAPPED) {
if (entry->value == NULL) {
entry->key = NULL;
entry->value = NULL;
entry->hash = -1;
Expand Down
Loading