Repository navigation
Add ContiguousMap.CopyKeysTo/CopyValuesTo and obsolete the copying *Span accessors [minor] - #145
Open
matt-edmondson wants to merge 1 commit into
Open
matt-edmondson wants to merge 1 commit into
matt-edmondson wants to merge 1 commit into
Conversation
…pan accessors [minor] GetKeysSpan and GetValuesSpan read as zero-copy views, but every call allocated a new array and copied the whole map, so an indexed loop over them was O(n²). CopyKeysTo(Span<TKey>) and CopyValuesTo(Span<TValue>) fill a caller-owned buffer without allocating. The *Span methods keep working, now through those, but are marked [Obsolete] (a warning, not an error) pointing at the replacements and at AsReadOnlySpan() for zero-copy access to Entry.Key/Value. The ContiguousMap span-access benchmark now measures AsReadOnlySpan, the real zero-copy path, instead of the copy. Fixes #102 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW6AfToAg6F8bcZwwZ5
|
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.



Fixes #102
What was wrong
ContiguousMap.GetKeysSpan()andGetValuesSpan()return aReadOnlySpan<T>, and their remarks promised "direct access … from the contiguous memory". In fact every call allocated a new array and copied every entry, sofor (i …) map.GetKeysSpan()[i]was O(n²) and allocated n arrays. Entries are stored asEntry { Key, Value }structs, so a keys-only view can't exist without a copy.Change
This follows the option recommended in the issue's triage comment:
CopyKeysTo(Span<TKey>)andCopyValuesTo(Span<TValue>)fill the start of a caller-owned buffer in insertion order, with no allocation. A destination shorter thanCountthrowsArgumentException(ParamName == "destination") before anything is written.GetKeysSpanandGetValuesSpanare now[Obsolete]as a warning, not an error, so existing callers keep compiling. The message points at the newCopy*Tomethod and atAsReadOnlySpan()for zero-copy access toEntry.Key/Entry.Value. They now delegate toCopy*To, and their remarks state that each call allocates. Removing them would be a[major]change and is left for later.ContiguousMap_SpanAccesswas timing the copy as if it were span access. It now iteratesAsReadOnlySpan().The existing
GetKeysSpan/GetValuesSpantests still run, wrapped in a local#pragma warning disable CS0618.Tests
ContiguousMapTests: insertion-order copies for keys and values, a longer destination keeps its tail, and a shorter destination throws without writing (keys and values).CopyKeysAndValuesTo_DoNotAllocate:GC.GetAllocatedBytesForCurrentThread()shows 0 bytes for both copies.CopyingSpanAccessors_AreObsoleteAndPointAtTheirReplacement: reflection checks the[Obsolete]attribute, its message, and thatIsErroris false.[Obsolete]attributes removed, both cases of the obsolete test fail. Onmain, the new tests don't compile (CS0117, noCopyKeysTo/CopyValuesTo).Containers.Testsuite: 524 passed, 0 failed. The solution builds with no warnings, anddotnet pack(package validation) succeeds.Generated by Claude Code