GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — timers: assorted cleanup + improvements by apapirovski · Pull Request #18486 · nodejs/node

Conversation

@apapirovski

@apapirovski apapirovski commented Jan 31, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

There a few changes in this PR, all to timers, and it's best to review each commit independently. It will make way more sense that way.

  1. Instead of using nextTick to process failed lists, just attempt to process them again from C++ if the process is still alive. This also allows the removal of domain specific code in timers. (The current behaviour is not quite ideal as it means that all lists after the failed one will process on an arbitrary nextTick, even if they're — say — not due to fire for another 2 days...)

  2. Pass in Timer.now() as an argument of kOnTimeout instead of always re-entering C++ to get it. Also don't constantly call Timer.now() from ontimeout, even when it isn't needed. Improves performance on our pooled benchmark by roughly 40%.

  3. Switch to use const where appropriate.

  4. Instead of calling stop + start from the refresh method of Timeout, instead just call start as libuv already calls stop if necessary.

  5. When an interval takes exactly as long to run as its timeout setting, it can sometimes block the event loop if exactly 1ms elapses before start() is called. This commit fixes that.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

timers

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@apapirovski apapirovski added c++ Issues and PRs that require attention from people who are familiar with C++. domain Issues and PRs related to the domain subsystem. timers Issues and PRs related to timers, setImmediate(), setInterval(), and setTimeout(). semver-major PRs that contain breaking changes and should be released in the next major version. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. labels Jan 31, 2018
@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. labels Jan 31, 2018
@apapirovski

apapirovski commented Jan 31, 2018

Copy link
Copy Markdown
Contributor Author

CI: https://ci.nodejs.org/job/node-test-pull-request/12855/
CitGM: https://ci.nodejs.org/view/Node.js-citgm/job/citgm-smoker/1248/
Benchmark CI: https://ci.nodejs.org/view/Node.js%20benchmark/job/benchmark-node-micro-benchmarks/112/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

jasnell
jasnell approved these changes Jan 31, 2018
View reviewed changes
BridgeAR
BridgeAR approved these changes Feb 1, 2018
View reviewed changes

@BridgeAR BridgeAR left a comment

Copy link
Copy Markdown
Member

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.

Nice!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Fishrock123
Fishrock123 self-requested a review February 1, 2018 18:14
Fishrock123
Fishrock123 suggested changes Feb 2, 2018
View reviewed changes

@Fishrock123 Fishrock123 left a comment

Copy link
Copy Markdown
Contributor

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.

Some smaller things. I noticed that the commit descriptions are less informative than what's in your PR here, think you could update them?

Speaking of that, it's pretty nuts that removing Timer.now() has that kind of perf impact, however your PR says 40% but you commit says 80%.. which is it? :P

Oh and, could you explain 5. a bit more? I think I have a hint of what's going on there but I suspect it'd be pretty difficult to understand in any detail.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread test/parallel/test-timers-unref-throw-then-ref.js Outdated

@Fishrock123 Fishrock123 Feb 2, 2018

Copy link
Copy Markdown
Contributor

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.

common.mustCall() doesn't work here? It checks for one and only one call by default.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@apapirovski apapirovski Feb 2, 2018

Copy link
Copy Markdown
Contributor Author

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.

It can potentially cause an infinite loop since it only checks at exit and this tests the fact that calling ref on unref'd handle doesn't cause the C++ loop to continue forever.

This is related to the owner check in C++. Someone could, in the future, think to optimize that by checking whether the handle is ref or unref, instead of checking for presence of an owner, but that would actually be incorrect and would cause an infinite loop on this test.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread test/sequential/test-timers-set-interval-excludes-callback-duration.js Outdated

@Fishrock123 Fishrock123 Feb 2, 2018

Copy link
Copy Markdown
Contributor

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.

Should these new callbacks also have common.mustCall()?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@apapirovski apapirovski Feb 2, 2018

Copy link
Copy Markdown
Contributor Author

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 opted not to include since setImmediate is guaranteed to run and if it doesn't, others tests will fail.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Fishrock123 Fishrock123 Feb 2, 2018

Copy link
Copy Markdown
Contributor

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.

Mmm yes but I'm not sure that's guaranteed to be true for all APIs so we do need to be careful about that path of doing things,

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@apapirovski

apapirovski commented Feb 2, 2018

Copy link
Copy Markdown
Contributor Author

Some smaller things. I noticed that the commit descriptions are less informative than what's in your PR here, think you could update them?

Yep, no worries. Will update later today.

Speaking of that, it's pretty nuts that removing Timer.now() has that kind of perf impact, however your PR says 40% but you commit says 80%.. which is it? :P

Not sure. I got 80% on my local machine but I'm getting 40% on benchmark CI but also 10% on a couple of other tests which I didn't see locally. From doing other benchmark runs in the past, I suspect that some of our performance gains can be platform specific (for example, frequency of GC would be one consideration).

In terms of why the big jump, the function itself does very little so constantly re-entering C++ accounts for a huge part of it.

Oh and, could you explain 5. a bit more? I think I have a hint of what's going on there but I suspect it'd be pretty difficult to understand in any detail.

There was a PR recently (e647c5d#diff-0a5d4868b2b9b17cf9e2c11f1bd1311e) that fixed the case where an interval executing longer than its timeout value would block the event loop. It solved that issue for most cases except where the trip from rearm to the code here: https://github.com/apapirovski/node/blob/4746614757b8881523211c40dc200fcc2f459084/lib/timers.js#L237-L245 takes exactly 1ms to complete. Then the timeRemaining = 0 and the interval would execute on the same cycle of the event loop.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fishrock123

Fishrock123 commented Feb 2, 2018

Copy link
Copy Markdown
Contributor

Not sure. I got 80% on my local machine but I'm getting 40% on benchmark CI but also 10% on a couple of other tests which I didn't see locally. From doing other benchmark runs in the past, I suspect that some of our performance gains can be platform specific (for example, frequency of GC would be one consideration).

Imo put down something like up to 80% improvement or 40%-80% improvement, or 40%+ improvement.

  1. (...)

Ah yes, thanks.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Fishrock123
Fishrock123 approved these changes Feb 2, 2018
View reviewed changes
apapirovski added 5 commits February 2, 2018 16:42
@apapirovski
timers: refactor error handling
0f0304b
Instead of using nextTick to process failed lists, just attempt to
process them again from C++ if the process is still alive.

This also allows the removal of domain specific code in timers.

The current behaviour is not quite ideal as it means that all lists
after the failed one will process on an arbitrary nextTick, even if
they're — say — not due to fire for another 2 days...
@apapirovski
timers: re-enter C++ less frequently
cdf6da5
Pass in Timer.now() as an argument of kOnTimeout instead of always
re-entering C++ to get it. Also don't constantly call Timer.now()
from ontimeout, even when it isn't needed. Improves performance
on our pooled benchmark by upwards of 40%.
@apapirovski
timers: use const as appropriate
8c6953d
@apapirovski
timers: use start instead of stop + start
987d96e
Instead of calling stop + start from the refresh method
of Timeout, instead just call start as libuv already
calls stop if necessary.
@apapirovski
timers: prevent event loop blocking
4dacb85
When an interval takes as long or longer to run as its timeout setting
and the roundtrip from rearm() to its deferal takes exactly 1ms, that
interval can then block the event loop. This is an edge case of another
recently fixed bug (which in itself was an edge case).

Refs: nodejs#15072
@apapirovski
apapirovski force-pushed the patch-timers-improvements branch from 4746614 to 4dacb85 Compare February 2, 2018 21:47
@apapirovski

apapirovski commented Feb 2, 2018

Copy link
Copy Markdown
Contributor Author

I've updated the commit messages with a bit more info.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@apapirovski

apapirovski commented Feb 4, 2018

Copy link
Copy Markdown
Contributor Author

Landed in 9b8e1c2, a986158, 71c0d03, d7894f3, and 47a984a

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@apapirovski apapirovski closed this Feb 4, 2018
@apapirovski
apapirovski deleted the patch-timers-improvements branch February 4, 2018 16:05
apapirovski added a commit that referenced this pull request Feb 4, 2018
@apapirovski
timers: refactor error handling
9b8e1c2
Instead of using nextTick to process failed lists, just attempt to
process them again from C++ if the process is still alive.

This also allows the removal of domain specific code in timers.

The current behaviour is not quite ideal as it means that all lists
after the failed one will process on an arbitrary nextTick, even if
they're — say — not due to fire for another 2 days...

PR-URL: #18486
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Jeremiah Senkpiel <[email protected]>
apapirovski added a commit that referenced this pull request Feb 4, 2018
@apapirovski
timers: re-enter C++ less frequently
a986158
Pass in Timer.now() as an argument of kOnTimeout instead of always
re-entering C++ to get it. Also don't constantly call Timer.now()
from ontimeout, even when it isn't needed. Improves performance
on our pooled benchmark by upwards of 40%.

PR-URL: #18486
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Jeremiah Senkpiel <[email protected]>
apapirovski added a commit that referenced this pull request Feb 4, 2018
@apapirovski
timers: use const as appropriate
71c0d03
PR-URL: #18486
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Jeremiah Senkpiel <[email protected]>
apapirovski added a commit that referenced this pull request Feb 4, 2018
@apapirovski
timers: use start instead of stop + start
d7894f3
Instead of calling stop + start from the refresh method
of Timeout, instead just call start as libuv already
calls stop if necessary.

PR-URL: #18486
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Jeremiah Senkpiel <[email protected]>
apapirovski added a commit that referenced this pull request Feb 4, 2018
@apapirovski
timers: prevent event loop blocking
47a984a
When an interval takes as long or longer to run as its timeout setting
and the roundtrip from rearm() to its deferal takes exactly 1ms, that
interval can then block the event loop. This is an edge case of another
recently fixed bug (which in itself was an edge case).

PR-URL: #18486
Refs: #15072
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Jeremiah Senkpiel <[email protected]>
@dyatlov

dyatlov commented Apr 8, 2018

Copy link
Copy Markdown
Contributor

When it's going to be included in Node release?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@apapirovski apapirovski mentioned this pull request Apr 12, 2018
Unreferenced timer started in uncaughtException handler fires immediately if exception was thrown from inside a timer #19970
Closed
MayaLekova pushed a commit to MayaLekova/node that referenced this pull request May 8, 2018
@apapirovski @MayaLekova
timers: refactor error handling
248d984
Instead of using nextTick to process failed lists, just attempt to
process them again from C++ if the process is still alive.

This also allows the removal of domain specific code in timers.

The current behaviour is not quite ideal as it means that all lists
after the failed one will process on an arbitrary nextTick, even if
they're — say — not due to fire for another 2 days...

PR-URL: nodejs#18486
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Jeremiah Senkpiel <[email protected]>
MayaLekova pushed a commit to MayaLekova/node that referenced this pull request May 8, 2018
@apapirovski @MayaLekova
timers: re-enter C++ less frequently
b531515
Pass in Timer.now() as an argument of kOnTimeout instead of always
re-entering C++ to get it. Also don't constantly call Timer.now()
from ontimeout, even when it isn't needed. Improves performance
on our pooled benchmark by upwards of 40%.

PR-URL: nodejs#18486
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Jeremiah Senkpiel <[email protected]>
MayaLekova pushed a commit to MayaLekova/node that referenced this pull request May 8, 2018
@apapirovski @MayaLekova
timers: use const as appropriate
975032a
PR-URL: nodejs#18486
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Jeremiah Senkpiel <[email protected]>
MayaLekova pushed a commit to MayaLekova/node that referenced this pull request May 8, 2018
@apapirovski @MayaLekova
timers: use start instead of stop + start
52d2876
Instead of calling stop + start from the refresh method
of Timeout, instead just call start as libuv already
calls stop if necessary.

PR-URL: nodejs#18486
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Jeremiah Senkpiel <[email protected]>
MayaLekova pushed a commit to MayaLekova/node that referenced this pull request May 8, 2018
@apapirovski @MayaLekova
timers: prevent event loop blocking
2e8be1d
When an interval takes as long or longer to run as its timeout setting
and the roundtrip from rearm() to its deferal takes exactly 1ms, that
interval can then block the event loop. This is an edge case of another
recently fixed bug (which in itself was an edge case).

PR-URL: nodejs#18486
Refs: nodejs#15072
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Jeremiah Senkpiel <[email protected]>
@ChALkeR ChALkeR mentioned this pull request Aug 9, 2018
src: fix integer overflow in GetNow #22214
Closed
3 tasks done
@jhuckaby jhuckaby mentioned this pull request Aug 31, 2018
Cronicle scheduler stops working after 2^31 milliseconds (~25 days) of continuous running jhuckaby/Cronicle#108
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@jasnell jasnell jasnell approved these changes
@BridgeAR BridgeAR BridgeAR approved these changes
+1 more reviewer
@Fishrock123 Fishrock123 Fishrock123 approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. domain Issues and PRs related to the domain subsystem. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. semver-major PRs that contain breaking changes and should be released in the next major version. timers Issues and PRs related to timers, setImmediate(), setInterval(), and setTimeout().

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@apapirovski @Fishrock123 @dyatlov @jasnell @BridgeAR @nodejs-github-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.