Skip to content

Cache whether a type is a collection - #1890

Merged
SimonCropp merged 1 commit into
mainfrom
perf-collection-type-cache
Aug 29, 2026
Merged

Cache whether a type is a collection#1890
SimonCropp merged 1 commit into
mainfrom
perf-collection-type-cache

Conversation

@SimonCropp

Copy link
Copy Markdown
Member

Whether a value is a collection depends only on its type, but it was recomputed per value. With the default ignoreEmptyCollections this runs for every non null member value, array item and dictionary value, and anything that is not a non generic ICollection (every HashSet, dictionary key and value view, iterator, LINQ result and immutable collection) allocated a fresh Type[] from GetInterfaces and then made several LINQ passes over it with FullName string comparisons, for every occurrence of the same type.

The classification is now held in a ConcurrentDictionary keyed by type, leaving only the per value IsEmpty enumeration.

The added test cases all sit below the ICollection fast path, so they exercise the classification, and they pass unchanged against the previous implementation.

Whether a value is a collection depends only on its type, but it was recomputed
per value. With the default ignoreEmptyCollections this runs for every non null
member value, array item and dictionary value, and anything that is not a non
generic ICollection (every HashSet, dictionary key and value view, iterator,
LINQ result and immutable collection) allocated a fresh Type[] from GetInterfaces
and then made several LINQ passes over it with FullName string comparisons, for
every occurrence of the same type.

The classification is now held in a ConcurrentDictionary keyed by type, leaving
only the per value IsEmpty enumeration.

The added test cases all sit below the ICollection fast path, so they exercise
the classification, and they pass unchanged against the previous implementation.
@SimonCropp SimonCropp added this to the 33.0.0 milestone Aug 29, 2026
@SimonCropp
SimonCropp merged commit 0faf6cf into main Aug 29, 2026
7 checks passed
@SimonCropp
SimonCropp deleted the perf-collection-type-cache branch August 29, 2026 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant