Skip to content
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

Closed
wants to merge 4 commits into from

Conversation

danbev
Copy link
Contributor

@danbev danbev commented Feb 6, 2017

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), or vcbuild test (Windows) passes
  • commit message follows commit guidelines
Affected core subsystem(s)

lib

Currently the maximum number of tick is duplicated in two places. This
commit introduces a constant that both can use.
@@ -5,6 +5,7 @@ exports.setup = setupNextTick;
function setupNextTick() {
const promises = require('internal/process/promises');
const emitPendingUnhandledRejections = promises.setup(scheduleMicrotasks);
const MAX_TICKS = 1e4;
Copy link
Member

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?

Copy link
Member

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…

Copy link
Member

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

Copy link
Contributor

@Fishrock123 Fishrock123 Feb 6, 2017

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.

Copy link
Member

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.

Copy link
Contributor

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) })();

@addaleax addaleax added the lib / src Issues and PRs related to general changes in the lib or src directory. label Feb 6, 2017
@danbev
Copy link
Contributor Author

danbev commented Feb 7, 2017

Thanks for the feedback and explanations!

CI: https://ci.nodejs.org/job/node-test-pull-request/6252/

@danbev
Copy link
Contributor Author

danbev commented Feb 8, 2017

@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?

@trevnorris
Copy link
Contributor

kMaxCallbacksPerTick is misleading. The nextTickQueue will always be drained before proceeding (except in the case of an exception). That value is more kMaxCallbacksUntilQueueIsShortened.

@@ -1,5 +1,7 @@
'use strict';

const kMaxCallbacksPerTick = 1e4;
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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

Copy link
Contributor

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.

@jasnell
Copy link
Member

jasnell commented Feb 15, 2017

jasnell pushed a commit that referenced this pull request Feb 17, 2017
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>
@jasnell
Copy link
Member

jasnell commented Feb 17, 2017

Landed in aa05209

@jasnell jasnell closed this Feb 17, 2017
@danbev danbev deleted the introduce-max-ticks-constant branch February 17, 2017 06:33
italoacasas pushed a commit to italoacasas/node that referenced this pull request Feb 20, 2017
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>
@trevnorris
Copy link
Contributor

@jasnell I had mentioned in a comment that this should have waited until the variable name was changed:

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.

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.

@JungMinu
Copy link
Member

JungMinu commented Feb 21, 2017

opened a PR to rename kMaxCallbacksUntilQueueIsShortened : #11473

@danbev
Copy link
Contributor Author

danbev commented Feb 21, 2017

Heh. Sorry. I wasn't being literal in the name kMaxCallbacksUntilQueueIsShortened.

Ah sorry I misunderstood. Thanks for clearing that up despite being on parental leave!

@danbev
Copy link
Contributor Author

danbev commented Feb 21, 2017

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.

@jasnell
Copy link
Member

jasnell commented Feb 21, 2017

@trevnorris Sorry about that I had completely overlooked your comment!

italoacasas pushed a commit that referenced this pull request Feb 22, 2017
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>
jasnell pushed a commit that referenced this pull request Feb 24, 2017
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>
addaleax pushed a commit that referenced this pull request Feb 24, 2017
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>
@italoacasas italoacasas mentioned this pull request Feb 25, 2017
jasnell pushed a commit that referenced this pull request Mar 7, 2017
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>
jasnell pushed a commit that referenced this pull request Mar 7, 2017
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>
MylesBorins pushed a commit that referenced this pull request Mar 9, 2017
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>
MylesBorins pushed a commit that referenced this pull request Mar 9, 2017
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>
@MylesBorins MylesBorins mentioned this pull request Mar 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lib / src Issues and PRs related to general changes in the lib or src directory.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants