Skip to content

perf(linq): optimize taking one element from the end - #137157

Open
P4suta wants to merge 3 commits into
dotnet:mainfrom
P4suta:perf/take-last-single-element
Open

P4suta wants to merge 3 commits into
dotnet:mainfrom
P4suta:perf/take-last-single-element

Conversation

@P4suta

@P4suta P4suta commented Nov 8, 2026 •

Copy link
Copy Markdown

Why

TakeLast(1) allocates a queue for unknown-count sources.

Changes

Keep one value for TakeLast(1) and Take(^1..^0), preserving deferred execution, checked counting, and disposal before output.

Validation

Local Release build and 52,280 tests pass on macOS arm64 and Linux x64.
TakeLast(1) drops from 505.56 to 72.46 ns and 200 to 56 B, while TakeLastHalf measures 757.81 versus 747.30 ns with 776 B unchanged.

Benchmark reproduction

The PublishAot=true size probe grows by 48 B on macOS arm64.
Use this suite with these methods beside TakeLastHalf:

[Benchmark, ArgumentsSource(nameof(TakeLastArguments))]
public void TakeLastOne(LinqTestData input) => input.Collection.TakeLast(1).Consume(_consumer);
[Benchmark, ArgumentsSource(nameof(TakeLastArguments))]
public void TakeRangeLastOne(LinqTestData input) => input.Collection.Take(^1..^0).Consume(_consumer);
# Linux x64, Intel Core i7-7700K, .NET 12.0.0-alpha.1.26527.106, 100 integers.
# Baseline: 614e7dafc09bcf359e8ce4f166b28189db14c364; shared native runtime, matching System.Linq builds.
export DOTNET_PROCESSOR_COUNT=4 APP_ASSEMBLIES=PROPERTY
export DOTNET_gcServer=0 DOTNET_gcConcurrent=0 DOTNET_TC_CallCountingDelayMs=0 PERFLAB_EVALUATE_OVERHEAD=1
python3 scripts/benchmarks_ci.py --frameworks net10.0 --run-only --filter '*Perf_Enumerable.TakeLastOne*IEnumerable*' '*Perf_Enumerable.TakeRangeLastOne*IEnumerable*' '*Perf_Enumerable.TakeLastHalf*IEnumerable*' --corerun BASELINE_CORERUN CHANGED_CORERUN --bdn-arguments '--launchCount 3 --warmupCount 12 --iterationCount 12 --iterationTime 250'

Related to #118312.

AI generated this contribution.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@jkotas jkotas added the tenet-performance Performance related issue label Nov 8, 2026
@P4suta

P4suta commented Nov 9, 2026

Copy link
Copy Markdown
Author

@dotnet-policy-service agree

@P4suta

P4suta commented Nov 10, 2026

Copy link
Copy Markdown
Author

Build Analysis classifies the failures as known: #135056 and an infrastructure timeout.
Please rerun the canceled osx-arm64 checked Libraries_CheckedCoreCLR job when convenient.

AI generated this contribution.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Linq tenet-performance Performance related issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants