“ZLinq”, a Zero-Allocation LINQ Library for .NET
Published on: 2025-06-29 22:29:12
ValueEnumerable Architecture and Optimization
For usage, please refer to the ReadMe. Here, I’ll delve deeper into optimization. The architectural distinction goes beyond simply implementing lazy sequence execution, containing many innovations compared to collection processing libraries in other languages.
The definition of ValueEnumerable , which forms the basis of chaining, looks like this:
public readonly ref struct ValueEnumerable(TEnumerator enumerator)
where TEnumerator : struct, IValueEnumerator, allows ref struct // allows ref struct only in .NET 9 or later
{
public readonly TEnumerator Enumerator = enumerator;
}
public interface IValueEnumerator : IDisposable
{
bool TryGetNext(out T current); // as MoveNext + Current
// Optimization helper
bool TryGetNonEnumeratedCount(out int count);
bool TryGetSpan(out ReadOnlySpan span);
bool TryCopyTo(scoped Span destination, Index offset);
}
Based on this, operators like Where chain as follo
... Read full article.