dotnet/runtime

TakeLast Performance Improvement suggestion

Open

#118,312 opened on Aug 3, 2025

View on GitHub
 (3 comments) (0 reactions) (0 assignees)C# (17,886 stars) (5,445 forks)batch import
area-System.Linqhelp wantedtenet-performance

Description

TakeRangeFromEndIterator uses a regular Queue <T>when IsStartIndexFromEnd, but this process can be done without intervening yields, allowing optimization using ArrayPool and Span.

https://github.com/dotnet/runtime/blob/3f25a276772b8d0e54452321e7525b7a176aa4a7/src/libraries/System.Linq/src/System/Linq/Take.cs#L111C13-L154C18

Below is the PR I did on ZLinq. https://github.com/Cysharp/ZLinq/pull/207/files Unlike ZLinq, a ToArray is required at the end of the queueing for Linq, but it will be similar.

Contributor guide