Simplified implementations of Array.foreach and Array.map improve performance by 2–3.5× #23649
2Pit
started this conversation in
Feature Requests
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi!
I've been benchmarking performance of
Array.map
andArray.foreach
in Scala and found that their implementations can be made significantly faster —map
by up to 3.5×, andforeach
by up to 2× — by simplifying the loop structure in cases where the function operates on statically known primitive types.The current implementation relies on a
match
over array types, introducing overhead and making performance less predictable in cases chained transformations. In contrast, a plain loop yields consistent scaling and significantly lower latency per element.Here’s how the current
map
method can be simplified:You can find the full analysis with plots and measurements below.
Would this kind of improvement be something you'd like to see in the standard library? I'd be happy to contribute a PR.
📊 https://github.com/2Pit/scala-benchmarks/blob/main/review/array_map_foreach/README.md
Beta Was this translation helpful? Give feedback.
All reactions