Skip to content

Make whole-list ImmutableList<T>.Reverse() O(n) - #136063

Merged
eiriktsarpalis merged 3 commits into
mainfrom
copilot/fix-immutablelist-reverse-performance
Oct 20, 2026
Merged

eiriktsarpalis merged 3 commits into
mainfrom
copilot/fix-immutablelist-reverse-performance

Conversation

Copilot AI commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Whole-list reversal currently performs O(n log n) indexed swaps, affecting both ImmutableList<T> and its builder. Mirror the tree instead, visiting each node once while preserving AVL balance.

  • Implementation: Use copy-on-write mutation to clone frozen internal nodes, reuse leaves, and reverse unfrozen builder nodes in place. Route full-range overloads through this path; leave partial-range reversal unchanged.

  • Coverage: Add full-list boundary, balance, indexed-access, and snapshot-preservation cases.

  • Performance: Local BenchmarkDotNet short-run results at 100,000 elements:

    Operation Before After
    Immutable list 17.16 ms 1.23 ms
    Frozen builder 15.85 ms 1.01 ms
    Mutable builder 15.68 ms 0.437 ms

    Immutable-list allocation drops from 4.80 MB to 3.15 MB.

@azure-pipelines

azure-pipelines Bot commented Oct 19, 2026 •

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

Copilot AI changed the title [WIP] Fix performance of ImmutableList<T>.Reverse() method Make whole-list ImmutableList<T>.Reverse() O(n) Oct 19, 2026
Copilot AI requested a review from eiriktsarpalis October 19, 2026 07:40
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-collections
See info in area-owners.md if you want to be subscribed.

@EgorBo

EgorBo commented Oct 19, 2026

Copy link
Copy Markdown
Member

@copilot, benchmark results are ready:

Please analyze the results and act accordingly.
NOTE: some benchmarks may be flaky or bi-modal, so use your judgment when interpreting small differences.

@eiriktsarpalis
eiriktsarpalis merged commit 2422bb8 into main Oct 20, 2026
99 of 101 checks passed
@eiriktsarpalis
eiriktsarpalis deleted the copilot/fix-immutablelist-reverse-performance branch October 20, 2026 09:45
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 21, 2026
xperiandri added a commit to fsprojects/FSharp.Collections.Immutable that referenced this pull request Nov 18, 2026
…es `ImmutableList<T>.Reverse` O(n)

dotnet/runtime#136063 reverses a whole `ImmutableList<T>` or builder in O(n) by mirroring the tree. It fixes
dotnet/runtime#136030, the report of the measurement behind `IndexedList.rev`, and it was merged into main of
dotnet/runtime for 12.0-preview1. The target frameworks of the library, net8.0 and net10.0, keep the O(n log n) members.

The comment at `IndexedList.rev` says that the copy stays for these frameworks and that the choice is to be measured
again when the library targets net12.0. The comment at `IndexedListBuilder.rev` says that its call of the member takes
the fix without a change.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 21, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ImmutableList<T>.Reverse() takes O(n log n) time: it swaps the ends pair by pair through ReplaceAt

5 participants