Experiment: Avoid JSArrayConstr
for Varargs to optimize the Wasm backend
#5148
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 is more like an experiment report for discuss based on the results of this change.
Currently in Scala.js, varargs call like
List(1, 2, 3)
, it is translated into the IR formjs.WrappedArray(JSArrayConstr(...))
. That requires JS interop for constructing the array and accessing its elements. Since Wasm-to-JS calls are expensive, this is undesirable for performance.This commit experiments avoiding
JSArrayConstr
for varargs. Instead, varargs are transformed into something likenew WrappedArray$ofInt(ArrayValue(1, 2, 3))
(ornew ArraySeq$ofInt(...)
on 2.13) to explore potential Wasm-specific optimizations.Note1: While reducing JS interop can improve performance on the Wasm backend, the same does not apply the JS backend. We'd need to re-optimize back to
JSArrayConstr
-based code during the Optimizer for the JS backend.Note 2: How about
WrappedArray.make
instead of directly instantiating specializedWrappedArray
? I found that runtime type checks inmake
appear to be very slow, and in some micro-benchmarks, usingmake
performed slightly worse than the originalJSArrayConstr
-based code.Unfortunately, the performance improvements were negligible.
For example, in the following code:
Both versions (with and without
JSArrayConstr
) reported similar timings of ~540000–580000 ns.Benchmarks run using
sjrd/scalajs-benchmarks/wasm
also did not show any significant performance differences.(It might be because there's not so much varargs in the benchmark)
There may still be room for further optimization in the non-
JSArrayConstr
implementation.