avoid Keys/Values ToList materialization in DictionaryWrapper#366
Merged
SimonCropp merged 1 commit intoMay 18, 2026
Merged
Conversation
The Keys/Values properties (both the generic ICollection<T> and the
non-generic IDictionary surface) materialized a full List<T> on every
access via Cast<T>().ToList() / ToList(). Replace with a lazy
ReadOnlyCollectionWrapper<T> that holds the source IEnumerable<T> plus
a snapshot Count and defers enumeration to the caller.
Benchmark (net11.0 in-process, DictionaryWrapperKeysBenchmark):
Access only (no iteration):
Hashtable.Keys Count=500: 16,541 ns / 4160 B -> 43 ns / 80 B
ReadOnly.Keys Count=500: 4,060 ns / 4056 B -> 33 ns / 32 B
IDictionary.Keys Count=500: 3,735 ns / 4056 B -> 19 ns / 32 B
Access + foreach:
Hashtable.Keys Count=500: 17,530 ns / 4160 B -> 16,443 ns / 136 B
Hashtable.Keys Count=5 : 228 ns / 200 B -> 198 ns / 136 B
Allocations drop from O(N) to a flat 32-80 B regardless of dictionary
size. CPU is dominated by the underlying Cast iterator on the iterate
path, so wall-time wins there are modest; the pure-access path (e.g.
reflection / introspection callers that never enumerate) is 100-400x
faster at Count=500.
This was referenced May 18, 2026
Open
This was referenced May 19, 2026
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.
The Keys/Values properties (both the generic ICollection and the
non-generic IDictionary surface) materialized a full List on every
access via Cast().ToList() / ToList(). Replace with a lazy
ReadOnlyCollectionWrapper that holds the source IEnumerable plus
a snapshot Count and defers enumeration to the caller.
Benchmark (net11.0 in-process, DictionaryWrapperKeysBenchmark):
Allocations drop from O(N) to a flat 32-80 B regardless of dictionary
size. CPU is dominated by the underlying Cast iterator on the iterate
path, so wall-time wins there are modest; the pure-access path (e.g.
reflection / introspection callers that never enumerate) is 100-400x
faster at Count=500.