From b77745c792911a4fcb26619acb6e9268ee341ddb Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Fri, 3 Feb 2017 12:46:23 +0100 Subject: [PATCH 1/4] lib: introduce constant MAX_TICKS Currently the maximum number of tick is duplicated in two places. This commit introduces a constant that both can use. --- lib/internal/process/next_tick.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/internal/process/next_tick.js b/lib/internal/process/next_tick.js index f27ef622a96e6a..d08a97246aebce 100644 --- a/lib/internal/process/next_tick.js +++ b/lib/internal/process/next_tick.js @@ -5,6 +5,7 @@ exports.setup = setupNextTick; function setupNextTick() { const promises = require('internal/process/promises'); const emitPendingUnhandledRejections = promises.setup(scheduleMicrotasks); + const MAX_TICKS = 1e4; var nextTickQueue = []; var microtasksScheduled = false; @@ -96,7 +97,7 @@ function setupNextTick() { // callback invocation with small numbers of arguments to avoid the // performance hit associated with using `fn.apply()` _combinedTickCallback(args, callback); - if (1e4 < tickInfo[kIndex]) + if (MAX_TICKS < tickInfo[kIndex]) tickDone(); } tickDone(); @@ -120,7 +121,7 @@ function setupNextTick() { // callback invocation with small numbers of arguments to avoid the // performance hit associated with using `fn.apply()` _combinedTickCallback(args, callback); - if (1e4 < tickInfo[kIndex]) + if (MAX_TICKS < tickInfo[kIndex]) tickDone(); if (domain) domain.exit(); From 2d266c0700c88b2d73944ab7d641e04f18f46dd3 Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Tue, 7 Feb 2017 07:48:39 +0100 Subject: [PATCH 2/4] rename MAX_TICKS -> kMaxCallbacksPerTick --- lib/internal/process/next_tick.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/internal/process/next_tick.js b/lib/internal/process/next_tick.js index d08a97246aebce..82e27046f1b647 100644 --- a/lib/internal/process/next_tick.js +++ b/lib/internal/process/next_tick.js @@ -5,7 +5,7 @@ exports.setup = setupNextTick; function setupNextTick() { const promises = require('internal/process/promises'); const emitPendingUnhandledRejections = promises.setup(scheduleMicrotasks); - const MAX_TICKS = 1e4; + const kMaxCallbacksPerTick = 1e4; var nextTickQueue = []; var microtasksScheduled = false; @@ -97,7 +97,7 @@ function setupNextTick() { // callback invocation with small numbers of arguments to avoid the // performance hit associated with using `fn.apply()` _combinedTickCallback(args, callback); - if (MAX_TICKS < tickInfo[kIndex]) + if (kMaxCallbacksPerTick < tickInfo[kIndex]) tickDone(); } tickDone(); @@ -121,7 +121,7 @@ function setupNextTick() { // callback invocation with small numbers of arguments to avoid the // performance hit associated with using `fn.apply()` _combinedTickCallback(args, callback); - if (MAX_TICKS < tickInfo[kIndex]) + if (kMaxCallbacksPerTick < tickInfo[kIndex]) tickDone(); if (domain) domain.exit(); From 8788a977231cee8dbb3b8acd09e9f25ed50c5c79 Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Tue, 7 Feb 2017 07:52:54 +0100 Subject: [PATCH 3/4] make kMaxCallbacksPerTick a top level const --- lib/internal/process/next_tick.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/internal/process/next_tick.js b/lib/internal/process/next_tick.js index 82e27046f1b647..1b1421d723bdd3 100644 --- a/lib/internal/process/next_tick.js +++ b/lib/internal/process/next_tick.js @@ -1,11 +1,12 @@ 'use strict'; +const kMaxCallbacksPerTick = 1e4; + exports.setup = setupNextTick; function setupNextTick() { const promises = require('internal/process/promises'); const emitPendingUnhandledRejections = promises.setup(scheduleMicrotasks); - const kMaxCallbacksPerTick = 1e4; var nextTickQueue = []; var microtasksScheduled = false; From eda4d32d22f0ade20874d03bd3dcf8ec721c688a Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Sun, 12 Feb 2017 20:43:38 +0100 Subject: [PATCH 4/4] rename to kMaxCallbacksUntilQueueIsShortened --- lib/internal/process/next_tick.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/internal/process/next_tick.js b/lib/internal/process/next_tick.js index 1b1421d723bdd3..bc89c6e8b269e3 100644 --- a/lib/internal/process/next_tick.js +++ b/lib/internal/process/next_tick.js @@ -1,6 +1,10 @@ 'use strict'; -const kMaxCallbacksPerTick = 1e4; +// This value is used to prevent the nextTickQueue from becoming too +// large and cause the process to run out of memory. When this value +// is reached the nextTimeQueue array will be shortend (see tickDone +// for details). +const kMaxCallbacksUntilQueueIsShortened = 1e4; exports.setup = setupNextTick; @@ -98,7 +102,7 @@ function setupNextTick() { // callback invocation with small numbers of arguments to avoid the // performance hit associated with using `fn.apply()` _combinedTickCallback(args, callback); - if (kMaxCallbacksPerTick < tickInfo[kIndex]) + if (kMaxCallbacksUntilQueueIsShortened < tickInfo[kIndex]) tickDone(); } tickDone(); @@ -122,7 +126,7 @@ function setupNextTick() { // callback invocation with small numbers of arguments to avoid the // performance hit associated with using `fn.apply()` _combinedTickCallback(args, callback); - if (kMaxCallbacksPerTick < tickInfo[kIndex]) + if (kMaxCallbacksUntilQueueIsShortened < tickInfo[kIndex]) tickDone(); if (domain) domain.exit();