Repository navigation
perf: FlatList.insertManyAt and FlatList.concat copy the elements once - #53
xperiandri wants to merge 5 commits 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>
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
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
…lements once Comments only: a `<summary>` takes one line when its text has up to 60 characters, whatever the length of the `<remarks>`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lements once Comments only: the reference to the ADR leaves the comment next to the change of `FlatList.Parallel.collect`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lements once The cost of `FlatList.concat` counts the flat lists: the function reads every one of them, also the empty ones. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lements once The test of strings given as objects compiles without nullness warnings: `nonNull` asserts the box before the cast (FS3264), and `Assert.IsExactInstanceOfType` takes the place of `GetType` of a nullable array (FS3261). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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
Summary by CodeRabbit
Bug Fixes
concatnow reads non-array sequences to completion once, uses the elements actually yielded, and handles empty inputs consistently.concatand parallelcollectnow report errors consistently with array operations when the combined result is too large.insertManyAtnow handles array and flat-list inputs consistently with array insertion behavior.Performance