Skip to content

Commit f824b8f

Browse files
committed
add non working fast path
1 parent f08f756 commit f824b8f

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const assert = require('assert');
5+
6+
const bench = common.createBenchmark(main, {
7+
n: [1e4],
8+
});
9+
10+
// Warm up.
11+
const length = 1024;
12+
const array = [];
13+
for (let i = 0; i < length; ++i) {
14+
array.push(global.navigator.onLine);
15+
}
16+
17+
function main({ n }) {
18+
bench.start();
19+
for (let i = 0; i < n; ++i) {
20+
const index = i % length;
21+
array[index] = global.navigator.onLine;
22+
}
23+
bench.end(n);
24+
25+
// Verify the entries to prevent dead code elimination from making
26+
// the benchmark invalid.
27+
for (let i = 0; i < length; ++i) {
28+
assert.strictEqual(typeof array[i], 'boolean');
29+
}
30+
}

src/node_external_reference.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ using CFunctionCallbackWithStrings =
2727
const v8::FastOneByteString& base);
2828
using CFunctionWithUint32 = uint32_t (*)(v8::Local<v8::Value>,
2929
const uint32_t input);
30-
30+
using CFunctionReturnBool = bool (*)(v8::FastApiCallbackOptions& options);
3131
// This class manages the external references from the V8 heap
3232
// to the C++ addresses in Node.js.
3333
class ExternalReferenceRegistry {
@@ -43,6 +43,7 @@ class ExternalReferenceRegistry {
4343
V(CFunctionCallbackWithString) \
4444
V(CFunctionCallbackWithStrings) \
4545
V(CFunctionWithUint32) \
46+
V(CFunctionReturnBool) \
4647
V(const v8::CFunctionInfo*) \
4748
V(v8::FunctionCallback) \
4849
V(v8::AccessorGetterCallback) \

src/node_os.cc

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22+
#include <stdio.h>
2223
#include "env-inl.h"
2324
#include "node_external_reference.h"
2425
#include "string_bytes.h"
26+
#include "v8-fast-api-calls.h"
2527

2628
#ifdef __MINGW32__
2729
# include <io.h>
@@ -247,7 +249,7 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) {
247249
args.GetReturnValue().Set(Array::New(isolate, result.data(), result.size()));
248250
}
249251

250-
static void GetOnLineStatus(const FunctionCallbackInfo<Value>& args) {
252+
static void SlowGetOnLineStatus(const FunctionCallbackInfo<Value>& args) {
251253
Environment* env = Environment::GetCurrent(args);
252254
uv_interface_address_t* interfaces;
253255
int count, i;
@@ -274,6 +276,32 @@ static void GetOnLineStatus(const FunctionCallbackInfo<Value>& args) {
274276
return args.GetReturnValue().Set(false);
275277
}
276278

279+
static bool FastGetOnLineStatus(v8::FastApiCallbackOptions& options) {
280+
uv_interface_address_t* interfaces;
281+
int count, i;
282+
283+
printf("FastGetOnLineStatus\n");
284+
int err = uv_interface_addresses(&interfaces, &count);
285+
286+
if (err) {
287+
options.fallback = true;
288+
return false;
289+
}
290+
291+
for (i = 0; i < count; i++) {
292+
if (interfaces[i].is_internal == false) {
293+
uv_free_interface_addresses(interfaces, count);
294+
return true;
295+
}
296+
}
297+
298+
uv_free_interface_addresses(interfaces, count);
299+
return false;
300+
}
301+
302+
v8::CFunction fast_get_on_line_status_(
303+
v8::CFunction::Make(FastGetOnLineStatus));
304+
277305
static void GetHomeDirectory(const FunctionCallbackInfo<Value>& args) {
278306
Environment* env = Environment::GetCurrent(args);
279307
char buf[PATH_MAX];
@@ -429,7 +457,11 @@ void Initialize(Local<Object> target,
429457
SetMethod(context, target, "getPriority", GetPriority);
430458
SetMethod(
431459
context, target, "getAvailableParallelism", GetAvailableParallelism);
432-
SetMethod(context, target, "getOnLineStatus", GetOnLineStatus);
460+
SetFastMethod(context,
461+
target,
462+
"getOnLineStatus",
463+
SlowGetOnLineStatus,
464+
&fast_get_on_line_status_);
433465
SetMethod(context, target, "getOSInformation", GetOSInformation);
434466
target
435467
->Set(context,
@@ -446,7 +478,9 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
446478
registry->Register(GetFreeMemory);
447479
registry->Register(GetCPUInfo);
448480
registry->Register(GetInterfaceAddresses);
449-
registry->Register(GetOnLineStatus);
481+
registry->Register(SlowGetOnLineStatus);
482+
registry->Register(FastGetOnLineStatus);
483+
registry->Register(fast_get_on_line_status_.GetTypeInfo());
450484
registry->Register(GetHomeDirectory);
451485
registry->Register(GetUserInfo);
452486
registry->Register(SetPriority);

0 commit comments

Comments
 (0)