GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — benchmark: add n-api function arguments benchmark suite by kenny-y · Pull Request #21555 · nodejs/node

Conversation

@kenny-y

@kenny-y kenny-y commented Jun 27, 2018

Copy link
Copy Markdown
Contributor

This benchmark suite is added to measure the performance of n-api
function call with various type/number of arguments. The cases in
this suite are carefully selected to efficiently show the performance
trend.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@nodejs-github-bot nodejs-github-bot added the build Issues and PRs related to Node.js builds or CI infrastructure. label Jun 27, 2018
@kenny-y
benchmark: add n-api function arguments benchmark suite
6aa13a1
This benchmark suite is added to measure the performance of n-api
function call with various type/number of arguments. The cases in
this suite are carefully selected to efficiently show the performance
trend.
@kenny-y

kenny-y commented Jun 27, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

The below charts are generated by this benchmark suite:

image
image
image
image
image
image
image
image

Generally speaking, it's better to cover more combination of types and numbers of arguments, but from my observation, the trend is the same. So I filtered out most of the combination and selected the cases.

CC @gabrielschulhof

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gabrielschulhof

gabrielschulhof commented Jun 27, 2018

Copy link
Copy Markdown
Contributor

@kenny-y I think it might be better placed under benchmark/misc, like function_call, because this also provides useful data about V8 on its own, not just about V8 vs. N-API.

I'm surprised that N-API performs better in some use cases than V8. It seems strange.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gabrielschulhof
gabrielschulhof reviewed Jun 27, 2018
View reviewed changes
Comment thread benchmark/napi/function_args/napi_binding.c
status = napi_create_string_utf8(env, "reduce", strlen("reduce"), &key);
assert(status == napi_ok);
status = napi_get_property(env, args[0], key, &value);
assert(status == napi_ok);

@gabrielschulhof gabrielschulhof Jun 27, 2018

Copy link
Copy Markdown
Contributor

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.

You could combine these into napi_get_named_property(). That will internally create the string for you.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
gabrielschulhof
gabrielschulhof reviewed Jun 27, 2018
View reviewed changes
Comment thread benchmark/napi/function_args/napi_binding.c Outdated
status = napi_typeof(env, args[0], types);
assert(status == napi_ok);

assert(types[0] == napi_object);

@gabrielschulhof gabrielschulhof Jun 27, 2018

Copy link
Copy Markdown
Contributor

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.

You should add && argc == 1 into the assertion, to be on par with the V8 version.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
gabrielschulhof
gabrielschulhof reviewed Jun 27, 2018
View reviewed changes
Comment thread benchmark/napi/function_args/binding.cc
v8::Isolate* isolate = args.GetIsolate();

assert(args.Length() == 1 && args[0]->IsObject());
if (args.Length() == 1 && args[0]->IsObject()) {

@gabrielschulhof gabrielschulhof Jun 27, 2018

Copy link
Copy Markdown
Contributor

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 it might be best to save the result of args.Length() == 1 && args[0]->IsObject() first and then both assert() it and use it as the if-statement condition. For clarity, you could assert(argumentsCorrect && "argument length is 1 and the first argument is an object"), where bool argumentsCorrect = (args.Length() == 1 && args[0]->IsObject());

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@gabrielschulhof

gabrielschulhof commented Jun 27, 2018

Copy link
Copy Markdown
Contributor

Wait, NM. function_call is in napi, not misc. Still, strange about the CallWithObject results.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gabrielschulhof
gabrielschulhof reviewed Jun 27, 2018
View reviewed changes
Comment thread benchmark/napi/function_args/binding.cc Outdated
Local<Value> map = obj->Get(String::NewFromUtf8(isolate, "map"));
Local<Value> operand = obj->Get(String::NewFromUtf8(isolate, "operand"));
Local<Value> data = obj->Get(String::NewFromUtf8(isolate, "data"));
Local<Value> reduce = obj->Get(String::NewFromUtf8(isolate, "reduce"));

@gabrielschulhof gabrielschulhof Jun 27, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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.

N-API uses the maybe version of Get() and String::NewFromUtf8() because the others are deprecated, so, these should be changed to

Local<Context> context = isolate->GetCurrentContext();

and then, for each property,

MaybeLocal<String> map_string = String::NewFromUtf8(isolate,
                                                    "map",
                                                    NewStringType::kNormal);
assert(!map_string.IsEmpty());
MaybeLocal<value> maybe_map = obj->Get(context, map_string.ToLocalChecked());
assert(!maybe_map.IsEmpty());
Local<Value> map = maybe_map.ToLocalChecked();

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@kenny-y
Some fine adjustment based on the review comments in
cb758e9
nodejs#21555
@kenny-y

kenny-y commented Jun 28, 2018

Copy link
Copy Markdown
Contributor Author

Yep, there was a PR #21046 moved function_call to an napi dir, so I also put this new one under that dir.

After the change to CallWithObject(), the trend is still the same:

image

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kenny-y
Fixed wrong calc of typedarray offset (result not in use)
75e6e42
@mhdawson

mhdawson commented Jun 29, 2018

Copy link
Copy Markdown
Member

@kenny-y thanks for creating this. I won't have time to take a good look until at least next week but looks like @gabrielschulhof is already commenting :)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gabrielschulhof
gabrielschulhof reviewed Jun 29, 2018
View reviewed changes

@gabrielschulhof gabrielschulhof left a comment •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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.

On my machine, only when you run callWithObject() 1000 times does N-API come out ahead of V8. In all other cases, V8 comes out slightly ahead. Very strange indeed.
object-bench

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread benchmark/napi/function_args/index.js Outdated

const getArgs = (type) => {
return generateArgs(type.split('-')[1]);
};

@gabrielschulhof gabrielschulhof Jun 29, 2018

Copy link
Copy Markdown
Contributor

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.

You don't need this function if you use multiple parameters (see below).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread benchmark/napi/function_args/index.js Outdated
['v8', 'napi'].forEach((type) => {
benchTypes.push(type + '-' + arg);
});
});

