Skip to content

Commit

Permalink
src: simplify loop arithmetic in GetCPUInfo
Browse files Browse the repository at this point in the history
Cache the repeated operations and reuse; potentially generating
efficient code in some platforms, and improving readability.

PR-URL: #26183
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
gireeshpunathil authored and rvagg committed Feb 28, 2019
1 parent ddd71f4 commit 619b5e7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
17 changes: 9 additions & 8 deletions lib/os.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,17 @@ function cpus() {
// [] is a bugfix for a regression introduced in 51cea61
const data = getCPUs() || [];
const result = [];
for (var i = 0; i < data.length; i += 7) {
let i = 0;
while (i < data.length) {
result.push({
model: data[i],
speed: data[i + 1],
model: data[i++],
speed: data[i++],
times: {
user: data[i + 2],
nice: data[i + 3],
sys: data[i + 4],
idle: data[i + 5],
irq: data[i + 6]
user: data[i++],
nice: data[i++],
sys: data[i++],
idle: data[i++],
irq: data[i++]
}
});
}
Expand Down
16 changes: 8 additions & 8 deletions src/node_os.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ static void GetCPUInfo(const FunctionCallbackInfo<Value>& args) {
// The array is in the format
// [model, speed, (5 entries of cpu_times), model2, speed2, ...]
std::vector<Local<Value>> result(count * 7);
for (int i = 0; i < count; i++) {
for (int i = 0, j = 0; i < count; i++) {
uv_cpu_info_t* ci = cpu_infos + i;
result[i * 7] = OneByteString(isolate, ci->model);
result[i * 7 + 1] = Number::New(isolate, ci->speed);
result[i * 7 + 2] = Number::New(isolate, ci->cpu_times.user);
result[i * 7 + 3] = Number::New(isolate, ci->cpu_times.nice);
result[i * 7 + 4] = Number::New(isolate, ci->cpu_times.sys);
result[i * 7 + 5] = Number::New(isolate, ci->cpu_times.idle);
result[i * 7 + 6] = Number::New(isolate, ci->cpu_times.irq);
result[j++] = OneByteString(isolate, ci->model);
result[j++] = Number::New(isolate, ci->speed);
result[j++] = Number::New(isolate, ci->cpu_times.user);
result[j++] = Number::New(isolate, ci->cpu_times.nice);
result[j++] = Number::New(isolate, ci->cpu_times.sys);
result[j++] = Number::New(isolate, ci->cpu_times.idle);
result[j++] = Number::New(isolate, ci->cpu_times.irq);
}

uv_free_cpu_info(cpu_infos, count);
Expand Down

0 comments on commit 619b5e7

Please sign in to comment.