Skip to content

timers: improve setImmediate() performance - #8655

Merged
mscdex merged 1 commit into
nodejs:masterfrom
mscdex:timers-improve-immediate-perf
Oct 5, 2016
Merged

timers: improve setImmediate() performance#8655
mscdex merged 1 commit into
nodejs:masterfrom
mscdex:timers-improve-immediate-perf

Conversation

@mscdex

@mscdexmscdex commented Sep 19, 2016

Copy link
Copy Markdown
Contributor
Checklist
  • make -j4 test (UNIX), or vcbuild test nosign (Windows) passes
  • commit message follows commit guidelines
Affected core subsystem(s)
  • timers
Description of change

This commit avoids re-creating a new immediate queue object every time the immediate queue is processed. Additionally, a few functions are tweaked to make them inlineable.

These changes result in ~6-7% improvement in the existing setImmediate() benchmarks and should help reduce GC work.

@nodejs-github-botnodejs-github-bot added the timers Issues and PRs related to the timers subsystem / setImmediate, setInterval, setTimeout. label Sep 19, 2016
Comment threadlib/timers.js Outdated

@Fishrock123Fishrock123Sep 19, 2016

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p is poorly named

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to ptr. Is that sufficient?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just keep it as immediate?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@mscdex
mscdexforce-pushed the timers-improve-immediate-perf branch from 5f4c590 to a284f41CompareSeptember 19, 2016 17:59
@mscdex

Copy link
Copy Markdown
ContributorAuthor

@mscdex
mscdexforce-pushed the timers-improve-immediate-perf branch from a284f41 to dcc9fe0CompareSeptember 19, 2016 23:04
@mscdex

mscdex commented Sep 19, 2016

Copy link
Copy Markdown
ContributorAuthor

I've now tweaked a few of the functions used by setImmediate() and others to be inlineable, which gives another ~3% boost in performance, upping the improvement to ~6-7%.

@mscdex

Copy link
Copy Markdown
ContributorAuthor

Comment threadlib/internal/process/promises.js Outdated

@richardlaurichardlauSep 20, 2016

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uid and reason not defined here.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@mscdex
mscdexforce-pushed the timers-improve-immediate-perf branch from dcc9fe0 to e66340aCompareSeptember 20, 2016 06:25
@mscdex

Copy link
Copy Markdown
ContributorAuthor

Comment threadlib/internal/process/promises.js Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mscdex err, why is this included?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To allow for more inlining. emitPendingUnhandledRejections() is used by setImmediate().

@jasnelljasnell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@imyllerimyller left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jasnell

Copy link
Copy Markdown
Member

ping @Fishrock123 ... does this LGTY?

@Fishrock123Fishrock123 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functionality LGTM, I'd like some commenting around the new linkedlist stuff personally, I find them notoriously hard to read / understand and I doubt I am alone.

OK otherwise if CI passes.

Comment threadlib/timers.js Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a comment would be helpful

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What did you have in mind? To me it's self-explanatory, it's effectively clearing the linked list.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know what it does, I think it just may look strange to someone else encountering it.

Comment threadlib/timers.js Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be a helper fn?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean?

Comment threadlib/timers.js Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I probably should specify: comments on how these two modify their respective properties

@mscdex
mscdexforce-pushed the timers-improve-immediate-perf branch from e66340a to 67f1a2aCompareSeptember 29, 2016 08:15
@mscdex

Copy link
Copy Markdown
ContributorAuthor

@Fishrock123 Comments added, let me know if they are sufficient.

@mscdex

Copy link
Copy Markdown
ContributorAuthor

ping @Fishrock123

@Fishrock123Fishrock123 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mscdex

mscdex commented Oct 5, 2016

Copy link
Copy Markdown
ContributorAuthor

CI before landing: https://ci.nodejs.org/job/node-test-pull-request/4389/

EDIT: checking again after rebasing just to be sure flakiness is unrelated: https://ci.nodejs.org/job/node-test-commit/5451/

