-
Notifications
You must be signed in to change notification settings - Fork 14
weak_map: replace entries in one lookup and research ephemron vs key/value pair for insert #50
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # WeakMap Support Follow up | ||
|
|
||
| Note author: shruti2522 | ||
|
|
||
| Revision of: [2026-02-26](2026-02-26.md) | ||
|
|
||
| This document is a follow up to the original weak map support note. | ||
|
|
||
| ## what changed since the original note | ||
|
|
||
| ### `WeakMap<K, V>` representation: `HashTable` over `HashMap` | ||
|
|
||
| The original note identified using `HashTable` instead of `HashMap` as a | ||
| potential improvement. We implemented this in the move away from | ||
| `HashMap<usize, ArenaPointer<Ephemeron<K, V>>>` to a | ||
| `HashTable<(usize, EphemeronPtr)>` model. The address and ephemeron pointer are | ||
| now stored inline as a tuple in the table entry, which saves the cost of a | ||
| separate key allocation and improves memory locality | ||
|
|
||
| ### ephemeron vs key/value pair: why we kept the key/value approach | ||
|
|
||
| One research question was whether `insert` should take an `Ephemeron` directly | ||
| instead of a `(key, value)` pair. The answer is, it should not. Here's why: | ||
|
|
||
| **`Ephemeron` is a GC internal.** The collector owns the lifecycle | ||
| of ephemerons. They get allocated, traced during mark phase, checked during sweep | ||
| for reachability and finalized when their keys die. | ||
|
|
||
| If `WeakMap::insert` exposed `Ephemeron` in its public API, we would leak GC | ||
| internals into the user facing weak map interface. Instead, `insert` takes a | ||
| `(key, value)` pair, which is the right boundary. The ephemeron allocation and | ||
| queue registration happen internally via `collector.alloc_ephemeron_node` | ||
|
|
||
| This keeps the weak map simple for users while hiding the complex GC details. | ||
|
|
||
| ### `replace_or_insert`: one lookup instead of two | ||
|
|
||
| Previously, `insert` did a two step update: remove any old ephemeron, then | ||
| insert the new one. This meant two lookups in the map and two queue operations. | ||
|
|
||
| Now, `replace_or_insert` does both operations in a single `HashTable::find_entry` | ||
| lookup: | ||
|
|
||
| - If an entry exists for the address, swap the new ephemeron in and invalidate the old one | ||
| - If no entry exists, insert the new entry | ||
|
|
||
| This is faster and also makes sure the old ephemeron is cleaned up before the | ||
| new one takes its place. | ||
|
|
||
| ### how the collector manages weak maps | ||
|
|
||
| The collector owns all weak maps internally. `WeakMap` is just a handle pointing | ||
| to memory the collector owns. During cleanup, the collector prunes dead entries | ||
| from weak maps after marking dead objects but before freeing their memory. This | ||
| order matters because we need to read status bits on the ephemerons to decide | ||
| which entries to keep. If we freed the memory first, those bits would be gone. | ||
|
|
||
| ## conclusion | ||
|
|
||
| `WeakMap::insert` should look simple to users | ||
| (just key and value), while all the ephemeron management stays hidden inside | ||
| the collector. | ||
|
|
||
| Changes made since the original note: | ||
|
|
||
| 1. Switch to `HashTable` to store key and pointer together, saving memory | ||
| 2. Use `replace_or_insert` for faster updates (one lookup instead of two) | ||
| 3. Confirm that ephemerons should never appear in the user facing `WeakMap` API |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: let's make this insert, and return an Option.
This API is pretty standard for various other Rust map types, and we should adhere to it where possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done