-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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: (cpus) pre-allocate the size of the result array #57059
base: main
Are you sure you want to change the base?
Conversation
@@ -140,10 +141,12 @@ function loadavg() { | |||
function cpus() { | |||
// [] is a bugfix for a regression introduced in 51cea61 | |||
const data = getCPUs() || []; | |||
const result = []; | |||
const result = new Array(data.length / 7); |
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.
?
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.
7 comes from the return type of getCPUs
:
Lines 150 to 157 in 2c09633
model: data[i++], | |
speed: data[i++], | |
times: { | |
user: data[i++], | |
nice: data[i++], | |
sys: data[i++], | |
idle: data[i++], | |
irq: data[i++], |
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.
Oh yeah, makes sense. Sorry.
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.
Any particular reason of this patch?
while (i < data.length) { | ||
ArrayPrototypePush(result, { | ||
result[resultIndex++] = { |
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.
Why not use the Primordial as the rest of the codebase?
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.
Yeah so this is one of the smaller things I've seen in the codebase, sometimes we construct and push into arrays that we already know the size of
Pre-allocating the array with the size and filling it should be faster
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.
Yeah, I'm sorry for being like this, would you mid writing a simple benchmark then? V8 is a bit weird sometimes, it does not like common sense sometimes.
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.
Something like this?
function bench(size) {
// Push method
let start = performance.now();
const arr1 = [];
for (let i = 0; i < size; i++) {
arr1.push(i);
}
console.log(`push: ${(performance.now() - start).toFixed(2)}ms`);
// Pre-allocated array
start = performance.now();
const arr2 = new Array(size);
for (let i = 0; i < size; i++) {
arr2[i] = i;
}
console.log(`pre-allocated: ${(performance.now() - start).toFixed(2)}ms`);
}
// Run benchmark with 1 million elements
bench(10_000_000);
Chrome 133
VM83:8 push: 54.10ms
VM83:16 pre-allocated: 23.50ms
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.
Or should I add a bench to Node.js?
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 think a benchmark that iterates over the returned CPU data and do something with it, say doing result &&= (cpus[i].speed > 2000)
, would be more realistic. Pre-allocating an array and making it holely can affect the performance of indexing due to change of element kinds.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #57059 +/- ##
=======================================
Coverage 89.10% 89.10%
=======================================
Files 665 665
Lines 193203 193206 +3
Branches 37216 37216
=======================================
+ Hits 172158 172164 +6
- Misses 13768 13778 +10
+ Partials 7277 7264 -13
|
@anonrig PTAL, it should be an easy win in my opinion as the Array size is known in advance |
No description provided.