diff options
author | Gireesh Punathil <gpunathi@in.ibm.com> | 2019-02-18 08:35:40 -0500 |
---|---|---|
committer | Daniel Bevenius <daniel.bevenius@gmail.com> | 2019-02-21 08:45:40 +0100 |
commit | e51da1fcadc822fb514cd6b32e932d7c0c3239a7 (patch) | |
tree | 3f3be517ff6654480786fb76ab79c60537ba1557 | |
parent | 24f3f1160659ebb83416a2c779abf9d0e87ed0d6 (diff) | |
download | android-node-v8-e51da1fcadc822fb514cd6b32e932d7c0c3239a7.tar.gz android-node-v8-e51da1fcadc822fb514cd6b32e932d7c0c3239a7.tar.bz2 android-node-v8-e51da1fcadc822fb514cd6b32e932d7c0c3239a7.zip |
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>
-rw-r--r-- | lib/os.js | 17 | ||||
-rw-r--r-- | src/node_os.cc | 16 |
2 files changed, 17 insertions, 16 deletions
@@ -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++] } }); } diff --git a/src/node_os.cc b/src/node_os.cc index fa27b7bac5..1d3680a632 100644 --- a/src/node_os.cc +++ b/src/node_os.cc @@ -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); |