Repository navigation
HashMap.remove and HashSet.remove release the stored key (#1189) - #1194
Merged
Merged
Conversation
with_hashmap_remove copied the value out and cleared the slot's occupancy; the stored key was never dropped. The table is type-erased, so the runtime cannot drop it, and MAP_REMOVE in codegen emitted no drop either: a key that owns memory leaked 16 bytes per removal (debug allocator: LEAK size=16, leak count=1 for one m.remove(k) on a HashMap[str, str]; on main and on the installed compiler). MAP_CLEAR and scope exit were already right: they walk live slots with the receiver's sema type. with_hashmap_remove_entry transports the stored key's bytes into key_out before the slot is vacated - remove still compares stored keys while it rehashes the entries that follow, so the key cannot be dropped first - and codegen drops it with its real type. A miss leaves the zeroed slot, whose drop frees nothing. Spec §2.3 (D44): transport is not duplication, and the key has exactly one owner, which must release it. lib/map_remove_cases.w joins the D44 ownership harness (--debug-alloc with scribble, with and without reuse, zero leaks): hits, repeated misses, draining a map and reusing it, Copy and owning keys, HashSet. remove goes back into the traversal cases. emit-c never drops map elements at all: #1192. Fixes #1189
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #1189. Ownership change, alone in its batch.
with_hashmap_removecopied the value out and cleared the slot's occupancy; the stored key was never dropped. The table is type-erased, so the runtime cannot drop it, andMAP_REMOVEin codegen emitted no drop either. A key that owns memory leaked: onem.remove(k)on aHashMap[str, str]reportsdebug-alloc: LEAK size=16,leak count=1— onmainand on the installed compiler.MAP_CLEARand scope exit were already right (they walk live slots with the receiver's sema type).Fix.
with_hashmap_remove_entrytransports the stored key's bytes intokey_outbefore the slot is vacated —removestill compares stored keys while it rehashes the entries that follow, so the key cannot be dropped first — and codegen drops it with its real type. A miss leaves the zeroed slot, whose drop frees nothing. The old entry point is kept for Copy keys. Spec §2.3 (D44): transport is not duplication, and the key has exactly one owner, which must release it.Tests.
test/behavior/lib/map_remove_cases.wjoins the D44 ownership harness (run --debug-allocwithWITH_DEBUG_ALLOC_SCRIBBLE=1, with and without block reuse,leak count=0, no double free): hits, repeated misses, draining a map to empty and reusing it, Copy and owning keys,HashSet.removegoes back into the traversal cases, where it had been left out so that file could assert zero leaks.Battery (pinned seed v0.15.2.0):
build,:fixpoint,:test-with-audits(drop and move audits inside it),:test-green,:last-green— all rc=0, 1236 s.Not fixed here: emit-c never drops map elements at all (#1192).