Skip to content

perf: FlatList.insertManyAt and FlatList.concat copy the elements once - #53

Open
xperiandri wants to merge 1 commit into
mainfrom
perf/flat-list-sequence-inputs
Open

xperiandri wants to merge 1 commit into
mainfrom
perf/flat-list-sequence-inputs

Conversation

@xperiandri

Copy link
Copy Markdown
Collaborator

Proposed Changes

Two functions that take a sequence copied it more than once.

FlatList.insertManyAt. Array.insertManyAt copies every sequence into a temporary array first. An array or a flat list of values now goes to the InsertRange member of ImmutableArray<'T>, which copies it straight into the result. 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 to InsertRange as 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).
  • An empty flat list is left to Array.insertManyAt: InsertRange would 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, as append returns its other flat list. A total length that does not fit an array is left to Array.concat, so the exception stays the one of FSharp.Core.

FlatList.Parallel.collect joined its results with a second copy of the same two passes. Both functions 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. Before, the unchecked sum of its lengths could wrap around.

Allocated per call, .NET 10.0.15 Before After
insertManyAt, 100 values into 1 000, array 4 848 B 4 424 B
insertManyAt, 100 values into 1 000, flat list 4 872 B 4 448 B
insertManyAt, 4 values into 10, array 120 B 80 B
insertManyAt, 100 values, list, ResizeArray or lazy sequence unchanged unchanged
concat, 8 flat lists of 2 elements, array 392 B 88 B
concat, 8 flat lists of 2 elements, list 400 B 176 B
concat, 8 flat lists of 2 elements, flat list of flat lists 416 B 112 B
concat, 8 flat lists of 2 elements, lazy sequence 440 B 312 B
concat, 8 flat lists of 126 elements, array 4 360 B 4 056 B
concat, one flat list with elements among empty ones 312 B 48 B

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

This is a performance change of functions that are not in a release yet.

Checklist

  • Build and tests pass locally
  • I have added tests that prove my fix is effective or that my feature works (if appropriate)
  • I have added necessary documentation (if appropriate)

Further comments

Behaviour. I compared the built library with the one of main on 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:

  • concat returns the only flat list with elements itself;
  • concat reads a lazy outer sequence to the end before it reports a default flat list in it, as Array.concat does. The documentation says so.

A note on InsertRange. On an empty ImmutableArray<'T>, AddRange and InsertRange with 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 if insertManyAt starts 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.0 and net10.0 in Debug and Release. fantomas --check and CheckOrder pass.

🤖 Generated with Claude Code

… 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>
@coderabbitai

coderabbitai Bot commented Nov 18, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You'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.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4b0b9020-ec68-447f-9ffe-4c0e089929a3
Commits

Reviewing files that changed from the base of the PR and between 6b106fc and f09d204.

Files selected for processing: 4

  • src/FSharp.Collections.Immutable/FlatList/FlatList.fs
  • src/FSharp.Collections.Immutable/FlatList/FlatList.fsi
  • tests/FSharp.Collections.Immutable.Tests/FlatList/FlatListParallelTests.fs
  • tests/FSharp.Collections.Immutable.Tests/FlatList/FlatListTests.fs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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 insertManyAt for arrays and flat lists, retaining fallback paths for other inputs.
  • Shares a joining helper between concat and Parallel.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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants