-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Avoid creating a holey array in makeNimstrLit for JS target #16461
Conversation
can you write a benchmark to see the performance difference? |
Yeah, I can do that later today. |
It took me longer than I was hoping as I had to ask around about some interesting behavior with the benchmark. The benchmarks consistently show that despite not creating a holey array, the newer code is slower. This is because it resizes the array as more elements are added, while the old code initializes the array to the proper size once. I would argue that the new version should be preferred as the performance penalties of creating a holey array are worth sacrificing a microsecond to resize the array. |
@pizzafox IMO the real savings will come from timotheecour#483, ie using typed array
indeed, when I run this benchmark, the code after this PR is slower; shouldn't this warrant more investigation instead of merging this? I'm not sure what you mean by just "microseconds"; if something is 1.35x slower, this slowdown can be significant depending on application. |
…#16461) * Avoid creating a holey array in makeNimstrLit * Use array index instead of push
…#16461) * Avoid creating a holey array in makeNimstrLit * Use array index instead of push
This is a subtle, but important performance improvement in the
makeNimstrLit
proc for targeting JS.The previous code would accept a string and construct an array of character codes. The way it did this was be creating an array with
new Array(n)
. This creates a holey array (a sparse array) which V8 is very bad at optimizing compared to a packed array with no holes.And later in that same page:
There are other instances where
new Array(length)
is used, which for lengths 1 and above means holey arrays. I could spend some more time going around replacing all instances of those with optimized versions if requested.