Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
n-api: improve runtime perf of n-api func call
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. PR-URL: #21072 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
- Loading branch information
Showing
with
114 additions
and 89 deletions.
- +1 −0 Makefile
- +4 −0 benchmark/misc/function_call/binding.gyp
- +11 −2 benchmark/misc/function_call/index.js
- +26 −0 benchmark/misc/function_call/napi_binding.c
- +72 −87 src/node_api.cc
@@ -0,0 +1,26 @@ | ||
#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; | ||
} | ||
|
||
NAPI_MODULE_INIT() { | ||
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; | ||
} |