Skip to content

Commit ba20320

Browse files
committed
Add a comment explaining the collection fast paths
1 parent d1e75b9 commit ba20320

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

CommunityToolkit.Mvvm/Collections/ObservableGroupedCollectionExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ static void ThrowArgumentExceptionForKeyNotFound()
6363
ArgumentNullException.ThrowIfNull(source);
6464
ArgumentNullException.For<TKey>.ThrowIfNull(key);
6565

66+
// This pattern is used extensively in this file, with many of the public APIs having a first loop on the retrieved
67+
// list, and then a fallback one sometimes with the same logic, but on the collection itself. This is done as an
68+
// optimization: if a list is available, we can iterate on it directly, which will use List<T>.Enumerator and avoid
69+
// allocations (the enumerator is a struct), additional indirections (the enumerator wraps the list instead of the
70+
// outer collection, and additional overhead (using the value enumerator avoids the interface stub dispatches).
71+
// Because of this, duplicate logic below is intentional and not actually duplicate, as it results in different code.
6672
if (source.TryGetList(out List<ObservableGroup<TKey, TElement>>? list))
6773
{
6874
foreach (ObservableGroup<TKey, TElement> group in list)

0 commit comments

Comments
 (0)