-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Replace push.apply with dedicated function
#12361
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
Conversation
|
Thank you for the pull request, @javagl! ✅ We can confirm we have a CLA on file for you. |
jjspace
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested with the asset you provided and did confirm it works on this branch and not in main. I did have a few comments about the new function itself though.
Co-authored-by: jjspace <8007967+jjspace@users.noreply.github.com>
… into replace-push-apply
|
@javagl Is this ready for review and potential merge again? |
|
@ggetz Fine to merge from my perspective, if the comments by @jjspace are resolved. A tangential connection to #8359 : One of the rules/transforms of
Which changes this: But... as it turned out: The An aside: I considered to review the code for cases where normal |
I think this is a good reason to be targeted about modernization pushes we do, and to not blanket apply transforms: We need to ensure that there are no performance implications when we do so.
I agree at minimum, this is out of scope with the original scope of this PR and there's no need to do it here. Unless we confirm that there is a performance implication to motivate any other use case, I think we're getting into bikeshedding territory and we should hold off. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking good now. Just 2 comments:
- Should there be a section added to our Coding Guide (or some other guide) explaining why we want to avoid
.push.apply? maybe linking back to the discussion in this PR or the issue? - Optional change but should we actually restrict usage of this function through eslint? It's possible to do that through the
no-restricted-syntaxrule with either a warning or a full error. @ggetz thoughts on this?
rules: {
"no-restricted-syntax": [
"warn",
{
// Link to issue here
selector:
"CallExpression[callee.object.property.name=push][callee.property.name=apply]",
message:
"We prefer to avoid Array.push.apply() in favor of addAllToArray()",
},
],
},|
@jjspace No strong opinion from my side, just minor thoughts: As of my comment above, one 'Lebap' modernization transform allows converting (BTW: I wasn't aware of the Maybe we should just collect some ideas in some "Possible updates for the Coding Guide" issue, to better keep track of that? (I mean, this could also include other "modernization" hints, like "Don't do |
@javagl I think this is exactly why it would be worth calling out in the coding guide somewhere. I don't think we need to add a section on modernization in general. It would be more on this as a performance topic like the original issue.
If you have other immediate ideas of things to add then it could be worth opening an issue but I think it's more likely to actually get in if we add it to a PR directly. I think something should be added alongside this change just focused on the pitfall of I also do think we should add the eslint rule but only as a warning. This will ensure it's automated and shows up in the actual code instead of relying on everyone remembering everything in the coding guide at all times. The codeblock above will achieve this, I think it can be added to the Once those changes are in I think this should be good to merge |
|
@jjspace I added that linting rule as you suggested. I tried to elaborate the rule/warning text a bit:
The spread syntax can be nice, modern, and convenient, and for something like For some potential further updates of the Coding Guide (including |
jjspace
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, sorry this took so long to get back to. Also surprised there were no merge conflicts but it looks good 😄


Fixes #12053
Makes #12176 obsolete
Description
The title of the issue is "CesiumJS should not use
push.applyfor potentially large arrays" , which bears the question of what is a "large" array. Given that the answer to that question is the same as to "How long is a piece of string?", this PR just replaces all appearances oftarget.push.apply(target, source);with
addAll(source, target);using a newly introduced
addAllfunction.This
addAllfunction is a top-level function, but marked as@privatefor now.Issue number and link
#12053
Testing plan
The case where this issue originally came up was that of a glTF with many accessors: Each accessor causes a 'promise' to be created, and these promises had been pushed to a target array with
push.apply. For many accessors, this caused aRangeError, as described in the issue.The following is a sandcastle that loads such a glTF asset, and can be used to verify that the issue no longer appears:
The following archive contains the main asset for the test:
callStackTest.zip
When using the
callStackTest_250x250.glb, then the currentmainstate causes an error. With the fixed state, it works.The archive contains a few smaller assets as well, and the sandcastle offers a very basic "benchmarking" functionality, to repeatedly load an asset. This is intended for checking whether this change has a noticable performance impact on this particular code path. This makes limited sense: There have been many places that used
push.apply. Many of them never saw anything that even remotely was a "large" array. In the specific case of the glTF models, most of the time was not spent inpush.apply, but with loading the data to begin with. But as a baseline for this case that can easily be tested (beyond some "microbenchmarks", as shown in the linked PR), I ran this test with the 120x120 asset, and there doesn't seem to be a significant performance impact.Author checklist
CONTRIBUTORS.mdCHANGES.mdwith a short summary of my change