Conversation
src/Result/Extra.elm
Outdated
| List.foldr (Result.map2 (::)) (Ok []) | ||
| combine list = | ||
| combineHelp list [] | ||
| |> Result.map List.reverse |
There was a problem hiding this comment.
If you move the List.reverse into the [] -> branch of combineHelp then you don't need to Result.map over it or pipe. Will probably only add 0.000001% better performance 😅
There was a problem hiding this comment.
Oh, you'd be surprised... It's not the same kind of improvement, but for small lists, it's a noticeable improvement (compared to the version I already proposed).
https://ellie-app.com/fDcGZNzBFzGa1
You're absolutely right, and I don't know why I didn't do this in the first place (well, probably because adding List.reverse was something I realized I needed to do a lot later 🤦).
I updated the PR 👍
I didn't benchmark on the Err case but it should also improve performance 👍
|
Any chance of looking into merging this? 🙂 |



This improves the performance of the
combinefunction.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 anOkuntil 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
combineMapdepends oncombine, this will also improve its performance, which I didn't spend time benchmarking.