@gabrielschulhof gabrielschulhof Jun 29, 2018

Copy link
Copy Markdown
Contributor

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.

You don't need benchTypes either if you use multiple parameters (see below).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread benchmark/napi/function_args/index.js Outdated
});

const bench = common.createBenchmark(main, {
type: benchTypes,

@gabrielschulhof gabrielschulhof Jun 29, 2018

Copy link
Copy Markdown
Contributor

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.

Instead of benchTypes use argsTypes directly, and add a second parameter

  engine: ['v8', 'napi'],

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread benchmark/napi/function_args/index.js Outdated
n: [1, 1e1, 1e2, 1e3, 1e4, 1e5],
});

function main({ n, type }) {

@gabrielschulhof gabrielschulhof Jun 29, 2018

Copy link
Copy Markdown
Contributor

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.

Make this {n, engine, type}, and then you don't need to concatenate or split the arguments. The benchmark will generate all combinations of the three arguments.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

kenny-y reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@kenny-y kenny-y Jul 2, 2018

Copy link
Copy Markdown
Contributor Author

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 to know. Thanks 👍

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@gabrielschulhof

gabrielschulhof commented Jul 1, 2018

Copy link
Copy Markdown
Contributor

@kenny-y could you please also resolve the warnings?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

kenny-y added 2 commits July 2, 2018 13:55
@kenny-y
Removing compiler warnings
173fa80
@kenny-y
Use benchmark suite feature to match every engines with every args-types
10f3fc5
@kenny-y

kenny-y commented Jul 3, 2018

Copy link
Copy Markdown
Contributor Author

@mhdawson Thanks. My original purpose to create this benchmark was to find optimization opportunities, like the previous one #21072, but none had been identified yet... @gabrielschulhof helped me a lot on the previous one, as well as this PR 👍 👍 👍

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gabrielschulhof
gabrielschulhof approved these changes Jul 3, 2018
View reviewed changes

@gabrielschulhof gabrielschulhof left a comment

Copy link
Copy Markdown
Contributor

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, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread benchmark/napi/function_args/napi_binding.c
assert(status == napi_ok); \
status = napi_set_named_property(env, exports, name, func ## _v); \
assert(status == napi_ok); \
} while (0);

@gabrielschulhof gabrielschulhof Jul 3, 2018

Copy link
Copy Markdown
Contributor

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.

nit: The trailing backslashes must be aligned to the column of the outermost trailing backslash.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

kenny-y reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@gabrielschulhof

gabrielschulhof commented Jul 3, 2018

Copy link
Copy Markdown
Contributor

CI: https://ci.nodejs.org/job/node-test-pull-request/15718/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kenny-y
Fixing macro styling issue, trailing backslash alignment.
f10939a
gabrielschulhof
gabrielschulhof reviewed Jul 4, 2018
View reviewed changes
Comment thread benchmark/napi/function_args/napi_binding.c Outdated

#define EXPORT_FUNC(name, func) \
do { \
napi_value func ## _v; \

@gabrielschulhof gabrielschulhof Jul 4, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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.

Actually, you can safely declare

napi_status status;
napi_value js_func;

here and pass js_func to napi_create_function() and napi_set_named_property() because the do { ... } while (0) scope protects them, and then each invocation of the macro is self-contained. Then there's no need to func ## _v, nor to assume that napi_status status is declared outside the macro.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@gabrielschulhof gabrielschulhof Jul 4, 2018

Copy link
Copy Markdown
Contributor

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.

Oh, and please pass exports into the macro as a parameter, and put brackets around env, exports, and func when you use them inside the macro.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@kenny-y kenny-y Jul 4, 2018

Copy link
Copy Markdown
Contributor Author

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.

Done :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@kenny-y
Change the local casual macro to a more formal one.
645f38e
gabrielschulhof
gabrielschulhof reviewed Jul 4, 2018
View reviewed changes
Comment thread benchmark/napi/function_args/napi_binding.c
NULL, \
&js_func); \
assert(status == napi_ok); \
status = napi_set_named_property((env), \

@gabrielschulhof gabrielschulhof Jul 4, 2018

Copy link
Copy Markdown
Contributor

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.

nit: Align arguments as with the previous call.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@kenny-y
Use only one param alignment style in the helper macro.
0f6b238
gabrielschulhof
gabrielschulhof approved these changes Jul 5, 2018
View reviewed changes
@gabrielschulhof

gabrielschulhof commented Jul 5, 2018

Copy link
Copy Markdown
Contributor

CI: https://ci.nodejs.org/job/node-test-pull-request/15738/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gabrielschulhof

gabrielschulhof commented Jul 5, 2018

Copy link
Copy Markdown
Contributor

@kfarnung @mhdawson may I please have another set of eyes on this?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

kfarnung
kfarnung approved these changes Jul 5, 2018
View reviewed changes
@mhdawson

mhdawson commented Jul 5, 2018

Copy link
Copy Markdown
Member

Since @kfarnung beat me to it I'm happy to leave it at that :)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gabrielschulhof gabrielschulhof self-assigned this Jul 6, 2018
@gabrielschulhof

gabrielschulhof commented Jul 6, 2018

Copy link
Copy Markdown
Contributor

Landed in 3314b3a.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gabrielschulhof gabrielschulhof closed this Jul 6, 2018
gabrielschulhof pushed a commit that referenced this pull request Jul 6, 2018
@kenny-y Gabriel Schulhof
benchmark: add n-api function args benchmark
3314b3a
This benchmark suite is added to measure the performance of n-api
function call with various type/number of arguments. The cases in
this suite are carefully selected to efficiently show the performance
trend.

PR-URL: #21555
Reviewed-By: Gabriel Schulhof <[email protected]>
Reviewed-By: Kyle Farnung <[email protected]>
targos pushed a commit that referenced this pull request Jul 6, 2018
@kenny-y @targos
benchmark: add n-api function args benchmark
9776f1c
This benchmark suite is added to measure the performance of n-api
function call with various type/number of arguments. The cases in
this suite are carefully selected to efficiently show the performance
trend.

PR-URL: #21555
Reviewed-By: Gabriel Schulhof <[email protected]>
Reviewed-By: Kyle Farnung <[email protected]>
@targos targos mentioned this pull request Jul 17, 2018
v10.7.0 proposal #21851
Merged
This was referenced Jul 18, 2018
2018-07-18 Version 10.7.0 (Current) @targos beevelop/docker-nodejs#207
Closed
2018-07-18 Version 10.7.0 (Current) @targos beevelop/docker-nodejs#208
Closed
2018-07-18 Version 10.7.0 (Current) @targos beevelop/docker-nodejs#209
Closed
2018-07-18 Version 10.7.0 (Current) @targos beevelop/docker-nodejs#210
Closed
2018-07-18 Version 10.7.0 (Current) @targos beevelop/docker-nodejs#211
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@gabrielschulhof gabrielschulhof gabrielschulhof approved these changes
+1 more reviewer
@kfarnung kfarnung kfarnung approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@gabrielschulhof gabrielschulhof

Labels

build Issues and PRs related to Node.js builds or CI infrastructure.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@kenny-y @gabrielschulhof @mhdawson @kfarnung @nodejs-github-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.