From 80bb198947680b1ea5ab0409fc973cec036ea0bc Mon Sep 17 00:00:00 2001 From: Gireesh Punathil Date: Mon, 18 Feb 2019 08:35:40 -0500 Subject: [PATCH 1/5] src: simplify loop arithmetic in `GetCPUInfo` Cache the repeated operations and reuse; potentially generating efficient code in some platforms, and improving readability. --- src/node_os.cc | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/node_os.cc b/src/node_os.cc index fa27b7bac51b81..91ae86d76584cf 100644 --- a/src/node_os.cc +++ b/src/node_os.cc @@ -121,14 +121,15 @@ static void GetCPUInfo(const FunctionCallbackInfo& args) { // [model, speed, (5 entries of cpu_times), model2, speed2, ...] std::vector> result(count * 7); for (int i = 0; i < count; i++) { + int j = i * 7; 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); From 2427f3edf27902e783c44b151f8bd093b4983e9c Mon Sep 17 00:00:00 2001 From: Gireesh Punathil Date: Mon, 18 Feb 2019 23:57:22 -0500 Subject: [PATCH 2/5] fixup: address review comments --- src/node_os.cc | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/node_os.cc b/src/node_os.cc index 91ae86d76584cf..4d46f23cfa78cb 100644 --- a/src/node_os.cc +++ b/src/node_os.cc @@ -120,8 +120,7 @@ static void GetCPUInfo(const FunctionCallbackInfo& args) { // The array is in the format // [model, speed, (5 entries of cpu_times), model2, speed2, ...] std::vector> result(count * 7); - for (int i = 0; i < count; i++) { - int j = i * 7; + for (int i = 0, j = 0; i < count; i++, j = i * 7) { uv_cpu_info_t* ci = cpu_infos + i; result[j++] = OneByteString(isolate, ci->model); result[j++] = Number::New(isolate, ci->speed); @@ -129,7 +128,7 @@ static void GetCPUInfo(const FunctionCallbackInfo& args) { 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); + result[j++] = Number::New(isolate, ci->cpu_times.irq); } uv_free_cpu_info(cpu_infos, count); From 23cafc976d3833fc013d8230acfd887558d86e6c Mon Sep 17 00:00:00 2001 From: Gireesh Punathil Date: Tue, 19 Feb 2019 01:10:43 -0500 Subject: [PATCH 3/5] fixup: address review comments 2 --- src/node_os.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node_os.cc b/src/node_os.cc index 4d46f23cfa78cb..1d3680a6322fdb 100644 --- a/src/node_os.cc +++ b/src/node_os.cc @@ -120,7 +120,7 @@ static void GetCPUInfo(const FunctionCallbackInfo& args) { // The array is in the format // [model, speed, (5 entries of cpu_times), model2, speed2, ...] std::vector> result(count * 7); - for (int i = 0, j = 0; i < count; i++, j = i * 7) { + for (int i = 0, j = 0; i < count; i++) { uv_cpu_info_t* ci = cpu_infos + i; result[j++] = OneByteString(isolate, ci->model); result[j++] = Number::New(isolate, ci->speed); From 1666fa5a0fcce730d8ea11fef48e164ab96b1655 Mon Sep 17 00:00:00 2001 From: Gireesh Punathil Date: Tue, 19 Feb 2019 08:40:13 -0500 Subject: [PATCH 4/5] fixup: address review comments --- lib/os.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/os.js b/lib/os.js index 9886e2c3158bc8..ed97e1163ef87a 100644 --- a/lib/os.js +++ b/lib/os.js @@ -89,16 +89,16 @@ 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) { + for (var i = 0, j = 0; i < data.length; i++) { result.push({ - model: data[i], - speed: data[i + 1], + model: data[j++], + speed: data[j++], times: { - user: data[i + 2], - nice: data[i + 3], - sys: data[i + 4], - idle: data[i + 5], - irq: data[i + 6] + user: data[j++], + nice: data[j++], + sys: data[j++], + idle: data[j++], + irq: data[j++] } }); } From f1798dd77aacc80c2cbaaf4d6b0d6b6017c9bb19 Mon Sep 17 00:00:00 2001 From: Gireesh Punathil Date: Tue, 19 Feb 2019 09:14:51 -0500 Subject: [PATCH 5/5] fixup: correct the loop iteration variable --- lib/os.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/os.js b/lib/os.js index ed97e1163ef87a..d6ecd29f57a7e1 100644 --- a/lib/os.js +++ b/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, j = 0; i < data.length; i++) { + let i = 0; + while (i < data.length) { result.push({ - model: data[j++], - speed: data[j++], + model: data[i++], + speed: data[i++], times: { - user: data[j++], - nice: data[j++], - sys: data[j++], - idle: data[j++], - irq: data[j++] + user: data[i++], + nice: data[i++], + sys: data[i++], + idle: data[i++], + irq: data[i++] } }); }