diff options
author | Yash Ladha <yashladhapankajladha123@gmail.com> | 2021-01-03 04:53:15 +0300 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2021-01-19 00:41:29 +0300 |
commit | cb2535ef7ff9e61d827dc0f026f102a132740041 (patch) | |
tree | 4cd93e96b83df3c781e8c34389f1e1a057f3fe46 /src/node_os.cc | |
parent | 341bbd383164d99a7648a7568f7db4d377ca7a7f (diff) |
os: performance improvement in vector allocation
We were using the result vector with an object which is not a primitive
data type, and going with the constructor allocation pattern it creates
a size of that vector and also initializes the spaces with the data type
as well which is in our case is `Local<Value>`. It leads to waste of
some CPU cycles and instead we just wanted to have some reserved space
in our vector.
We can use `reserve` method on vector to reserve some space for the
vector but doesn't initialize the value since we are anyways doing it in
the following loop.
PR-URL: https://github.com/nodejs/node/pull/36748
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src/node_os.cc')
-rw-r--r-- | src/node_os.cc | 40 |
1 files changed, 21 insertions, 19 deletions
diff --git a/src/node_os.cc b/src/node_os.cc index 2e151ac4f89..3cde80996f0 100644 --- a/src/node_os.cc +++ b/src/node_os.cc @@ -112,16 +112,17 @@ static void GetCPUInfo(const FunctionCallbackInfo<Value>& args) { // assemble them into objects in JS than to call Object::Set() repeatedly // 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, j = 0; i < count; i++) { + std::vector<Local<Value>> result; + result.reserve(count * 7); + for (int i = 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); - 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); + result.emplace_back(OneByteString(isolate, ci->model)); + result.emplace_back(Number::New(isolate, ci->speed)); + result.emplace_back(Number::New(isolate, ci->cpu_times.user)); + result.emplace_back(Number::New(isolate, ci->cpu_times.nice)); + result.emplace_back(Number::New(isolate, ci->cpu_times.sys)); + result.emplace_back(Number::New(isolate, ci->cpu_times.idle)); + result.emplace_back(Number::New(isolate, ci->cpu_times.irq)); } uv_free_cpu_info(cpu_infos, count); @@ -182,7 +183,8 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) { } Local<Value> no_scope_id = Integer::New(isolate, -1); - std::vector<Local<Value>> result(count * 7); + std::vector<Local<Value>> result; + result.reserve(count * 7); for (i = 0; i < count; i++) { const char* const raw_name = interfaces[i].name; @@ -216,18 +218,18 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) { family = env->unknown_string(); } - result[i * 7] = name; - result[i * 7 + 1] = OneByteString(isolate, ip); - result[i * 7 + 2] = OneByteString(isolate, netmask); - result[i * 7 + 3] = family; - result[i * 7 + 4] = FIXED_ONE_BYTE_STRING(isolate, mac); - result[i * 7 + 5] = - interfaces[i].is_internal ? True(isolate) : False(isolate); + result.emplace_back(name); + result.emplace_back(OneByteString(isolate, ip)); + result.emplace_back(OneByteString(isolate, netmask)); + result.emplace_back(family); + result.emplace_back(FIXED_ONE_BYTE_STRING(isolate, mac)); + result.emplace_back( + interfaces[i].is_internal ? True(isolate) : False(isolate)); if (interfaces[i].address.address4.sin_family == AF_INET6) { uint32_t scopeid = interfaces[i].address.address6.sin6_scope_id; - result[i * 7 + 6] = Integer::NewFromUnsigned(isolate, scopeid); + result.emplace_back(Integer::NewFromUnsigned(isolate, scopeid)); } else { - result[i * 7 + 6] = no_scope_id; + result.emplace_back(no_scope_id); } } |