-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
src: remove pushValueToArray and setupProcessObject #24264
Conversation
Some local benchmark results:
The Intuitively the new API is doing what has to be done either in JS or C++ - it constructs an Array out of a FixedArray with all the elements readily packed inside, so it shouldn't be slower than the old way (which calls into JS), the impact seems to come from how the new approach affect what gets optimized in a hot loop which rarely happen in real word, so I am inclined to just ignore the results in For reference, something like #24125 shows significant improvement when the length of the array is long enough and the call cannot be optimized (e.g. unconditionally done in C++ land without
|
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.
Not sure about the benchmark results TBH.
src/node_http2.cc
Outdated
Local<Value> holder = Array::New(isolate); | ||
Local<Function> fn = env()->push_values_to_array_function(); | ||
Local<Value> argv[NODE_PUSH_VAL_TO_ARRAY_MAX]; | ||
std::vector<Local<Value>> origin_v; |
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.
Since you already know the capacity needed for this, how about pre-allocating it here?
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.
Good idea! Thanks for catching that
src/node_http_parser.cc
Outdated
fn->Call(env()->context(), headers, j * 2, argv).ToLocalChecked(); | ||
} | ||
} while (i < num_values_); | ||
std::vector<Local<Value>> headers_v; |
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.
Same here, how about preallocating the appropriate capacity?
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.
Now come to think of it, this one can actually be an array since its capacity cannot be bigger than 32*2, maybe that'll speed it up a bit more..
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'd trust Are the benchmark around this? 😮std::vector
. No need to over-optimize.
Instead of calling into JS from C++ to push values into an array, use the new Array::New API that takes a pointer and a length directly.
Instead of calling into JS from C++ to push values into an array, use the new Array::New API that takes a pointer and a length directly.
Instead of calling into JS from C++ to push values into an array, use the new Array::New API that takes a pointer and a length directly.
Instead of calling into JS from C++ to push values into an array, use the new Array::New API that takes a pointer and a length directly.
Instead of calling into JS from C++ to push values into an array, use the new Array::New API that takes a pointer and a length directly.
Instead of calling into JS from C++ to push values into an array, use the new Array::New API that takes a pointer and a length directly.
3cb8bf9
to
cc511fb
Compare
Instead of calling into JS from C++ to push values into an array, use the new Array::New API that takes a pointer and a length directly.
The usage of NODE_PUSH_VAL_TO_ARRAY_MAX and push_values_to_array_function has all been removed in favor of the new Array::New API that takes a C++ array. Remove the unused code.
cc511fb
to
db5e5fa
Compare
Apparently using a fixed-size array in the http parser does the trick, though the impact shown from
when n=100 (somehow accuracy is a little bit better):
Other benchmark results:
|
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.
🎉
(just have some style nits)
src/node_http2.cc
Outdated
size_t headers_size = headers.size(); | ||
std::vector<Local<Value>> headers_v(headers_size * 2); | ||
for (size_t i = 0; i < headers_size; ++i) { | ||
nghttp2_header item = headers[i]; |
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.
BTW: this is an unneeded copy
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.
Good catch! (though I realize it's originally that way :/)
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 think the compiler should be able optimize this away anyway?
src/node_http2.cc
Outdated
} | ||
|
||
Local<Value> holder = Array::New(isolate, origin_v.data(), nov); |
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.
/s/nov/origin_v.size()/
for correctness and readability
This comment has been minimized.
This comment has been minimized.
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.
Nice work!
And, to be explicit, I’m 👍 on using indexed for-loops here as well (for the reasons @joyeecheung is mentioning).
src/node_http2.cc
Outdated
size_t headers_size = headers.size(); | ||
std::vector<Local<Value>> headers_v(headers_size * 2); | ||
for (size_t i = 0; i < headers_size; ++i) { | ||
nghttp2_header item = headers[i]; |
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 think the compiler should be able optimize this away anyway?
src/node_http2.cc
Outdated
if (j > 0) | ||
fn->Call(context, holder, j, argv).ToLocalChecked(); | ||
for (size_t i = 0; i < nov; ++i) { | ||
auto entry = origin->ov[i]; |
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 know this is copy-pasted, but I think getting rid of this auto
might make things more readable :)
Interesting results: I tried using push_back:
emplace_back:
I suspect RVO is somehow not in effect with the way Fixed a few nits with const references and a few comments. CI: https://ci.nodejs.org/job/node-test-pull-request/18576/ |
Instead of calling into JS from C++ to push values into an array, use the new Array::New API that takes a pointer and a length directly. PR-URL: #24264 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
The usage of NODE_PUSH_VAL_TO_ARRAY_MAX and push_values_to_array_function has all been removed in favor of the new Array::New API that takes a C++ array. Remove the unused code. PR-URL: #24264 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
Instead of calling into JS from C++ to push values into an array, use the new Array::New API that takes a pointer and a length directly. PR-URL: #24264 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
Instead of calling into JS from C++ to push values into an array, use the new Array::New API that takes a pointer and a length directly. PR-URL: #24264 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
Instead of calling into JS from C++ to push values into an array, use the new Array::New API that takes a pointer and a length directly. PR-URL: #24264 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
Instead of calling into JS from C++ to push values into an array, use the new Array::New API that takes a pointer and a length directly. PR-URL: #24264 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
Instead of calling into JS from C++ to push values into an array, use the new Array::New API that takes a pointer and a length directly. PR-URL: #24264 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
Instead of calling into JS from C++ to push values into an array, use the new Array::New API that takes a pointer and a length directly. PR-URL: #24264 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
Instead of calling into JS from C++ to push values into an array, use the new Array::New API that takes a pointer and a length directly. PR-URL: #24264 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
The usage of NODE_PUSH_VAL_TO_ARRAY_MAX and push_values_to_array_function has all been removed in favor of the new Array::New API that takes a C++ array. Remove the unused code. PR-URL: #24264 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
Instead of calling into JS from C++ to push values into an array, use the new Array::New API that takes a pointer and a length directly. PR-URL: nodejs#24264 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
Instead of calling into JS from C++ to push values into an array, use the new Array::New API that takes a pointer and a length directly. PR-URL: nodejs#24264 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
Instead of calling into JS from C++ to push values into an array, use the new Array::New API that takes a pointer and a length directly. PR-URL: nodejs#24264 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
Instead of calling into JS from C++ to push values into an array, use the new Array::New API that takes a pointer and a length directly. PR-URL: nodejs#24264 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
Instead of calling into JS from C++ to push values into an array, use the new Array::New API that takes a pointer and a length directly. PR-URL: nodejs#24264 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
Instead of calling into JS from C++ to push values into an array, use the new Array::New API that takes a pointer and a length directly. PR-URL: nodejs#24264 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
Instead of calling into JS from C++ to push values into an array, use the new Array::New API that takes a pointer and a length directly. PR-URL: nodejs#24264 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
The usage of NODE_PUSH_VAL_TO_ARRAY_MAX and push_values_to_array_function has all been removed in favor of the new Array::New API that takes a C++ array. Remove the unused code. PR-URL: nodejs#24264 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
@joyeecheung can/should this be backported to |
This PR removes
pushValueToArray
NODE_PUSH_VAL_TO_ARRAY_MAX
env->env->push_values_to_array_function()
In favor of the new V8 C++ API that constructs an Array from a C++ array directly.
Also removes
setupProcessObject
since by now it is only doing thepush_values_to_array_function
setup.Also added a test for
os.cpus()
values.Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes