Repository navigation
Make whole-list ImmutableList<T>.Reverse() O(n) - #136063
Merged
eiriktsarpalis merged 3 commits intoOct 20, 2026
Merged
Conversation
|
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. |
Co-authored-by: eiriktsarpalis <[email protected]>
Co-authored-by: eiriktsarpalis <[email protected]>
Copilot
AI
changed the title
[WIP] Fix performance of ImmutableList<T>.Reverse() method
Make whole-list ImmutableList<T>.Reverse() O(n)
Oct 19, 2026
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-collections |
Member
|
@copilot, benchmark results are ready: Please analyze the results and act accordingly. |
eiriktsarpalis
approved these changes
Oct 19, 2026
tannergooding
approved these changes
Oct 19, 2026
akoeplinger
approved these changes
Oct 20, 2026
eiriktsarpalis
deleted the
copilot/fix-immutablelist-reverse-performance
branch
October 20, 2026 09:45
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
Immutable-list allocation drops from 4.80 MB to 3.15 MB.
ImmutableList<T>.Reverse()takes O(n log n) time: it swaps the ends pair by pair throughReplaceAt#136030