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

os: improve networkInterfaces performance #22359

Closed

Conversation

BridgeAR
Copy link
Member

@BridgeAR BridgeAR commented Aug 16, 2018

This algorithm uses less data transformations and is therefore
significantly faster than the one before.

The main pain point is how the CIDR suffix is calculated. It would
be best to port this to C++ instead of having to do this in JS as all other
transformations are done in C++. But since this improves the
performance already quite a bit it's a good first step.

Performance:

> console.time(); for(var i = 0; i < 10000; i++) os.networkInterfaces(); console.timeEnd()

// Before
default: 1170.912ms

// After:
default: 758.899ms
Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

@nodejs-github-bot nodejs-github-bot added build Issues and PRs related to build files or the CI. os Issues and PRs related to the os subsystem. labels Aug 16, 2018
@BridgeAR BridgeAR added the performance Issues and PRs related to the performance of Node.js. label Aug 16, 2018
@BridgeAR BridgeAR force-pushed the improve-comparison-performance branch 2 times, most recently from e7c90b9 to bb4a589 Compare August 16, 2018 13:30
@BridgeAR
Copy link
Member Author

BridgeAR commented Aug 16, 2018

@BridgeAR BridgeAR force-pushed the improve-comparison-performance branch from bb4a589 to e625c9d Compare August 16, 2018 13:38
@mafintosh
Copy link
Member

LGTM, nice!

@BridgeAR
Copy link
Member Author

BridgeAR commented Aug 16, 2018

I just pushed another commit that optimizes the algorithm. It is now a much simpler and straight forward as I calculate the number of ones right away and use modulo 2 to validate the entry.

New benchmark result:

> console.time(); for(var i = 0; i < 10000; i++) os.networkInterfaces(); console.timeEnd()
default: 758.899ms

lib/os.js Outdated Show resolved Hide resolved
@BridgeAR
Copy link
Member Author

BridgeAR commented Aug 16, 2018

@BridgeAR BridgeAR added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Aug 16, 2018
lib/os.js Outdated
}

const parts = netmask.split(split);
for (const part of parts) {
Copy link
Member

Choose a reason for hiding this comment

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

Since you are rewriting this for performance, is for...of as fast as the classic for loop now?

Copy link
Member Author

Choose a reason for hiding this comment

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

lib/os.js Outdated
return null;
}
} else if (tmp !== groupLength) {
if (binary % 2 !== 0) {
Copy link
Member

Choose a reason for hiding this comment

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

You could use (binary & 1) !== 0. Last time I tried the reminder operator was not optimized for this, maybe it is now, don't know.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

lib/os.js Outdated Show resolved Hide resolved
@BridgeAR
Copy link
Member Author

Comments addressed. The performance is now again a tiny bit improved compared to before by using:

  1. Simple for loops
  2. No Object.values
  3. Using the binary operator instead of modulo

@BridgeAR
Copy link
Member Author

This algorithm uses less data transformations and is therefore
significantly faster than the one before.
@BridgeAR BridgeAR force-pushed the improve-comparison-performance branch from 0baa2d7 to 17f4930 Compare August 19, 2018 16:57
@BridgeAR
Copy link
Member Author

BridgeAR commented Aug 19, 2018

Rebased due to a hiccup with the CI. I only squashed commits that were already reviewed.

CI https://ci.nodejs.org/job/node-test-pull-request/16576/ ✔️

@mafintosh
Copy link
Member

Still LGTM

@BridgeAR
Copy link
Member Author

@addaleax @lpinca mind taking another look?

BridgeAR added a commit to BridgeAR/node that referenced this pull request Aug 20, 2018
This algorithm uses less data transformations and is therefore
significantly faster than the one before.

PR-URL: nodejs#22359
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
@BridgeAR
Copy link
Member Author

Landed in 9bcb744 🎉

@BridgeAR BridgeAR closed this Aug 20, 2018
targos pushed a commit that referenced this pull request Aug 21, 2018
This algorithm uses less data transformations and is therefore
significantly faster than the one before.

PR-URL: #22359
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
targos pushed a commit that referenced this pull request Sep 3, 2018
This algorithm uses less data transformations and is therefore
significantly faster than the one before.

PR-URL: #22359
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
@BridgeAR BridgeAR deleted the improve-comparison-performance branch January 20, 2020 11:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. build Issues and PRs related to build files or the CI. os Issues and PRs related to the os subsystem. performance Issues and PRs related to the performance of Node.js.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants