-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lib: introduce constant MAX_TICKS #11199
Conversation
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
Outdated
@@ -5,6 +5,7 @@ exports.setup = setupNextTick; | |||
function setupNextTick() { | |||
const promises = require('internal/process/promises'); | |||
const emitPendingUnhandledRejections = promises.setup(scheduleMicrotasks); | |||
const MAX_TICKS = 1e4; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps this should just be a top level const
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I’m reading the code correctly this should be the maximum number of callbacks that are run in a single event loop iteration – is that correct? Maybe there is a better name than MAX_TICKS
, or maybe there should be a comment explaining what this does…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bnoordhuis or @trevnorris for more specifics on the exact semantics. Whichever it is, a comment is good
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@addaleax's interpretation is correct. However, I'm not sure if breaking out in this way runs more nextTicks until the next turn of the event loop or if it runs another loop if more nextTicks remain/are scheduled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A name like kMaxCallbacksPerTick
is arguably more self-documenting - even if 'tick' is a misnomer by now, because...
if breaking out in this way runs more nextTicks until the next turn of the event loop or if it runs another loop if more nextTicks remain/are scheduled
...the remaining ones run when the next call into the VM is made. Anywhere you see a call to MakeCallback()
in the C++ code, that's where nexttick callbacks run.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Fishrock123 that interpretation is incorrect. it guards against the nextTickQueue
array becoming too large and running the process out of memory. There's a simple test for this which shows the event loop not being allowed to proceed. This is by design, as the alternative was a bit of a pain to work around.
setImmediate(() => process._rawDebug('hi'));
(function runner() { process.nextTick(runner) })();
Thanks for the feedback and explanations! |
@Fishrock123 I don't think you are opposing these changes in your review comment, but wanted to double check if you approve of the changes made? |
|
lib/internal/process/next_tick.js
Outdated
@@ -1,5 +1,7 @@ | |||
'use strict'; | |||
|
|||
const kMaxCallbacksPerTick = 1e4; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with the const, but the name needs to be changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback. I've changed the name of the const now and also added a comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@trevnorris Does the latest change look alright to you? Thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heh. Sorry. I wasn't being literal in the name kMaxCallbacksUntilQueueIsShortened
. For an actual name, possibly kMaxCallbacksPerLoop
. Though I won't wait until you make the change to sign-off so you won't have to wait for me.
Currently the maximum number of tick is duplicated in two places. This commit introduces a constant that both can use. PR-URL: #11199 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Landed in aa05209 |
Currently the maximum number of tick is duplicated in two places. This commit introduces a constant that both can use. PR-URL: nodejs#11199 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
@jasnell I had mentioned in a comment that this should have waited until the variable name was changed:
And I simply signed off early because I'm technically on paternity leave ATM and didn't want to hold up this PR any longer. |
opened a PR to rename |
Ah sorry I misunderstood. Thanks for clearing that up despite being on parental leave! |
Thanks for all the feedback on this issue! I appreciate you all taking time to help me better understand this part of the code base. |
@trevnorris Sorry about that I had completely overlooked your comment! |
Currently the maximum number of tick is duplicated in two places. This commit introduces a constant that both can use. PR-URL: #11199 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
PR-URL: #11473 Ref: #11199 (comment) Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
PR-URL: #11473 Ref: #11199 (comment) Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Currently the maximum number of tick is duplicated in two places. This commit introduces a constant that both can use. PR-URL: #11199 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
PR-URL: #11473 Ref: #11199 (comment) Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Currently the maximum number of tick is duplicated in two places. This commit introduces a constant that both can use. PR-URL: #11199 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
PR-URL: #11473 Ref: #11199 (comment) Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Currently the maximum number of tick is duplicated in two places. This
commit introduces a constant that both can use.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
lib