Skip to content

HashMap remove: two heap allocations per displaced entry when re-inserting the probe cluster #939

Closed

Description

@ehartford

Found in the container survey prompted by #936. The design is sound — FNV hash, open addressing with linear probing, 0.7 load factor, doubling growth, and deletion by clearing the slot and re-inserting the following cluster (correct for linear probing). One hot-path wart:

rt/rt_core.w with_hashmap_remove: for every occupied slot after the removed one, it does rt_alloc(ksz) + rt_alloc(vsz), memcpy both, clear the slot, with_hashmap_insert, then rt_free_sized both. Two allocator round trips per displaced entry, on every removal from a busy table.

Fix

Re-insert in place: compute the displaced entry's home slot and use backward-shift deletion (move it into the hole if the hole lies between its home and current position), or at least stage the key/value in a fixed stack buffer sized by ksz/vsz (both are known per map) instead of the heap. No semantic change; the existing HashMap fixtures cover correctness. Measure with a remove-heavy fixture (insert 100k, remove 90k) before and after.

Activity

  1. ehartford commented on Sep 2, 2026

    @ehartford
    ContributorAuthor

    Ruling (2026-09-01): the engine comes from the migrated corpora (c-algorithms / TommyDS / STC, migrated whole; M*LIB's B+ tree surgically) with a With facade choosing the winner — see docs/stdlib_sourcing_plan.md and the note on #940. This issue stays open as the facade deliverable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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