Skip to content

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

Open
xperiandri wants to merge 5 commits into
mainfrom
perf/flat-list-sequence-inputs
Open

xperiandri wants to merge 5 commits into
mainfrom
perf/flat-list-sequence-inputs

Conversation

@xperiandri

@xperiandri xperiandri commented Nov 18, 2026 •

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

Summary by CodeRabbit

  • Bug Fixes

    • concat now reads non-array sequences to completion once, uses the elements actually yielded, and handles empty inputs consistently.
    • concat and parallel collect now report errors consistently with array operations when the combined result is too large.
    • insertManyAt now handles array and flat-list inputs consistently with array insertion behavior.
  • Performance

    • Concatenating a single nonempty flat list returns that list directly.

… 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

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

FlatList.concat and Parallel.collect now share joining logic that validates parts and handles array-length limits. insertManyAt uses InsertRange for eligible inputs. The changes add tests for sequence shapes, enumeration, storage reuse, and oversized results.

Changes: FlatList operations

Layer / File(s) Summary
Shared joining and concat
src/FSharp.Collections.Immutable/FlatList/FlatList.fs, src/FSharp.Collections.Immutable/FlatList/FlatList.fsi, tests/FSharp.Collections.Immutable.Tests/FlatList/FlatListTests.fs, tests/FSharp.Collections.Immutable.Tests/FlatList/FlatListParallelTests.fs
concat reuses array and flat-list backing arrays, snapshots other sequences, and joins through joinFlatLists. Parallel.collect also calls joinFlatLists. Tests cover sequence shapes, enumeration, single-part reuse, and array-length limits.
Range insertion
src/FSharp.Collections.Immutable/FlatList/FlatList.fs, tests/FSharp.Collections.Immutable.Tests/FlatList/FlatListTests.fs
insertManyAt uses InsertRange for valid indices with nonempty array or flat-list values. Tests cover sequence shapes, copied arrays, lazy enumeration, and collection counts.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Refactor

Possibly related PRs

Suggested reviewers: irina-konovalova

Merge Risk: Minimal · up to d560d

This change reduces copying in FlatList.insertManyAt and FlatList.concat, and in FlatList.Parallel.collect, which now shares the concat joining code. No merge-blocking risk was found, and the new tests cover each guard and the oversized-result exceptions.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the two changed operations, FlatList.insertManyAt and FlatList.concat, and the goal of copying elements once. The changeset matches this: insertManyAt uses InsertRange for array and fl…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the two changed operations, FlatList.insertManyAt and FlatList.concat, and the goal of copying elements once. The changeset matches this: insertManyAt uses InsertRange for array and flat-list inputs, and concat reads inputs in place, totals lengths, and copies into one array. The title is specific and concise, so it passes.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • 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

Recent review info

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: db2d24ea-2c85-4a46-a5a7-5e0b67050a27
Commits

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

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

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

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

xperiandri and others added 2 commits November 30, 2026 04:19
…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>
xperiandri added a commit that referenced this pull request Nov 30, 2026
The comments of `FlatList.Parallel.collect` and of `IndexedContract` are left to #53 and #31, which change the lines next to them, so that the branches do not conflict. Those pull requests have the same fix in them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
xperiandri and others added 2 commits November 30, 2026 14:36
…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>

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