2018-12-10 13:27:32 +01:00
|
|
|
// Show the difference between calling a short js function
|
2013-02-11 18:22:43 -08:00
|
|
|
// relative to a comparable C++ function.
|
2018-03-17 20:49:09 +05:30
|
|
|
// Reports n of calls per second.
|
2013-02-11 18:22:43 -08:00
|
|
|
// Note that JS speed goes up, while cxx speed stays about the same.
|
2016-02-19 17:03:16 -08:00
|
|
|
'use strict';
|
2013-02-11 18:22:43 -08:00
|
|
|
|
2017-09-13 22:48:53 -03:00
|
|
|
const assert = require('assert');
|
|
|
|
|
const common = require('../../common.js');
|
2013-02-11 18:22:43 -08:00
|
|
|
|
2018-12-03 17:15:45 +01:00
|
|
|
// This fails when we try to open with a different version of node,
|
2013-02-12 00:13:18 -08:00
|
|
|
// which is quite common for benchmarks. so in that case, just
|
|
|
|
|
// abort quietly.
|
|
|
|
|
|
2019-05-08 20:45:10 +02:00
|
|
|
let binding;
|
2013-02-12 00:13:18 -08:00
|
|
|
try {
|
2019-05-08 20:45:10 +02:00
|
|
|
binding = require(`./build/${common.buildType}/binding`);
|
2018-11-04 12:38:54 -05:00
|
|
|
} catch {
|
2013-02-12 00:13:18 -08:00
|
|
|
console.error('misc/function_call.js Binding failed to load');
|
|
|
|
|
process.exit(0);
|
|
|
|
|
}
|
2017-09-13 22:48:53 -03:00
|
|
|
const cxx = binding.hello;
|
2013-02-11 18:22:43 -08:00
|
|
|
|
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: https://github.com/nodejs/node/pull/21072
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
2018-06-07 10:01:47 +08:00
|
|
|
let napi_binding;
|
|
|
|
|
try {
|
2018-11-02 07:53:57 +01:00
|
|
|
napi_binding = require(`./build/${common.buildType}/napi_binding`);
|
2018-11-04 12:38:54 -05:00
|
|
|
} catch {
|
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: https://github.com/nodejs/node/pull/21072
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
2018-06-07 10:01:47 +08:00
|
|
|
console.error('misc/function_call/index.js NAPI-Binding failed to load');
|
|
|
|
|
process.exit(0);
|
|
|
|
|
}
|
|
|
|
|
const napi = napi_binding.hello;
|
|
|
|
|
|
2020-01-31 16:50:00 +01:00
|
|
|
let c = 0;
|
2013-02-11 18:22:43 -08:00
|
|
|
function js() {
|
|
|
|
|
return c++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert(js() === cxx());
|
|
|
|
|
|
2017-09-13 22:48:53 -03:00
|
|
|
const bench = common.createBenchmark(main, {
|
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: https://github.com/nodejs/node/pull/21072
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
2018-06-07 10:01:47 +08:00
|
|
|
type: ['js', 'cxx', 'napi'],
|
2023-01-29 20:13:35 +02:00
|
|
|
n: [1e6, 1e7, 5e7],
|
2013-02-11 18:22:43 -08:00
|
|
|
});
|
|
|
|
|
|
2018-03-17 20:49:09 +05:30
|
|
|
function main({ n, type }) {
|
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: https://github.com/nodejs/node/pull/21072
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
2018-06-07 10:01:47 +08:00
|
|
|
const fn = type === 'cxx' ? cxx : type === 'napi' ? napi : js;
|
2013-02-11 18:22:43 -08:00
|
|
|
bench.start();
|
2020-01-31 16:50:00 +01:00
|
|
|
for (let i = 0; i < n; i++) {
|
2013-02-11 18:22:43 -08:00
|
|
|
fn();
|
|
|
|
|
}
|
2018-03-17 20:49:09 +05:30
|
|
|
bench.end(n);
|
2013-02-11 18:22:43 -08:00
|
|
|
}
|