Skip to content

perf((Seq|List|Array).traverseResult[AM]): Speed/memory optimizations - #383

Merged
TheAngryByrd merged 16 commits into
demystifyfp:masterfrom
bartelink:list-traverse-perf
Nov 24, 2026
Merged

TheAngryByrd merged 16 commits into
demystifyfp:masterfrom
bartelink:list-traverse-perf

Conversation

@bartelink

@bartelink bartelink commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #167

Copilot AI balanced review requested due to automatic review settings November 8, 2026 04:25

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.

🟡 Changes recommended

Immutable bindings for mutable-struct collectors invalidate the new benchmark comparisons, and the task API’s cancellation behavior lacks regression coverage.

2 open issues
What changed in this PR

This PR adds list-traversal benchmarking toward #167 and aligns APIs across the core, Hopac, and IcedTasks packages with FSharp.Core 11.

Changes:

  • Adds list-traversal benchmark wiring and .NET 10 targets.
  • Migrates constructors, exception handlers, and async-await helpers, retaining deprecated compatibility APIs.
  • Updates tests, documentation, release notes, and formatting guidance.

This pull request requires changes. The following findings remain unresolved:

  • Moderate · 2 votes — benchmarks/benchmarks.fsproj:17: The included ListTraverse.fs workers bind mutable-struct collectors without mutable, causing empty results and invalid comparisons. Make every collector binding mutable.
  • Moderate · 1 vote — src/FsToolkit.ErrorHandling/List.fs:42: Library traversals still prepend and reverse lists; the allocation improvements exist only in benchmarks. Apply the selected implementation with ordering/short-circuit tests, or describe the PR as benchmark-only rather than resolving #167.
  • Moderate · 3 votes — src/FsToolkit.ErrorHandling/TaskResult.fs:182: Add catchWith tests for canceled input tasks and OperationCanceledException, verifying cancellation propagation without calling the error handler across shim-backed and FSharp.Core 11 targets.
  • Nit · 3 votes — gitbook/jobResult/fromAsync.md:25: Returning an exception produces Ok exception. Throw it inside the computation to demonstrate the documented error path.
  • Nit · 1 vote — tests/FsToolkit.ErrorHandling.IcedTasks.Tests/ValueTaskValueOptionCE.fs:63: Retain a Task input in the return-from test so it covers the Task<'T voption> Source overload returning ValueNone.
  • Nit · 1 vote — tests/FsToolkit.ErrorHandling.IcedTasks.Tests/ValueTaskValueOptionCE.fs:153: Use TaskValueOption.none to preserve Task-specific ValueNone bind and short-circuit coverage.
