Skip to content

Commit

Permalink
Enable non-allocating enumeration via collection interfaces (#5246)
Browse files Browse the repository at this point in the history
Interfaces like `IEnumerable<T>`, `IList<T>` and `IDictionary<TKey, TValue>` are used in many APIs. The upside of this is that the implementation of these APIs may change the concrete type of the collection as it sees fit without breaking consumers. The downside of this is that some operations through the interface are more expensive than if they were performed directly on the concrete type. Enumeration is perhaps the biggest challege here, as enumerating through these interfaces with `foreach` always requires a heap allocation.

Most concrete implementations these implementations will be types such as `List<T>` and `Dictionary<TKey,TValue>` which have a non-allocating struct enumerators.

This change adds `NoAllocEnumerate()` extension methods that avoid any enumerator allocation when the concrete collection type is known to have such a struct enumerator at runtime.
  • Loading branch information
drewnoakes authored Jun 21, 2023
1 parent 008151d commit 6e78daf
Show file tree
Hide file tree
Showing 6 changed files with 638 additions and 5 deletions.
Loading

0 comments on commit 6e78daf

Please sign in to comment.