Repository navigation
perf: FlatList.insertManyAt and FlatList.concat copy the elements once - #53
Open
xperiandri wants to merge 1 commit into
Open
xperiandri wants to merge 1 commit into
xperiandri wants to merge 1 commit into
Conversation
… once `insertManyAt` hands an array or a flat list of values to the `InsertRange` member of `ImmutableArray<'T>`, which copies the values straight into the result, where `Array.insertManyAt` copies them into a temporary array first. Everything else still goes to `Array.insertManyAt`, for a reason each: * `InsertRange` reads the count of a collection before it enumerates it, so a collection whose count differs from what it yields, such as one that another thread changes, loses or invents elements. An array and a flat list have a fixed count. * The values are given as a sequence on purpose: the overload of `InsertRange` for an array keeps the array of the caller instead of copying it when the flat list is empty. * An empty flat list is left to `Array.insertManyAt`: `InsertRange` would return values that are a flat list as the result, so that strings given as objects would give a result that is not an array of objects. `concat` reads an array of flat lists, and the array under a flat list of flat lists, in place, adds up the lengths in one pass and copies into one array of the total length. A flat list that is the only one with elements is returned as it is, as `append` returns its other flat list. A total length that does not fit an array is left to `Array.concat`, so that the exception stays the one of FSharp.Core. `Parallel.collect` joined its results with a second copy of the same two passes, so both call one private function now, which also gives `Parallel.collect` the exception of FSharp.Core for a total length that does not fit an array. The new tests compare the results with FSharp.Core for every shape of the sequence, and each guard above has a test that fails when the guard is removed. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 39 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the FlatList optimization changes.
No open issues 🎉
What changed in this PR
This PR reduces copying and allocations in FlatList sequence operations while preserving input validation and FSharp.Core exception behavior.
Changes:
- Optimizes
insertManyAtfor arrays and flat lists, retaining fallback paths for other inputs. - Shares a joining helper between
concatandParallel.collect, with overflow handling and reuse of the sole non-empty flat list. - Adds regression tests and documents concatenation behavior.
| File | Description |
|---|---|
| tests/FSharp.Collections.Immutable.Tests/FlatList/FlatListTests.fs | Tests sequence shapes, copying, covariance, enumeration, and concatenation overflow. |
| tests/FSharp.Collections.Immutable.Tests/FlatList/FlatListParallelTests.fs | Tests overflow exception compatibility for parallel collection. |
| src/FSharp.Collections.Immutable/FlatList/FlatList.fsi | Documents concatenation reuse and sequence enumeration behavior. |
| src/FSharp.Collections.Immutable/FlatList/FlatList.fs | Implements insertion fast paths and shared concatenation logic. |
Review effort: Balanced
This branch has not been deployed
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Proposed Changes
Two functions that take a sequence copied it more than once.
FlatList.insertManyAt.Array.insertManyAtcopies every sequence into a temporary array first. An array or a flat list of values now goes to theInsertRangemember ofImmutableArray<'T>, which copies it straight into the result. Everything else still goes toArray.insertManyAt, for a reason each:InsertRangereads the count of a collection before it enumerates it, so a collection whose count differs from what it yields, such as one that another thread changes, loses or invents elements. An array and a flat list have a fixed count.InsertRangeas a sequence on purpose: its overload for an array keeps the array of the caller instead of copying it when the flat list is empty (see the note below).Array.insertManyAt:InsertRangewould return values that are a flat list as the result, so strings given as objects would give a result that is not an array of objects.FlatList.concat. It reads an array of flat lists, and the array under a flat list of flat lists, in place, adds up the lengths in one pass and copies into one array of the total length. A flat list that is the only one with elements is returned as it is, asappendreturns its other flat list. A total length that does not fit an array is left toArray.concat, so the exception stays the one of FSharp.Core.FlatList.Parallel.collectjoined its results with a second copy of the same two passes. Both functions call one private function now, which also givesParallel.collectthe exception of FSharp.Core for a total length that does not fit an array. Before, the unchecked sum of its lengths could wrap around.insertManyAt, 100 values into 1 000, arrayinsertManyAt, 100 values into 1 000, flat listinsertManyAt, 4 values into 10, arrayinsertManyAt, 100 values, list,ResizeArrayor lazy sequenceconcat, 8 flat lists of 2 elements, arrayconcat, 8 flat lists of 2 elements, listconcat, 8 flat lists of 2 elements, flat list of flat listsconcat, 8 flat lists of 2 elements, lazy sequenceconcat, 8 flat lists of 126 elements, arrayconcat, one flat list with elements among empty onesTypes of changes
This is a performance change of functions that are not in a release yet.
Checklist
Further comments
Behaviour. I compared the built library with the one of
mainon 609 inputs: every shape of the sequence,null, default flat lists, indices out of range, collections that report a wrong count, strings given as objects, and totals that do not fit an array. The results, the exception types, the parameter names, the first lines of the messages and the type of the array under each result are identical. The differences are by design:concatreturns the only flat list with elements itself;concatreads a lazy outer sequence to the end before it reports a default flat list in it, asArray.concatdoes. The documentation says so.A note on
InsertRange. On an emptyImmutableArray<'T>,AddRangeandInsertRangewith an array keep the array of the caller instead of copying it (System.Collections.Immutable 10.0, checked by writing to the array afterwards). The library does not call these overloads, and a test fails ifinsertManyAtstarts to.Tests. There are 231 new rows and tests. They compare the results with FSharp.Core for every shape of the values and of the outer sequence, and for the inputs where the guards above apply. Each guard has a test that fails when the guard is removed. I checked that by breaking the code once: dropping the guards and the overflow branch failed exactly the 10 tests that cover them. The overflow tests need only a small input, one flat list of 65 536 elements repeated.
All 3 020 tests pass on
net8.0andnet10.0in Debug and Release.fantomas --checkandCheckOrderpass.🤖 Generated with Claude Code