Skip to content

Scala.js: Emit js.NewArray IR nodes when possible. #22446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 27, 2025

Conversation

sjrd
Copy link
Member

@sjrd sjrd commented Jan 24, 2025

Although there is a correct implementation of sr.Arrays.newArray, it is not efficient when creating 1-dimensional arrays. The JVM backend special-cases it to emit newarray bytecode instructions.

We now also special-case it in the JS backend. In the Scala.js IR however, js.NewArray only accepts a single dimension. For multiple dimensions, the right thing to do is to emit a direct call to jlr.Array.newInstance.

Although there is a *correct* implementation of `sr.Arrays.newArray`,
it is not efficient when creating 1-dimensional arrays. The JVM
backend special-cases it to emit `newarray` bytecode instructions.

We now also special-case it in the JS backend. In the Scala.js IR
however, `js.NewArray` only accepts a single dimension. For multiple
dimensions, the right thing to do is to emit a direct call to
`jlr.Array.newInstance`.
@sjrd sjrd requested a review from smarter January 27, 2025 03:07
@smarter smarter merged commit 89c20f8 into scala:main Jan 27, 2025
29 checks passed
@smarter smarter deleted the sjs-decent-newarray branch January 27, 2025 16:53
@WojciechMazur WojciechMazur added this to the 3.7.0 milestone Mar 11, 2025
@tgodzik
Copy link
Contributor

tgodzik commented Apr 3, 2025

Does make sense backporting it to LTS, which doesn't update the Scala JS versions?

@sjrd
Copy link
Member Author

sjrd commented Apr 3, 2025

Probably. The LTS uses an older version of Scala.js that had builtin support for js.NewArray with multiple dimensions. So on the LTS you could always use the // Use a js.NewArray case, and use dimsArray.elems.map(genExpr(_)) instead of genExpr(singleDim).

If you backport this one, you also need to backport #22797 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants