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: (cpus) pre-allocate the size of the result array #57059

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gurgunday
Copy link
Member

No description provided.

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. os Issues and PRs related to the os subsystem. labels Feb 15, 2025
@@ -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);
Copy link
Member

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.

7 comes from the return type of getCPUs:

node/lib/os.js

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++],

Copy link
Member

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.

Copy link
Member

@juanarbol juanarbol left a 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++] = {
Copy link
Member

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?

Copy link
Member Author

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

Copy link
Member

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.

Copy link
Member Author

@gurgunday gurgunday Feb 15, 2025

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

Copy link
Member Author

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?

Copy link
Member

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.

Copy link

codecov bot commented Feb 15, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.10%. Comparing base (cc7018e) to head (2c09633).

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     
Files with missing lines Coverage Δ
lib/os.js 97.33% <100.00%> (+0.01%) ⬆️

... and 36 files with indirect coverage changes

@gurgunday
Copy link
Member Author

@anonrig PTAL, it should be an easy win in my opinion as the Array size is known in advance

@gurgunday gurgunday requested a review from juanarbol February 23, 2025 12:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-ci PRs that need a full CI run. os Issues and PRs related to the os subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants