What does IPartition do in .NET Core?

I looked at the source of .NET Core to see if IEnumerable.Last() (LINQ) calls were optimized when the collection implements IList , which I suspected happened after some quick benchmarking. It turns out that yes, the input is specifically checked for IList , but even before that it checks to see if the IPartition input is IPartition . IPartition defined here , but I don’t understand what it should do.

What is the purpose of IPartition and how can it be faster than the regular indexing of IList (or if it is not so, why is the input checked for IPartition earlier than IList )?

+5
source share
1 answer

This is an optimization for enumerated methods that act on indexes (Take / Skip and others) when the collection supports indexes. IIListProvider is bound.

There are several implementations . Find the file for IPartition .

issues about it.

IPartition allows IPartition to delegate higher-level operations to the base collection. For example, new int[10].Skip(1) slow because all data runs through two IEnumerable<int> . When implementing IPartition this effectively turns into for (int i = 1 ... 9) emit(list[i]); . Thus, it does not use intermediate counters and calls a list pointer, which is slightly faster than an enumeration.

This is a very crude description. I recommend that you read the Github discussions and code.

+4
source

All Articles