-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
napi: improve runtime performance of every napi fun call.
Added a new struct CallbackBundle to reduce the number of GetInternalField() calls -- was: 3; now: 1. The principle is to store all required data inside a C++ struct, and then store the pointer in the JavaScript object. Before this change, the required data are stored in the JavaScript object in 3 or 4 seperate pointers. For every napi fun call, 3 of them have to be fetched out, which is 3 GetInternalField() calls; after this change, there will be only 1 GetInternalField() call. Profiling data show that GetInternalField() is slow. On i7-4770K (3.50GHz), a C++ V8-binding fun call is 8 ns, before this change, napi fun call is 36 ns; after this change, napi fun call is 22 ns. The above data are measured using a new benchmark in 'benchmark/misc/napi_function_call'. This new benchmark measures the average delay of a 'chatty' napi fun call (max 50M runs). A simple C++ binding function (e.g. returning a number) is called 'chatty' case for JS<-->napi fun call. This change will speed up chatty case 1.6x (overall), and will cut down the delay of napi mechanism to approx. 0.5x This improvement also applies to getter/setter fun calls.
- Loading branch information
Showing
6 changed files
with
156 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include <node_api.h> | ||
#include <assert.h> | ||
|
||
static int32_t counter = 0; | ||
|
||
napi_value Hello(napi_env env, napi_callback_info info) { | ||
napi_status status; | ||
napi_value value; | ||
napi_create_int32(env, counter++, &value); | ||
assert(status == napi_ok); | ||
return value; | ||
} | ||
|
||
#define DECLARE_NAPI_METHOD(name, func) \ | ||
{ name, 0, func, 0, 0, 0, napi_default, 0 } | ||
|
||
napi_value Init(napi_env env, napi_value exports) { | ||
napi_status status; | ||
napi_property_descriptor desc = DECLARE_NAPI_METHOD("hello", Hello); | ||
status = napi_define_properties(env, exports, 1, &desc); | ||
assert(status == napi_ok); | ||
return exports; | ||
} | ||
|
||
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"targets": [ | ||
{ | ||
"target_name": "binding", | ||
"sources": [ "binding.cc" ] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// show the difference between calling a short js function | ||
// relative to a comparable C++ function. | ||
// Reports n of calls per second. | ||
// Note that JS speed goes up, while cxx speed stays about the same. | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const common = require('../../common.js'); | ||
|
||
// this fails when we try to open with a different version of node, | ||
// which is quite common for benchmarks. so in that case, just | ||
// abort quietly. | ||
|
||
try { | ||
var binding = require('./build/Release/binding'); | ||
} catch (er) { | ||
console.error('misc/napi_function_call/index.js Binding failed to load'); | ||
process.exit(0); | ||
} | ||
const cxx = binding.hello; | ||
|
||
var c = 0; | ||
function js() { | ||
return c++; | ||
} | ||
|
||
assert(js() === cxx()); | ||
|
||
const bench = common.createBenchmark(main, { | ||
type: ['js', 'cxx(napi)'], | ||
n: [1e6, 1e7, 5e7] | ||
}); | ||
|
||
function main({ n, type }) { | ||
const fn = type === 'cxx(napi)' ? cxx : js; | ||
bench.start(); | ||
for (var i = 0; i < n; i++) { | ||
fn(); | ||
} | ||
bench.end(n); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters