Repository navigation
perf((Seq|List|Array).traverseResult[AM]): Speed/memory optimizations - #383
Conversation
There was a problem hiding this comment.
🟡 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 includedListTraverse.fsworkers bind mutable-struct collectors withoutmutable, 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: AddcatchWithtests for canceled input tasks andOperationCanceledException, 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 producesOk exception. Throw it inside the computation to demonstrate the documented error path. - Nit · 1 vote —
tests/FsToolkit.ErrorHandling.IcedTasks.Tests/ValueTaskValueOptionCE.fs:63: Retain aTaskinput in the return-from test so it covers theTask<'T voption>Source overload returningValueNone. - Nit · 1 vote —
tests/FsToolkit.ErrorHandling.IcedTasks.Tests/ValueTaskValueOptionCE.fs:153: UseTaskValueOption.noneto preserve Task-specificValueNonebind 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
| ([<InlineIfLambda>] handler: exn -> 'error) | ||
| (x: Task<Result<'ok, 'error>>) | ||
| : Task<Result<'ok, 'error>> = | ||
| Task.catchWith (fun ex -> Error(handler ex)) x |
d5006b7 to
d0e91d2
Compare
|
@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 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) |
a3bad85 to
7389c6f
Compare
|
@bartelink Thanks for this. Go ahead with the explicit Before merge:
Keep the For the Async variants, I agree with the separate implementation. Please put them in a follow-up PR so that this PR stays small. |
b5bcc65 to
6097dd7
Compare
fix(Warnings): Life is too short not to have WarningLevel 5
Will run and post timings before removing
6ee7730 to
7a41686
Compare
|
@TheAngryByrd Turns out there was a win to be had on the Array side too:
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 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]
|
7a41686 to
b5d0be9
Compare
| | Error e -> | ||
| #if FABLE_COMPILER | ||
| if ok then | ||
| let remaining = |
There was a problem hiding this comment.
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
c29feea to
2141094
Compare
There was a problem hiding this comment.
🟡 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
| module List = | ||
|
|
||
| let rec private traverseResultM' (state: Result<_, _>) (f: _ -> Result<_, _>) xs = | ||
| let traverseResultM' |
There was a problem hiding this comment.
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
f5789d9 to
b72668d
Compare
b72668d to
1b0252e
Compare
|
Thanks!!! |
|
Thank you - by the way for the That said, I'm sure there are cases where an |

Resolves #167