File Description
tools/​FunctionMap/​README.md Removes deprecated helper entries.
tests/​FsToolkit.ErrorHandling.Tests/​TestHelpers.fs Uses ValueTask.result in test helpers.
tests/​FsToolkit.ErrorHandling.Tests/​TaskValueOptionCE.fs Migrates computation-expression test inputs.
tests/​FsToolkit.ErrorHandling.Tests/​TaskValueOption.fs Updates value-option task constructors.
tests/​FsToolkit.ErrorHandling.Tests/​TaskValidationCE.fs Migrates validation-expression test inputs.
tests/​FsToolkit.ErrorHandling.Tests/​TaskValidation.fs Uses Task.result in validation tests.
tests/​FsToolkit.ErrorHandling.Tests/​TaskResultOption.fs Updates specialized task constructors.
tests/​FsToolkit.ErrorHandling.Tests/​TaskResultCE.fs Migrates result-expression test inputs.
tests/​FsToolkit.ErrorHandling.Tests/​TaskResult.fs Tests catchWith and removes legacy tests.
tests/​FsToolkit.ErrorHandling.Tests/​TaskOptionCE.fs Migrates option-expression test inputs.
tests/​FsToolkit.ErrorHandling.Tests/​TaskOption.fs Updates option-task constructors.
tests/​FsToolkit.ErrorHandling.Tests/​Seq.fs Uses Async.Await in sequence tests.
tests/​FsToolkit.ErrorHandling.Tests/​Result.fs Removes duplicate fallback tests.
tests/​FsToolkit.ErrorHandling.Tests/​ParallelAsyncValidationCE.fs Migrates parallel-validation test inputs.
tests/​FsToolkit.ErrorHandling.Tests/​ParallelAsyncResultCE.fs Migrates parallel-result test inputs.
tests/​FsToolkit.ErrorHandling.Tests/​Option.fs Updates async/task traversal constructors.
tests/​FsToolkit.ErrorHandling.Tests/​Main.fs Registers shim and await tests.
tests/​FsToolkit.ErrorHandling.Tests/​FsToolkit.ErrorHandling.Tests.fsproj Adds .NET 10 and new test modules.
tests/​FsToolkit.ErrorHandling.Tests/​FSharpCoreShims.fs Tests shim behavior and auto-opening.
tests/​FsToolkit.ErrorHandling.Tests/​FSharpCoreShimConsumer.fs Checks consumer-facing function resolution.
tests/​FsToolkit.ErrorHandling.Tests/​Expecto.fs Updates task-test adapters to Async.Await.
tests/​FsToolkit.ErrorHandling.Tests/​Expect.fs Centralizes synchronous task assertions.
tests/​FsToolkit.ErrorHandling.Tests/​BackgroundTaskValueOptionCE.fs Migrates background value-option test inputs.
tests/​FsToolkit.ErrorHandling.Tests/​BackgroundTaskResultCE.fs Migrates background result test inputs.
tests/​FsToolkit.ErrorHandling.Tests/​BackgroundTaskOptionCE.fs Migrates background option test inputs.
tests/​FsToolkit.ErrorHandling.Tests/​AsyncValidation.fs Uses Async.result in validation tests.
tests/​FsToolkit.ErrorHandling.Tests/​AsyncResultOptionCE.fs Migrates constructors and test names.
tests/​FsToolkit.ErrorHandling.Tests/​AsyncResultCE.fs Migrates constructors and removes an unused helper.
tests/​FsToolkit.ErrorHandling.Tests/​AsyncResult.fs Migrates exception-handling tests to catchWith.
tests/​FsToolkit.ErrorHandling.Tests/​AsyncOptionCE.fs Migrates constructors and removes an unused helper.
tests/​FsToolkit.ErrorHandling.Tests/​AsyncOption.fs Updates option constructors and test naming.
tests/​FsToolkit.ErrorHandling.Tests/​Async.fs Tests synchronization-context preservation.
tests/​FsToolkit.ErrorHandling.JobResult.Tests/​JobResultCE.fs Migrates job-expression test inputs.
tests/​FsToolkit.ErrorHandling.JobResult.Tests/​JobResult.fs Tests catchWith and Job.result.
tests/​FsToolkit.ErrorHandling.JobResult.Tests/​JobOptionCE.fs Updates constructors and inference checks.
tests/​FsToolkit.ErrorHandling.JobResult.Tests/​JobOption.fs Uses JobOption.some and none.
tests/​FsToolkit.ErrorHandling.JobResult.Tests/​FsToolkit.ErrorHandling.JobResult.Tests.fsproj Adds .NET 10 testing.
tests/​FsToolkit.ErrorHandling.IcedTasks.Tests/​ValueTaskValueOptionCE.fs Migrates task/value-task expression inputs.
tests/​FsToolkit.ErrorHandling.IcedTasks.Tests/​FsToolkit.ErrorHandling.IcedTasks.Tests.fsproj Adds .NET 10 and nullness suppression.
tests/​FsToolkit.ErrorHandling.IcedTasks.Tests/​CancellableValueTaskResultCE.fs Updates constructors and awaiting.
tests/​FsToolkit.ErrorHandling.IcedTasks.Tests/​CancellableValueTaskOptionCE.fs Updates constructors and awaiting.
tests/​FsToolkit.ErrorHandling.IcedTasks.Tests/​CancellableTaskValidationCE.fs Migrates constructors and async interop tests.
tests/​FsToolkit.ErrorHandling.IcedTasks.Tests/​CancellableTaskResultCE.fs Migrates constructors and async interop tests.
tests/​FsToolkit.ErrorHandling.IcedTasks.Tests/​CancellableTaskOptionCE.fs Migrates constructors and async interop tests.
tests/​FsToolkit.ErrorHandling.AsyncSeq.Tests/​FsToolkit.ErrorHandling.AsyncSeq.Tests.fsproj Adds .NET 10 testing.
tests/​Directory.Build.props Selects FSharp.Core 11 for .NET 10 tests.
src/​FsToolkit.ErrorHandling/​Validation.fs Adds the errors constructor.
src/​FsToolkit.ErrorHandling/​TaskValueOptionCE.fs Updates constructors and async-source conversion.
src/​FsToolkit.ErrorHandling/​TaskValueOption.fs Updates constructors and simplifies binding.
src/​FsToolkit.ErrorHandling/​TaskValidationCE.fs Updates constructors and async-source conversion.
src/​FsToolkit.ErrorHandling/​TaskValidation.fs Uses Task.result for lifting.
src/​FsToolkit.ErrorHandling/​TaskResultOptionCE.fs Simplifies async-source conversion.
src/​FsToolkit.ErrorHandling/​TaskResultOption.fs Uses Task.result for lifting.
src/​FsToolkit.ErrorHandling/​TaskResultCE.fs Updates constructors and async-source conversion.
src/​FsToolkit.ErrorHandling/​TaskResult.fs Adds catchWith and deprecated compatibility APIs.
src/​FsToolkit.ErrorHandling/​TaskOptionCE.fs Updates constructors and async-source conversion.
src/​FsToolkit.ErrorHandling/​TaskOption.fs Updates option-task constructors.
src/​FsToolkit.ErrorHandling/​Task.fs Removes overlapping helpers and deprecates singleton.
src/​FsToolkit.ErrorHandling/​ParallelAsyncValidationCE.fs Adopts newer await and result helpers.
src/​FsToolkit.ErrorHandling/​ParallelAsyncResultCE.fs Adopts newer await and result helpers.
src/​FsToolkit.ErrorHandling/​List.fs Updates async accumulator construction.
src/​FsToolkit.ErrorHandling/​FsToolkit.ErrorHandling.fsproj Adds .NET 10 and shim compilation entries.
src/​FsToolkit.ErrorHandling/​AsyncValidationCE.fs Updates task awaiting and constructors.
src/​FsToolkit.ErrorHandling/​AsyncValidation.fs Uses Async.result for lifting.
src/​FsToolkit.ErrorHandling/​AsyncResultOptionCE.fs Updates task-source awaiting.
src/​FsToolkit.ErrorHandling/​AsyncResultOption.fs Uses Async.result for lifting.
src/​FsToolkit.ErrorHandling/​AsyncResultCE.fs Updates task awaiting and constructors.
src/​FsToolkit.ErrorHandling/​AsyncResult.fs Adds catchWith and deprecates conversion helpers.
src/​FsToolkit.ErrorHandling/​AsyncOptionCE.fs Updates task awaiting and constructors.
src/​FsToolkit.ErrorHandling/​AsyncOption.fs Updates constructors and documentation examples.
src/​FsToolkit.ErrorHandling.JobResult/​List.fs Uses Job.result during traversal.
src/​FsToolkit.ErrorHandling.JobResult/​JobResult.fs Adds fromAsync, catchWith, and compatibility aliases.
src/​FsToolkit.ErrorHandling.JobResult/​JobOptionCE.fs Uses Job.result for option sources.
src/​FsToolkit.ErrorHandling.JobResult/​JobOption.fs Adds some/none and deprecates singleton.
src/​FsToolkit.ErrorHandling.JobResult/​Job.fs Deprecates singleton in favor of result.
src/​FsToolkit.ErrorHandling.JobResult/​FsToolkit.ErrorHandling.JobResult.fsproj Adds .NET 10 targeting.
src/​FsToolkit.ErrorHandling.IcedTasks/​ValueTaskValueOptionCE.fs Updates async and task-source conversions.
src/​FsToolkit.ErrorHandling.IcedTasks/​FsToolkit.ErrorHandling.IcedTasks.fsproj Adds .NET 10 targeting.
src/​FsToolkit.ErrorHandling.IcedTasks/​CancellableValueTaskResultCE.fs Adds result and updates result constructors.
src/​FsToolkit.ErrorHandling.IcedTasks/​CancellableValueTaskOptionCE.fs Updates interop helpers and option constructors.
src/​FsToolkit.ErrorHandling.IcedTasks/​CancellableTaskValidationCE.fs Adds constructors and updates async interop.
src/​FsToolkit.ErrorHandling.IcedTasks/​CancellableTaskResultCE.fs Adds result, ok, and error constructors.
src/​FsToolkit.ErrorHandling.IcedTasks/​CancellableTaskResultBuilderBase.fs Updates cancellable task/async interoperability.
src/​FsToolkit.ErrorHandling.IcedTasks/​CancellableTaskOptionBuilderBase.fs Updates interoperability and source adapters.
src/​FsToolkit.ErrorHandling.AsyncSeq/​FsToolkit.ErrorHandling.AsyncSeq.fsproj Adds .NET 10 targeting.
src/​Directory.Build.props Selects FSharp.Core 11 for .NET 10.
RELEASE_NOTES.md Documents API migrations and behavior changes.
playground.fsx Updates experimental API names and syntax.
gitbook/​taskValueOption/​bind.md Uses TaskValueOption.none in the example.
gitbook/​taskResult/​ofTask.md Removes deprecated conversion documentation.
gitbook/​taskResult/​ofResult.md Removes deprecated conversion documentation.
gitbook/​taskResult/​ofCatchTask.md Removes deprecated conversion documentation.
gitbook/​taskResult/​ofAsync.md Removes deprecated conversion documentation.
gitbook/​taskResult/​catchWith.md Documents the renamed exception-handling API.
gitbook/​taskOption/​zip.md Uses TaskOption.none in the example.
gitbook/​task/​zip.md Uses Task.result in examples.
gitbook/​task/​mapV.md Uses ValueTask.result in the example.
gitbook/​task/​map.md Uses Task.result in the example.
gitbook/​task/​catch.md Removes obsolete Choice-based documentation.
gitbook/​task/​apply.md Uses Task.result in the example.
gitbook/​SUMMARY.md Updates navigation for renamed and removed APIs.
gitbook/​result/​others.md Updates task construction and removes valueOr.
gitbook/​req/​functions.md Corrects the Req.none example.
gitbook/​option/​sequenceTask.md Uses Task.result in the example.
gitbook/​option/​sequenceAsync.md Uses Async.result in the example.
gitbook/​option/​map3.md Removes redundant Core-function documentation.
gitbook/​option/​map2.md Removes redundant Core-function documentation.
gitbook/​option/​map.md Removes redundant Core-function documentation.
gitbook/​option/​bind.md Removes redundant Core-function documentation.
gitbook/​list/​sequenceJobResultM.md Uses JobResult.ok in examples.
gitbook/​list/​sequenceJobResultA.md Uses JobResult.ok in examples.
gitbook/​jobResultOption/​map.md Uses JobResult.ok in the example.
gitbook/​jobResult/​zipError.md Uses JobResult.ok in the example.
gitbook/​jobResult/​zip.md Uses JobResult.ok in examples.
gitbook/​jobResult/​ofResult.md Removes deprecated conversion documentation.
gitbook/​jobResult/​ofJob.md Removes deprecated conversion documentation.
gitbook/​jobResult/​fromAsync.md Documents the renamed async-conversion API.
gitbook/​jobResult/​catchWith.md Documents the renamed exception-handling API.
gitbook/​jobOption/​map.md Uses Job.result in the example.
gitbook/​jobOption/​eitherBind.md Corrects the signature and callback examples.
gitbook/​jobOption/​apply.md Uses specialized option constructors.
gitbook/​job/​zip.md Uses Job.result in examples.
gitbook/​job/​result.md Documents the result constructor name.
gitbook/​job/​apply.md Uses Job.result in the example.
gitbook/​cancellableValueTaskResult/​zip.md Uses ok constructors in examples.
gitbook/​cancellableValueTaskResult/​map.md Uses the renamed success constructor.
gitbook/​cancellableValueTaskResult/​apply.md Uses the renamed success constructor.
gitbook/​cancellableTaskValidation/​orElse.md Uses ok constructors in examples.
gitbook/​cancellableTaskValidation/​ok.md Documents the renamed success constructor.
gitbook/​cancellableTaskResult/​ok.md Documents the renamed success constructor.
gitbook/​cancellableTaskResult/​ofAsync.md Removes obsolete conversion documentation.
gitbook/​cancellableTaskOption/​zip.md Uses none in the example.
gitbook/​asyncResult/​ofTaskAction.md Removes deprecated conversion documentation.
gitbook/​asyncResult/​ofTask.md Removes deprecated conversion documentation.
gitbook/​asyncResult/​ofResult.md Removes deprecated conversion documentation.
gitbook/​asyncResult/​ofAsync.md Removes deprecated conversion documentation.
gitbook/​asyncResult/​catchWith.md Documents the renamed exception-handling API.
gitbook/​asyncOption/​orElseFunctions.md Updates constructors and fallback examples.
gitbook/​asyncOption/​map.md Uses AsyncOption.none in the example.
gitbook/​asyncOption/​bind.md Uses AsyncOption.none in the example.
gitbook/​asyncOption/​apply.md Uses AsyncOption.none in examples.
Directory.Build.props Extends language/nullability settings to newer targets.
benchmarks/​benchmarks.fsproj Adds .NET 10 and list-traversal benchmarks.
benchmarks/​Benchmarks.fs Migrates benchmark helper names.
benchmarks/​AsyncResultCE.fs Uses Async.result in benchmark expressions.
AGENTS.md Documents format-check cleanup behavior.
.fantomasignore Excludes the async shim from formatting.

