src: simplify loop arithmetic in GetCPUInfo
Cache the repeated operations and reuse; potentially generating efficient code in some platforms, and improving readability. PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
parent
24f3f11606
commit
e51da1fcad
17
lib/os.js
17
lib/os.js
@ -89,16 +89,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++]
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -120,15 +120,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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user