Skip to content

Commit 3ac7f2f

Browse files
committed
fixup! fixup! timers: use V8 fast API calls
1 parent 2352648 commit 3ac7f2f

File tree

5 files changed

+24
-7
lines changed

5 files changed

+24
-7
lines changed

lib/internal/timers.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,17 @@ class ImmediateList {
303303
const immediateQueue = new ImmediateList();
304304

305305
function incRefCount() {
306-
if (timeoutInfo[0]++ === 0)
306+
if (timeoutInfo[0]++ === 0) {
307+
// We need to use the binding as the receiver for fast API calls.
307308
binding.toggleTimerRef(true);
309+
}
308310
}
309311

310312
function decRefCount() {
311-
if (--timeoutInfo[0] === 0)
313+
if (--timeoutInfo[0] === 0) {
314+
// We need to use the binding as the receiver for fast API calls.
312315
binding.toggleTimerRef(false);
316+
}
313317
}
314318

315319
// Schedule or re-schedule a timer.
@@ -353,6 +357,7 @@ function insertGuarded(item, refed, start) {
353357
item[kRefed] = refed;
354358
}
355359

360+
// We need to use the binding as the receiver for fast API calls.
356361
function insert(item, msecs, start = binding.getLibuvNow()) {
357362
// Truncate so that accuracy of sub-millisecond timers is not assumed.
358363
msecs = MathTrunc(msecs);
@@ -367,6 +372,7 @@ function insert(item, msecs, start = binding.getLibuvNow()) {
367372
timerListQueue.insert(list);
368373

369374
if (nextExpiry > expiry) {
375+
// We need to use the binding as the receiver for fast API calls.
370376
binding.scheduleTimer(msecs);
371377
nextExpiry = expiry;
372378
}
@@ -556,8 +562,10 @@ function getTimerCallbacks(runNextTicks) {
556562
emitBefore(asyncId, timer[trigger_async_id_symbol], timer);
557563

558564
let start;
559-
if (timer._repeat)
565+
if (timer._repeat) {
566+
// We need to use the binding as the receiver for fast API calls.
560567
start = binding.getLibuvNow();
568+
}
561569

562570
try {
563571
const args = timer._timerArgs;
@@ -624,17 +632,22 @@ class Immediate {
624632
ref() {
625633
if (this[kRefed] === false) {
626634
this[kRefed] = true;
627-
if (immediateInfo[kRefCount]++ === 0)
635+
636+
if (immediateInfo[kRefCount]++ === 0) {
637+
// We need to use the binding as the receiver for fast API calls.
628638
binding.toggleImmediateRef(true);
639+
}
629640
}
630641
return this;
631642
}
632643

633644
unref() {
634645
if (this[kRefed] === true) {
635646
this[kRefed] = false;
636-
if (--immediateInfo[kRefCount] === 0)
647+
if (--immediateInfo[kRefCount] === 0) {
648+
// We need to use the binding as the receiver for fast API calls.
637649
binding.toggleImmediateRef(false);
650+
}
638651
}
639652
return this;
640653
}

lib/timers.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,10 @@ function clearImmediate(immediate) {
323323
immediateInfo[kCount]--;
324324
immediate._destroyed = true;
325325

326-
if (immediate[kRefed] && --immediateInfo[kRefCount] === 0)
326+
if (immediate[kRefed] && --immediateInfo[kRefCount] === 0) {
327+
// We need to use the binding as the receiver for fast API calls.
327328
binding.toggleImmediateRef(false);
329+
}
328330
immediate[kRefed] = null;
329331

330332
if (destroyHooksExist() && immediate[async_id_symbol] !== undefined) {

src/timers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class BindingData : public SnapshotableObject {
3434
static void SlowScheduleTimer(
3535
const v8::FunctionCallbackInfo<v8::Value>& args);
3636
static void FastScheduleTimer(v8::Local<v8::Object> receiver,
37-
int64_t duration);
37+
int64_t duration);
3838
static void ScheduleTimerImpl(BindingData* data, int64_t duration);
3939

4040
static void SlowToggleTimerRef(

test/parallel/test-timers-now.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ const { internalBinding } = require('internal/test/binding');
77
const binding = internalBinding('timers');
88

99
// Return value of getLibuvNow() should easily fit in a SMI after start-up.
10+
// We need to use the binding as the receiver for fast API calls.
1011
assert(binding.getLibuvNow() < 0x3ffffff);

test/parallel/test-timers-ordering.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function f(i) {
3939
last_i = i;
4040

4141
// Check that this iteration is fired at least 1ms later than the previous
42+
// We need to use the binding as the receiver for fast API calls.
4243
const now = binding.getLibuvNow();
4344
assert(now >= last_ts + 1,
4445
`current ts ${now} < prev ts ${last_ts} + 1`);

0 commit comments

Comments
 (0)