Review effort: Balanced

Comment thread benchmarks/benchmarks.fsproj Resolved
([<InlineIfLambda>] handler: exn -> 'error)
(x: Task<Result<'ok, 'error>>)
: Task<Result<'ok, 'error>> =
Task.catchWith (fun ex -> Error(handler ex)) x

@bartelink bartelink Nov 9, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Maybe

@bartelink
bartelink force-pushed the list-traverse-perf branch 3 times, most recently from d5006b7 to d0e91d2 Compare November 8, 2026 04:48
@bartelink

bartelink commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor Author

@TheAngryByrd TLDR - I've done ArrayCollector for Seq and that makes lots of things ok in general wrt implementing list in terms of Seq impls

However it does seem like a full explict impl is worth it based on initial. benchmarks (after I put in the mutable, that is 😊

Note I upped the warning level as FS0052 an be very valuable when you're not paying attention...

Will post the numbers later.

Once I have a high level OK on this, I'll be able to put in the candidates into List.fs and will then follow up with run results for the Async ones (my sense is that a bespoke/longhand algorithm makes more sense there as being able to handle common cases of 0/1 item well can spare allocations in Async that don't matter for non-Async)

@bartelink
bartelink force-pushed the list-traverse-perf branch 14 times, most recently from a3bad85 to 7389c6f Compare November 9, 2026 02:46
@TheAngryByrd

Copy link
Copy Markdown
Collaborator

@bartelink Thanks for this. Go ahead with the explicit ListCollector implementations (your traverseResultA2 / traverseResultM2) for List. Keep the ArrayCollector change in Seq.

Before merge:

  1. Rebase onto master. Rebase impl, docs and surface API on FSharp.Core v 11 APIs #373 merged as a squash commit.
  2. Post the benchmark numbers for the current, 1, and 2 variants. Include sizes 0, 1, 10, 100, and 1000, with and without errors.
  3. Add tests for these behaviors:
    • The order of oks and errors.
    • traverseResultM stops calling f after the first error.
    • traverseResultA calls f on every element.
    • A start state of Error.
  4. Move the release notes entry out of 6.0.0-beta004 because that version is already released. Record that the public Seq.traverse*' state parameters now accept #seq<_>.
  5. Remove the duplicate FS3513 NoWarn lines in src/FsToolkit.ErrorHandling/FsToolkit.ErrorHandling.fsproj.

Keep the WarningLevel 5 / WarnOn 52 change. FS0052 catches a ListCollector bound without mutable.

For the Async variants, I agree with the separate implementation. Please put them in a follow-up PR so that this PR stays small.

@bartelink
bartelink force-pushed the list-traverse-perf branch 8 times, most recently from b5bcc65 to 6097dd7 Compare November 21, 2026 15:06
@bartelink

bartelink commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

@TheAngryByrd Turns out there was a win to be had on the Array side too:

  • employing ArrayCollector for the errors (if you are not pre-allocating it grows better than a default ResizeArray, though that's arguably a bug)
  • special casing zero inputs returning shared Array.empty

Will re-post a final clean run of these on AC power, but pretty sure this 99% run is in sync with the final code.

Have removed AsyncResult variants (might do them in a future PR), so I don't believe there's anything extraneous in this PR (though I've yet to do a final read-through)

In the following, the *1 variants are the one that I've applied in the PR (the unsufficed is a literal copy of the original code). The *2 ones have some interesting wins and tradeoffs and might be mildly interesting for you but are pretty easy to say no to as an overall proposition

BenchmarkDotNet v0.16.0-preview.2, macOS Tahoe 26.6.2 (25G83) [Darwin 25.6.0]
Apple M3 Max, 1 CPU, 14 logical and 14 physical cores
Memory: 36 GB Total, 1.21 GB Available
.NET SDK 11.0.100
[Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), Arm64 RyuJIT armv8.0-a DEBUG
DefaultJob : .NET 11.0.0 (11.0.0, 11.0.26.53018), Arm64 RyuJIT armv8.0-a

Method Size HasErrors Mean Error StdDev P80 P95 Ratio RatioSD Gen0 Gen1 Allocated Alloc Ratio
TraverseResultA 0 False 4.1632 ns 0.0176 ns 0.0165 ns 4.1752 ns 4.1881 ns baseline 0.0038 - 32 B
TraverseResultA1 0 False 1.8894 ns 0.0173 ns 0.0162 ns 1.9062 ns 1.9136 ns 2.20x faster 0.02x - - - NA
TraverseResultA2 0 False 0.6668 ns 0.0008 ns 0.0008 ns 0.6672 ns 0.6679 ns 6.24x faster 0.02x - - - NA
TraverseResultA 0 True 4.1814 ns 0.0092 ns 0.0077 ns 4.1880 ns 4.1925 ns baseline 0.0038 - 32 B
TraverseResultA1 0 True 1.9304 ns 0.0200 ns 0.0187 ns 1.9450 ns 1.9615 ns 2.17x faster 0.02x - - - NA
TraverseResultA2 0 True 0.6664 ns 0.0004 ns 0.0004 ns 0.6667 ns 0.6669 ns 6.27x faster 0.01x - - - NA
TraverseResultA 1 False 12.3665 ns 0.0203 ns 0.0170 ns 12.3757 ns 12.3905 ns baseline 0.0115 0.0000 96 B
TraverseResultA1 1 False 12.5759 ns 0.0383 ns 0.0320 ns 12.5983 ns 12.6203 ns 1.02x slower 0.00x 0.0115 0.0000 96 B 1.00x more
TraverseResultA2 1 False 8.5652 ns 0.0582 ns 0.0516 ns 8.6049 ns 8.6569 ns 1.44x faster 0.01x 0.0038 - 32 B 3.00x less
TraverseResultA 1 True 22.3789 ns 0.0401 ns 0.0376 ns 22.4033 ns 22.4339 ns baseline 0.0229 0.0000 192 B
TraverseResultA1 1 True 11.0664 ns 0.0195 ns 0.0183 ns 11.0844 ns 11.0893 ns 2.02x faster 0.00x 0.0115 0.0000 96 B 2.00x less
TraverseResultA2 1 True 7.7731 ns 0.0138 ns 0.0122 ns 7.7804 ns 7.7912 ns 2.88x faster 0.01x 0.0038 - 32 B 6.00x less
TraverseResultA 2 False 13.0038 ns 0.0468 ns 0.0438 ns 13.0354 ns 13.0843 ns baseline 0.0115 0.0000 96 B
TraverseResultA1 2 False 13.2263 ns 0.0149 ns 0.0132 ns 13.2343 ns 13.2379 ns 1.02x slower 0.00x 0.0115 0.0000 96 B 1.00x more
TraverseResultA2 2 False 10.1978 ns 0.0193 ns 0.0181 ns 10.2109 ns 10.2243 ns 1.28x faster 0.00x 0.0038 - 32 B 3.00x less
TraverseResultA 2 True 24.1455 ns 0.0789 ns 0.0738 ns 24.2077 ns 24.2649 ns baseline 0.0229 0.0000 192 B
TraverseResultA1 2 True 11.9558 ns 0.0128 ns 0.0107 ns 11.9629 ns 11.9728 ns 2.02x faster 0.01x 0.0115 0.0000 96 B 2.00x less
TraverseResultA2 2 True 8.7949 ns 0.0116 ns 0.0103 ns 8.8046 ns 8.8069 ns 2.75x faster 0.01x 0.0038 - 32 B 6.00x less
TraverseResultA 3 False 14.9493 ns 0.1635 ns 0.1529 ns 15.1043 ns 15.1950 ns baseline 0.0134 0.0000 112 B
TraverseResultA1 3 False 14.9062 ns 0.0159 ns 0.0149 ns 14.9159 ns 14.9246 ns 1.00x faster 0.01x 0.0134 0.0000 112 B 1.00x more
TraverseResultA2 3 False 16.2427 ns 0.1039 ns 0.0921 ns 16.2851 ns 16.3147 ns 1.09x slower 0.01x 0.0134 0.0000 112 B 1.00x more
TraverseResultA 3 True 25.1486 ns 0.0496 ns 0.0439 ns 25.1725 ns 25.2208 ns baseline 0.0239 0.0000 200 B
TraverseResultA1 3 True 13.3993 ns 0.0293 ns 0.0274 ns 13.4241 ns 13.4437 ns 1.88x faster 0.00x 0.0124 0.0000 104 B 1.92x less
TraverseResultA2 3 True 14.2762 ns 0.0436 ns 0.0408 ns 14.3092 ns 14.3469 ns 1.76x faster 0.01x 0.0124 0.0000 104 B 1.92x less
TraverseResultA 10 False 31.0488 ns 0.0612 ns 0.0511 ns 31.0881 ns 31.1140 ns baseline 0.0191 0.0001 160 B
TraverseResultA1 10 False 22.0798 ns 0.0408 ns 0.0341 ns 22.1111 ns 22.1317 ns 1.41x faster 0.00x 0.0191 0.0001 160 B 1.00x more
TraverseResultA2 10 False 23.5127 ns 0.0485 ns 0.0430 ns 23.5451 ns 23.5680 ns 1.32x faster 0.00x 0.0191 0.0001 160 B 1.00x more
TraverseResultA 10 True 30.6908 ns 0.1878 ns 0.1569 ns 30.7984 ns 30.9209 ns baseline 0.0268 0.0001 224 B
TraverseResultA1 10 True 17.9214 ns 0.0247 ns 0.0219 ns 17.9395 ns 17.9574 ns 1.71x faster 0.01x 0.0153 0.0000 128 B 1.75x less
TraverseResultA2 10 True 19.0236 ns 0.0458 ns 0.0406 ns 19.0523 ns 19.0786 ns 1.61x faster 0.01x 0.0153 0.0000 128 B 1.75x less
TraverseResultA 100 False 245.8588 ns 1.2923 ns 1.2088 ns 247.1466 ns 247.8790 ns baseline 0.1049 0.0024 880 B
TraverseResultA1 100 False 123.2464 ns 0.3690 ns 0.3081 ns 123.3979 ns 123.6886 ns 1.99x faster 0.01x 0.1051 0.0026 880 B 1.00x more
TraverseResultA2 100 False 131.3936 ns 0.3497 ns 0.3271 ns 131.5452 ns 131.9817 ns 1.87x faster 0.01x 0.1051 0.0026 880 B 1.00x more
TraverseResultA 100 True 141.7914 ns 0.9384 ns 0.8777 ns 142.3682 ns 143.2545 ns baseline 0.0696 - 584 B
TraverseResultA1 100 True 84.0885 ns 0.0732 ns 0.0649 ns 84.1272 ns 84.1726 ns 1.69x faster 0.01x 0.0583 - 488 B 1.20x less
TraverseResultA2 100 True 84.7341 ns 0.1117 ns 0.1044 ns 84.8216 ns 84.8680 ns 1.67x faster 0.01x 0.0583 - 488 B 1.20x less
TraverseResultA 1000 False 2,228.9904 ns 15.2998 ns 14.3114 ns 2,239.9323 ns 2,250.9874 ns baseline 0.9651 0.2403 8080 B
TraverseResultA1 1000 False 1,122.5119 ns 1.4786 ns 1.2347 ns 1,123.5009 ns 1,124.2081 ns 1.99x faster 0.01x 0.9651 0.2403 8080 B 1.00x more
TraverseResultA2 1000 False 1,176.6261 ns 4.1979 ns 3.5055 ns 1,179.5274 ns 1,182.7943 ns 1.89x faster 0.01x 0.9651 0.2403 8080 B 1.00x more
TraverseResultA 1000 True 1,302.6417 ns 9.4776 ns 8.8654 ns 1,309.4611 ns 1,314.1460 ns baseline 0.4997 0.0076 4184 B
TraverseResultA1 1000 True 769.5182 ns 1.7103 ns 1.4282 ns 770.5887 ns 771.4511 ns 1.69x faster 0.01x 0.4883 0.0029 4088 B 1.02x less
TraverseResultA2 1000 True 760.6788 ns 5.8326 ns 4.5537 ns 764.5182 ns 765.4367 ns 1.71x faster 0.01x 0.4883 0.0029 4088 B 1.02x less
TraverseResultM 0 False 3.9583 ns 0.0267 ns 0.0250 ns 3.9854 ns 3.9917 ns baseline 0.0038 - 32 B
TraverseResultM1 0 False 1.6775 ns 0.0327 ns 0.0469 ns 1.7382 ns 1.7583 ns 2.36x faster 0.07x - - - NA
TraverseResultM2 0 False 2.6235 ns 0.0016 ns 0.0015 ns 2.6248 ns 2.6255 ns 1.51x faster 0.01x - - - NA
TraverseResultM 0 True 3.7333 ns 0.0734 ns 0.0753 ns 3.8060 ns 3.8264 ns baseline 0.0038 - 32 B
TraverseResultM1 0 True 1.6437 ns 0.0011 ns 0.0008 ns 1.6444 ns 1.6446 ns 2.27x faster 0.04x - - - NA
TraverseResultM2 0 True 2.6252 ns 0.0016 ns 0.0014 ns 2.6262 ns 2.6273 ns 1.42x faster 0.03x - - - NA
TraverseResultM 1 False 11.0179 ns 0.0222 ns 0.0186 ns 11.0310 ns 11.0468 ns baseline 0.0115 - 96 B
TraverseResultM1 1 False 11.5785 ns 0.0854 ns 0.0799 ns 11.6308 ns 11.6584 ns 1.05x slower 0.01x 0.0115 - 96 B 1.00x more
TraverseResultM2 1 False 6.8819 ns 0.0479 ns 0.0400 ns 6.9038 ns 6.9504 ns 1.60x faster 0.01x 0.0038 - 32 B 3.00x less
TraverseResultM 1 True 6.6740 ns 0.0217 ns 0.0203 ns 6.6918 ns 6.6995 ns baseline 0.0077 - 64 B
TraverseResultM1 1 True 6.7004 ns 0.0623 ns 0.0552 ns 6.7536 ns 6.7720 ns 1.00x slower 0.01x 0.0077 - 64 B 1.00x more
TraverseResultM2 1 True 4.5852 ns 0.0346 ns 0.0324 ns 4.6136 ns 4.6457 ns 1.46x faster 0.01x - - - NA
TraverseResultM 2 False 12.1847 ns 0.0523 ns 0.0489 ns 12.2159 ns 12.2340 ns baseline 0.0115 - 96 B
TraverseResultM1 2 False 11.6892 ns 0.1139 ns 0.1009 ns 11.7472 ns 11.8702 ns 1.04x faster 0.01x 0.0115 - 96 B 1.00x more
TraverseResultM2 2 False 7.0886 ns 0.0070 ns 0.0058 ns 7.0936 ns 7.0956 ns 1.72x faster 0.01x 0.0038 - 32 B 3.00x less
TraverseResultM 2 True 8.0530 ns 0.0657 ns 0.0615 ns 8.0993 ns 8.1448 ns baseline 0.0076 - 64 B
TraverseResultM1 2 True 8.1816 ns 0.0466 ns 0.0436 ns 8.2205 ns 8.2337 ns 1.02x slower 0.01x 0.0076 - 64 B 1.00x more
TraverseResultM2 2 True 3.8294 ns 0.0018 ns 0.0014 ns 3.8305 ns 3.8309 ns 2.10x faster 0.02x - - - NA
TraverseResultM 3 False 14.1792 ns 0.1529 ns 0.1355 ns 14.2783 ns 14.3286 ns baseline 0.0134 - 112 B
TraverseResultM1 3 False 13.3440 ns 0.0748 ns 0.0699 ns 13.4020 ns 13.4173 ns 1.06x faster 0.01x 0.0134 - 112 B 1.00x more
TraverseResultM2 3 False 14.0424 ns 0.0517 ns 0.0458 ns 14.0820 ns 14.0980 ns 1.01x faster 0.01x 0.0134 - 112 B 1.00x more
TraverseResultM 3 True 8.6624 ns 0.0345 ns 0.0323 ns 8.6890 ns 8.6999 ns baseline 0.0086 - 72 B
TraverseResultM1 3 True 8.6978 ns 0.0390 ns 0.0346 ns 8.7241 ns 8.7472 ns 1.00x slower 0.01x 0.0086 - 72 B 1.00x more
TraverseResultM2 3 True 8.4809 ns 0.0214 ns 0.0178 ns 8.4905 ns 8.5020 ns 1.02x faster 0.00x 0.0086 - 72 B 1.00x more
TraverseResultM 10 False 21.7262 ns 0.0357 ns 0.0298 ns 21.7510 ns 21.7661 ns baseline 0.0191 - 160 B
TraverseResultM1 10 False 20.8916 ns 0.0727 ns 0.0680 ns 20.9530 ns 20.9653 ns 1.04x faster 0.00x 0.0191 - 160 B 1.00x more
TraverseResultM2 10 False 21.6907 ns 0.1175 ns 0.1099 ns 21.7928 ns 21.8614 ns 1.00x faster 0.01x 0.0191 - 160 B 1.00x more
TraverseResultM 10 True 12.7552 ns 0.0591 ns 0.0524 ns 12.8144 ns 12.8312 ns baseline 0.0115 - 96 B
TraverseResultM1 10 True 12.2114 ns 0.0229 ns 0.0179 ns 12.2269 ns 12.2285 ns 1.04x faster 0.00x 0.0115 - 96 B 1.00x more
TraverseResultM2 10 True 12.7329 ns 0.0356 ns 0.0333 ns 12.7606 ns 12.7757 ns 1.00x faster 0.00x 0.0115 - 96 B 1.00x more
TraverseResultM 100 False 129.1415 ns 0.4938 ns 0.4619 ns 129.5223 ns 129.7087 ns baseline 0.1051 - 880 B
TraverseResultM1 100 False 121.3840 ns 0.3861 ns 0.3611 ns 121.6772 ns 121.7277 ns 1.06x faster 0.00x 0.1051 - 880 B 1.00x more
TraverseResultM2 100 False 123.0188 ns 0.3332 ns 0.3117 ns 123.2313 ns 123.4560 ns 1.05x faster 0.00x 0.1051 - 880 B 1.00x more
TraverseResultM 100 True 54.6380 ns 0.2736 ns 0.2559 ns 54.9172 ns 55.0361 ns baseline 0.0545 - 456 B
TraverseResultM1 100 True 56.5485 ns 0.1742 ns 0.1544 ns 56.6952 ns 56.7312 ns 1.03x slower 0.01x 0.0545 - 456 B 1.00x more
TraverseResultM2 100 True 51.9567 ns 0.1639 ns 0.1369 ns 52.0446 ns 52.1784 ns 1.05x faster 0.01x 0.0545 - 456 B 1.00x more
TraverseResultM 1000 False 1,111.4741 ns 1.9204 ns 1.7024 ns 1,112.5856 ns 1,113.7847 ns baseline 0.9651 0.0057 8080 B
TraverseResultM1 1000 False 1,109.3643 ns 2.1576 ns 2.0182 ns 1,110.4488 ns 1,112.5108 ns 1.00x faster 0.00x 0.9651 0.0057 8080 B 1.00x more
TraverseResultM2 1000 False 1,122.4880 ns 6.6171 ns 6.1897 ns 1,126.5723 ns 1,129.4219 ns 1.01x slower 0.01x 0.9651 0.0057 8080 B 1.00x more
TraverseResultM 1000 True 492.0660 ns 1.0370 ns 0.9700 ns 492.6355 ns 493.8374 ns baseline 0.4845 - 4056 B
TraverseResultM1 1000 True 506.6808 ns 1.6633 ns 1.5559 ns 507.7133 ns 508.8013 ns 1.03x slower 0.00x 0.4845 - 4056 B 1.00x more
TraverseResultM2 1000 True 520.0735 ns 3.6320 ns 3.3974 ns 522.8677 ns 525.6413 ns 1.06x slower 0.01x 0.4845 - 4056 B 1.00x more

@bartelink bartelink changed the title List performance work perf((List|Array).traverseResult[AM]): Speed/memory optimizations Nov 24, 2026
| Error e ->
#if FABLE_COMPILER
if ok then
let remaining =

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm sure Fable won't benefit from this, but IMO in real life, in error cases where there are lots of failures, preallocating in one block is going to be better than having it resize all the time given ultimately there's a ToArray that will drain it all?

Anyway, the ArrayCollector wins the benchmark across all sizes hands down so what do I know

@bartelink bartelink changed the title perf((List|Array).traverseResult[AM]): Speed/memory optimizations perf((Seq|List|Array).traverseResult[AM]): Speed/memory optimizations Nov 24, 2026
@TheAngryByrd
TheAngryByrd requested a balanced review from Copilot November 24, 2026 16:14

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.

🟡 Changes recommended

The optimization unintentionally exposes the stateful List worker as a public API; retaining private prevents its accumulator interface from becoming part of the supported library API.

2 open issues
1 resolved since last review

Review effort: Balanced

Comment thread src/FsToolkit.ErrorHandling/List.fs Outdated
module List =

let rec private traverseResultM' (state: Result<_, _>) (f: _ -> Result<_, _>) xs =
let traverseResultM'

@bartelink bartelink Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hm had missed the fact that it was private in the first instance
Noticing now that it does not have inline/InlineIfLambda like the Array counterpart. Might have to run the numbers on that and potentially do a follow-up

@bartelink
bartelink force-pushed the list-traverse-perf branch 2 times, most recently from f5789d9 to b72668d Compare November 24, 2026 18:08
@TheAngryByrd
TheAngryByrd merged commit 29b1bdf into demystifyfp:master Nov 24, 2026
26 checks passed
@TheAngryByrd

Copy link
Copy Markdown
Collaborator

Thanks!!!

@bartelink

Copy link
Copy Markdown
Contributor Author

Thank you - by the way for the List traverseResultM' and traverseResultM I tried the combos of inlining the inner function, inlining the outer etc but it doesnt dramatically improve perf (and the allocations stay identical).

That said, I'm sure there are cases where an InlineIfLambda can be transformative - The testbed is there now for for people to see for themselves. If there does turn out to be a use case for it, then I guess the lower level function could be made inline with f as InlineIfLambda in the future...

@bartelink
bartelink deleted the list-traverse-perf branch November 25, 2026 15:16
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.

Use ListCollector on List operations to improve perf

3 participants