Skip to content

feat: replace HashMap with HashTable for WeakMap#49

Open
shruti2522 wants to merge 1 commit intoboa-dev:mainfrom
shruti2522:hashtable-weakmap
Open

feat: replace HashMap with HashTable for WeakMap#49
shruti2522 wants to merge 1 commit intoboa-dev:mainfrom
shruti2522:hashtable-weakmap

Conversation

@shruti2522
Copy link
Contributor

As discussed in #13, HashTable is a better approach for our WeakMap

  • HashTable prevents any redundnat key storage since HashTable<(usize,Ptr)> stores address once, while HashMap<usize, Ptr> stores it twice
  • HashTable::insert_unique avoids the lookup HashMap::insert always performs.
  • HashTable::find_entry returns an OccupiedEntry so .remove() extracts the value at that same slot without a second lookup.
  • HashTable::retain(|&mut T|) operates on the entry tuple directly avoiding separate key/value refs

Copy link
Member

@nekevss nekevss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Pretty big fan of the change.

Can you add a unit test for what happens when somebody tries to insert a Gc<K> for the key on two entries into a WeakMap? Do we handle that correctly?

Mostly I'm skeptical about whether key_addr is actually unique ... so we should test it! 😄

@shruti2522 shruti2522 force-pushed the hashtable-weakmap branch 2 times, most recently from ef94d60 to 5037d9e Compare March 13, 2026 12:02
@shruti2522 shruti2522 marked this pull request as draft March 13, 2026 12:03
@shruti2522 shruti2522 marked this pull request as ready for review March 13, 2026 12:45
@shruti2522
Copy link
Contributor Author

added three new tests to verify key_addr uniqueness:
two_distinct_keys_wm : two different keys in the same map, to confirm they don’t collide and their values stay separate.
two_maps_same_key_wm : same key used in two different maps, ensuring each map works independently.
drop_map_with_live_key_wm : dropping a WeakMap while its key is still alive, to check that the collector safely ignores the removed map

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.

2 participants