Closed
Description
Currently, most operators are implemented as such:
public static IEnumerator<TSource> Operator<TSource>(this IEnumerable<TSource> source, ...)
{
// guard statements
return _();
_()
{
yield return source.First();
yield break;
}
}
A better implementation would be the following:
public static IEnumerator<TSource> Operator<TSource>(this IEnumerable<TSource> source, ...)
{
// guard statements
return _(source, ...);
static _(IEnumerable<TSource> source, ...)
{
yield return source.First();
yield break;
}
}
With the current implementation, the host method must create a closure class before even doing guard statements, copy parameters, and then call a method on that closure class. This means that there are two closure classes: one for the parameters, and one for the enumerator.
Switching to passing the parameters into the iterator method means that only one closure class has to be created, the one for the enumerator; the parameters will be held by that class. This should simplify and improve the IL, the JIT, and the output JIT-ted code.