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

Arena dict #5

Merged
merged 8 commits into from
Dec 9, 2023
Merged

Arena dict #5

merged 8 commits into from
Dec 9, 2023

Conversation

Enichan
Copy link
Owner

@Enichan Enichan commented Dec 9, 2023

ArenaDict works like a normal Dictionary<TKey, TValue> but allocated to an unmanaged memory arena. It uses a single contiguous array of memory storage for both its backing array and any additional entries that are part of a linked list for a bucket with multiple items. The linked list's head is always found in the backing array. Linked list nodes are allocated via a bump allocator which starts after the backing array, and removed entries are stored in a freelist to be reused.

The amount of items stored after the backing array always equals or exceeds the capacity of the backing array, which means that even in the worst case scenario of all entries mapping to a single bucket there is always enough space for all items before rebalancing occurs.

Though I have not tested it my assumption is that because the backing array and linked list node entries exist in the same contiguous memory area cache misses are reduced, at least for small dictionaries.

This dictionary uses fibonacci hashing, which means the capacity of the backing array must always be a power of 2.

@Enichan Enichan merged commit a3479de into main Dec 9, 2023
@Enichan Enichan deleted the ArenaDict branch December 9, 2023 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant