timers: use ref counts to count timers · nodejs/node@b5b3750 · GitHub
Skip to content

Commit b5b3750

Browse files
RaisinTendanielleadams
authored andcommitted
timers: use ref counts to count timers
The additional objects that were getting added and deleted from the activeTimersMap object were slowing down the rest of the timers code, so this change falls back to using the ref counts to count the active timers inside process.getActiveResourcesInfo(). Fixes: #41219 Signed-off-by: Darshan Sen <darshan.sen@postman.com> PR-URL: #41231 Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 77c1868 commit b5b3750

3 files changed

Lines changed: 14 additions & 22 deletions

File tree

lib/internal/bootstrap/node.js

Lines changed: 5 additions & 7 deletions

lib/internal/timers.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,6 @@ const kRefed = Symbol('refed');
139139
// Create a single linked list instance only once at startup
140140
const immediateQueue = new ImmediateList();
141141

142-
// Object map containing timers
143-
//
144-
// - key = asyncId
145-
// - value = { type, resource }
146-
const activeTimersMap = ObjectCreate(null);
147-
148142
let nextExpiry = Infinity;
149143
let refCount = 0;
150144

@@ -166,7 +160,6 @@ function initAsyncResource(resource, type) {
166160
resource[trigger_async_id_symbol] = getDefaultTriggerAsyncId();
167161
if (initHooksExist())
168162
emitInit(asyncId, type, triggerAsyncId, resource);
169-
activeTimersMap[asyncId] = { type, resource };
170163
}
171164

172165
// Timer constructor function.
@@ -478,7 +471,6 @@ function getTimerCallbacks(runNextTicks) {
478471

479472
if (destroyHooksExist())
480473
emitDestroy(asyncId);
481-
delete activeTimersMap[asyncId];
482474

483475
outstandingQueue.head = immediate = immediate._idleNext;
484476
}
@@ -551,7 +543,6 @@ function getTimerCallbacks(runNextTicks) {
551543

552544
if (destroyHooksExist())
553545
emitDestroy(asyncId);
554-
delete activeTimersMap[asyncId];
555546
}
556547
continue;
557548
}
@@ -580,7 +571,6 @@ function getTimerCallbacks(runNextTicks) {
580571

581572
if (destroyHooksExist())
582573
emitDestroy(asyncId);
583-
delete activeTimersMap[asyncId];
584574
}
585575
}
586576

@@ -648,6 +638,13 @@ class Immediate {
648638
}
649639
}
650640

641+
function getTimerCounts() {
642+
return {
643+
timeoutCount: refCount,
644+
immediateCount: immediateInfo[kRefCount],
645+
};
646+
}
647+
651648
module.exports = {
652649
TIMEOUT_MAX,
653650
kTimeout: Symbol('timeout'), // For hiding Timeouts on other internals.
@@ -670,9 +667,9 @@ module.exports = {
670667
active,
671668
unrefActive,
672669
insert,
673-
activeTimersMap,
674670
timerListMap,
675671
timerListQueue,
676672
decRefCount,
677-
incRefCount
673+
incRefCount,
674+
getTimerCounts,
678675
};

lib/timers.js

Lines changed: 0 additions & 3 deletions

0 commit comments

Comments
 (0)