@mscdex
mscdexforce-pushed the timers-improve-immediate-perf branch from 67f1a2a to 1bb9048CompareOctober 5, 2016 06:43
This commit avoids re-creating a new immediate queue object every
time the immediate queue is processed. Additionally, a few functions
are tweaked to make them inlineable.
These changes give ~6-7% boost in setImmediate() performance in the
existing setImmediate() benchmarks.
PR-URL: nodejs#8655
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
@mscdex
mscdexforce-pushed the timers-improve-immediate-perf branch from 1bb9048 to 0ed8839CompareOctober 5, 2016 07:11
@mscdex
mscdex merged commit 0ed8839 into nodejs:masterOct 5, 2016
@mscdex
mscdex deleted the timers-improve-immediate-perf branch October 5, 2016 07:13
jasnell pushed a commit that referenced this pull request Oct 6, 2016
This commit avoids re-creating a new immediate queue object every
time the immediate queue is processed. Additionally, a few functions
are tweaked to make them inlineable.
These changes give ~6-7% boost in setImmediate() performance in the
existing setImmediate() benchmarks.
PR-URL: #8655
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
@MylesBorins

Copy link
Copy Markdown
Contributor

adding to lts watch but unsure if this should land. Will definitely need time to sit in a release for a while

/cc @mscdex

Fishrock123 pushed a commit that referenced this pull request Oct 11, 2016
This commit avoids re-creating a new immediate queue object every
time the immediate queue is processed. Additionally, a few functions
are tweaked to make them inlineable.
These changes give ~6-7% boost in setImmediate() performance in the
existing setImmediate() benchmarks.
PR-URL: #8655
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Conflicts:
lib/internal/process/promises.js
@ErisDS

Copy link
Copy Markdown

Hey there!

We started having test failures due to this change. There's more information on the issue & PR linked just above this comment. To pull some of that here:

The last job never get's executed, because setImmediate does not get triggered!

We have a temporary fix in place in the PR, that reduces the timeout and this appears to work (the tests pass). Would love a little bit of input into how this change has impacted the functionality and whether we've found a bug or are doing something wrong :)

@MylesBorins

Copy link
Copy Markdown
Contributor

/cc @nodejs/ctc

@Trott

Copy link
Copy Markdown
Member

@ErisDS I don't suppose you have a minimal test that could be used to demonstrate the immediate running in v6.7.0 and not running in v6.8.0, do you? I'm guessing not because you probably would have mentioned it, but I'm having trouble replicating the problem, probably because I am Doing Something Wrong.

@kirrg001

Copy link
Copy Markdown

@Trott i will provide an example file asap

@kirrg001

kirrg001 commented Oct 13, 2016

Copy link
Copy Markdown
var jobs = [Date.now() + 1000, Date.now() + 2000, Date.now() + 3000];
jobs.forEach(function(timestamp) {
var timeout = setTimeout(function() {
clearTimeout(timeout);
(function retry() {
var immediate = setImmediate(function() {
clearImmediate(immediate);
if (Date.now() < timestamp) {
return retry();
}
console.log("FINISHED JOB");
});
}());
}, timestamp - 200);
});

v6.8.0 - does not work, you will see 1 x FINISHED JOB
v4.4.7 && v6.7.0 - works as expected, you will see 3 x FINISHED JOB

@Fishrock123

Fishrock123 commented Oct 13, 2016

Copy link
Copy Markdown
Contributor

Moving to a new issue so we can help you better: #9084

please put all comments in that thread rather than here

@nodejsnodejs locked and limited conversation to collaborators Oct 13, 2016
@nodejsnodejs unlocked this conversation Oct 13, 2016
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

timersIssues and PRs related to the timers subsystem / setImmediate, setInterval, setTimeout.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants

@mscdex@jasnell@MylesBorins@ErisDS@Trott@kirrg001@Fishrock123@imyller@richardlau@nodejs-github-bot