Repository navigation
feat: flatList { } loops over a flat list and an array without boxing - #52
xperiandri wants to merge 4 commits into
Conversation
`for` gets overloads for `FlatList<'U>` and `'U array`. A loop over a flat list used to box the struct and read it through an enumerator object, because the only overload took a sequence. The new overloads read the array under the flat list directly. The check of the argument is in the function that the overload returns, because a statement before it costs one more closure for each loop, which a loop inside a loop pays for each inner loop. The compiler chooses the overload by the type of the source, which must be known where the loop is written, as for `yield!`: a function parameter without a type annotation does not compile as the source of a loop. The changelog and the documentation of the builder say so. The documentation of `FlatList.toSeq` states what the conversion costs, and a new how-to page explains when to use the functions of `FlatList`, `Seq` and LINQ on a flat list. 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 2 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.
🟡 Changes recommended
The guide incorrectly describes parameterless Count() as a full traversal, although a boxed flat list uses the ICollection<T>.Count fast path in O(1) without creating an enumerator.
1 open issue
What changed in this PR
Reduces allocations in flatList { } loops by adding specialized For overloads for flat lists and arrays, while retaining sequence support.
Changes:
- Iterates arrays and flat-list backing arrays directly, with validation inside the returned buffers.
- Adds tests for ordering, nested loops, overload selection, type inference, and invalid sources.
- Documents sequence-conversion costs, LINQ usage, and source-type annotation requirements.
| File | Description |
|---|---|
| FlatListComputationExpressionTests.fs | Tests specialized loops and existing sequence behavior. |
| FlatListCE.fsi | Declares the new overloads and documents type-inference requirements. |
| FlatListCE.fs | Implements direct iteration over arrays and flat lists. |
| FlatList.fsi | Explains toSeq boxing costs and collection interfaces. |
| Use_A_Flat_List_With_Sequences.md | Adds guidance on FlatList, Seq, LINQ, and computation expressions. |
| CHANGELOG.md | Records optimized loops and annotation requirements. |
Review effort: Balanced
| such as `Sum`, `Count`, `Take` and `OrderBy`, are the methods of `System.Linq.Enumerable`: called on a flat list, they | ||
| box it and read it through an enumerator object. `Seq.sum` does the same, so `FlatList.sum` reads a flat list faster | ||
| than both. |
`Span`, `ReadOnlySpan`, `Memory` and `ReadOnlyMemory`. A function cannot keep a span, so a span is copied at once; a memory is read in place. The documentation says where a span does not compile. Arrays, flat lists, lists, array segments and strings keep the overloads that they had: the tests read the overload of `for` from a quotation. The buffer abbreviation stays public, because the inline members use it: an internal abbreviation breaks every other assembly that uses the expression with FS1118. It is hidden from completion and warned about, as FSharp.Core does for `TaskCode`. The references to the ADR leave the comments of the expression. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ut boxing 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>
…ries The test of `yield!` compares the instances that the parts of the expression get: it fails when a part gets another collector than the part before it, which the counts alone do not show. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Proposed Changes
A
forloop inflatList { }over a flat list boxed the struct and read it through an enumerator object, because the only overload ofFortook a sequence.FlatListCEgets two more overloads, forFlatList<'U>and for'U array. The flat list overload reads the array under the flat list directly.forover a flat list of 10forover an array of 10forover a list of 10forover 100 flat lists of 10The overloads check their argument inside the function that they return. A statement before that function costs one more closure for each loop, so a loop inside a loop would allocate for each inner loop (14 832 B instead of 12 432 B in the last row).
The cost. The compiler chooses the overload by the type of the source, which must be known where the loop is written, as for
yield!onmainalready:let f items = flatList { for item in items do yield item }stops compiling (FS0041) and needs a type annotation;#seq<string>needs an annotation when the body uses a member of the loop variable.flatList { }is not in a release yet, so no released code is affected. The changelog and the documentation of the builder say so.Also in this change:
yield!andfortakeSpan,ReadOnlySpan,MemoryandReadOnlyMemory, as the review of feat:IndexedListandIIndexedListmodules with the vocabulary of the FSharp.CoreListmodule #31 asked forindexedList { }. A function cannot keep a span, so a span is copied at once, and a span that comes from outside the expression, or that is read after the next element, does not compile. A memory is read in place and has no such limit. An array segment and a string convert implicitly to a span and stay sequences; the test of the chosen overload covers them and the memories;FlatListBuffer<'T>, the type of the body of the expression, stays public, because the inline members use it: aninternalabbreviation breaks every other assembly that uses the expression with FS1118. It is hidden from completion, and its use by name is a warning, as FSharp.Core does forTaskCode;FlatList.toSeqstates what the conversion costs: one box of 24 bytes whatever the length, and the interfaces that the box implements;How_Tos/Use_A_Flat_List_With_Sequences, explains when to use the functions ofFlatList,Seqand LINQ on a flat list. Every code block was run against this build, andfsdocs build --strict --evalrenders the page.Types of changes
Checklist
Further comments
Eight tests of the loops are new, and the test of a default flat list is split into one for the flat list overload and one for a flat list given as a sequence. They cover the order of the elements for each kind of source, nested loops, the type of the loop variable in the body for each kind of source, the null and default checks with the name of the parameter, and which overload the compiler chose, which a test reads from a quotation of the expression, so that no test depends on a measurement.
Each new behaviour was broken once on purpose: renaming the flat list overload fails the test of the chosen overload, and removing the null check of the array overload fails its test.
The tests of spans and memories have a row for each construct and shape, empty ones included. The tests of
yield!of the body of another expression show that a body adds to what the expression has: the parts of the expression get one and the same builder, with the elements that were added before.All 2 812 tests pass on
net8.0andnet10.0in Debug, and CI runs both configurations.fantomas --checkandCheckOrderpass.🤖 Generated with Claude Code