Skip to content
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

SparseArray code generated for simple array spreads #1425

Open
DoctorGester opened this issue Mar 30, 2023 · 1 comment
Open

SparseArray code generated for simple array spreads #1425

DoctorGester opened this issue Mar 30, 2023 · 1 comment
Labels

Comments

@DoctorGester
Copy link
Contributor

[...[], ...[]]

https://typescripttolua.github.io/play#code/NoOjwXQGgAjFISA

This code uses lualib sparse arrays, even though there shouldn't be any need to, since arrays with nulls are not supported anyway. This might have a considerable impact on performance in hot loops.

@Perryvw Perryvw added bug scope: transformation Transformation of TS to Lua labels Mar 30, 2023
@Perryvw
Copy link
Member

Perryvw commented Jun 11, 2023

This is intended behavior to deal with Lua behavior when using an unpack as an array literal member before the last element.

ie:

local a = { 1, 2, 3 }
local b = { 4, 5, 6 }
return { table.unpack(a), table.unpack(b) }

Will unexpectedly return {1, 4, 5, 6}.

While this will functionally work and result in the correct result, there are two issues with this:

  • Like you said, the performance of this is questionable, there is not really a reason to use the expensive parse array push when a regular array push would do.
  • We are making an extra copy of array in your example. It is already loading the result into a temporary array to do the array pushes on, but then once it's done we unpack and put it indo another array literal again, which is not required

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

No branches or pull requests

2 participants