Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This improves the performance of the
combine
function.There are two main parts to this.
The first part is that this version avoids using
Result.map2
, which removes a lot of unwrapping of the intermediate accumulator. We also don't wrap the accumulator in anOk
until the very end.The second part is that whenever we encounter an
Err
, we stop and return anErr
, without continuing to iterate over the rest of the list, which removes a lot of processing time whose result is thrown away.List.fold*
functions are great, but if you want to control how you iterate over the list, recursion is the way to go.Here is a benchmark you can run: https://ellie-app.com/fCBmw4NHHBHa1
which should give you results like the following:
The performance increase of the second benchmark can be raised even further if you change the size of the length of the list and move the position of the error (which is in 2nd position out of 10 items in these benchmarks).
With the error in first position in a list of 1000 items, the performance changes to something like this:
Since
combineMap
depends oncombine
, this will also improve its performance, which I didn't spend time benchmarking.