-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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
buffer: Preallocate array with buffer length in Buffer#toJSON #11733
Conversation
I think I'm -1 on this. See this relevant comment. It's not immediately clear what branches this change affects (negatively) performance-wise, as I'm not sure which (if any) have had the appropriate patches backported from upstream V8. |
There is actually a benchmark created in the last update of this function "benchmark/buffers/buffer-tojson.js". It could be useful to know if this change have had some performance improvement, I'll be able to get some results in a while. |
Note that the commit message does not follow commit guidelines atm — the subsystem is not present. |
Here I have some interesting results from the benchmark mentioned before showing up to 3 times more operations for the bigger buffer: master
Actual PR
|
I just checked the results on each versioned branch and master, and it does provide about the same performance or faster in each case, so LGTM. |
The commit message still needs to be fixed =). |
I'll get the commit message fixed shortly, I suppose it can be done for already pushed commits. |
subsystem: buffer Because the final array length is known, it's better to allocate its final length at initialization time to avoid future reallocations. It also adds an explicit buffer length greater than 0 comparison so it's more readable, avoids the internal ToBoolean call and follows the standard Node.js API format (as it can be checked in other similar structures where 'length > 0' is preferred over 'length')
I just fixed the commit message adding the subsystem and fixing a couple of spelling mistakes. Apart from that, I have been doing tests removing the "if(this.length > 0)... else" and i am getting exactly the same results when the buffer's length is 0. If there is no other reason why we are using this if statement other than performance, I will suggest to remove it so the code will look more polished and simple. |
Can you do that while at it? |
I just ran the benchmark without the if...else statement and the results are exactly the same so, I suppose that it doesn't affect performance.
|
@alemures ... can you look at #11733 (comment), thank you! |
@jasnell I proposed a small style change to this PR but I'm not sure if you guys agree or not. I suppose that it's better to leave as it is now that CI reported no errors with the actual code. Do you think it is still worth it the change? |
I'm good with this as is. Will get it landed. |
Because the final array length is known, it's better to allocate its final length at initialization time to avoid future reallocations. Also add an explicit buffer length greater than 0 comparison so it's more readable, avoids the internal ToBoolean call and follows the standard Node.js API format (as it can be checked in other similar structures where 'length > 0' is preferred over 'length') PR-URL: #11733 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net>
landed in 51587b2 |
Because the final array length is known, it's better to allocate its final length at initialization time to avoid future reallocations. Also add an explicit buffer length greater than 0 comparison so it's more readable, avoids the internal ToBoolean call and follows the standard Node.js API format (as it can be checked in other similar structures where 'length > 0' is preferred over 'length') PR-URL: nodejs#11733 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net>
@mscdex are you ok with the way this change landed? I'm seeing the original -1 but no sign off Also curious if this should be backported |
@MylesBorins It would need to be benchmarked first, I'm not sure the performance fix for |
Because the final array length is known, it's better to allocate its
final length at initialization time to avoid future reallocations.
It also adds an explicit buffer length greater than 0 comparison so
it's more readable, avoids the internal ToBoolean call and follows the
standard Node.js API format (as it can be checked in other similar
structures where 'length > 0' is preferred over 'length')
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
buffer