Added a new struct CallbackBundle to eliminate all GetInternalField() calls. 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 are 3 GetInternalField() calls; after this change, the C++ struct will be directly fetched out by using v8::External::Value(), which is faster. Profiling data show that GetInternalField() is slow. On an i7-4770K (3.50GHz) box, 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 20 ns. The above data are measured using a modified benchmark in 'benchmark/misc/function_call'. The modification adds an indicator of the average time of a "chatty" napi fun call (max 50M runs). This change will speed up chatty case 1.8x (overall), and will cut down the delay of napi mechanism to approx. 0.5x. Background: a simple C++ binding function (e.g. receiving little from JS, doing little and returning little to JS) is called 'chatty' case for JS<-->C++ fun call routine. This improvement also applies to getter/setter fun calls. Backport-PR-URL: https://github.com/nodejs/node/pull/21733 PR-URL: https://github.com/nodejs/node/pull/21072 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
28 lines
781 B
C
28 lines
781 B
C
#include <assert.h>
|
|
#include <node_api.h>
|
|
|
|
static int32_t increment = 0;
|
|
|
|
static napi_value Hello(napi_env env, napi_callback_info info) {
|
|
napi_value result;
|
|
napi_status status = napi_create_int32(env, increment++, &result);
|
|
assert(status == napi_ok);
|
|
return result;
|
|
}
|
|
|
|
static napi_value Init(napi_env env, napi_value exports) {
|
|
napi_value hello;
|
|
napi_status status =
|
|
napi_create_function(env,
|
|
"hello",
|
|
NAPI_AUTO_LENGTH,
|
|
Hello,
|
|
NULL,
|
|
&hello);
|
|
assert(status == napi_ok);
|
|
status = napi_set_named_property(env, exports, "hello", hello);
|
|
assert(status == napi_ok);
|
|
return exports;
|
|
}
|
|
|
|
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
|