Skip to content

Amortize the cost of small tables #47

Closed
@Amanieu

Description

@Amanieu

When growing a table, hashbrown will start with a table of size 2 (which can hold 1 element due to load factor) and then double every time the load factor is reached.

Table size Capacity Memory usage (sizeof(T) = 4) Memory usage (sizeof(T) = 8) Memory usage (sizeof(T) = 16)
(empty) 0 0 0 0
2 1 26 (32) 34 (40) 50 (56)
4 3 36 (40) 52 (56) 84 (88)
8 7 56 88 152
16 14 96 160 288
32 28 176 304 560

(rounded up to 8 for allocator alignment)

In comparison, the current std HashMap starts off empty but then immediately grows to a table size of 32 (capacity 29) as soon as the first element is added.

We want to try to balance memory usage for small tables with the allocation overhead when growing tables up to their final size.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions