Skip to content

Commit d3f7280

Browse files
yashLadharuyadorno
authored andcommitted
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: #36748 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 3d20415 commit d3f7280

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

src/node_os.cc

+21-19
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,17 @@ static void GetCPUInfo(const FunctionCallbackInfo<Value>& args) {
112112
// assemble them into objects in JS than to call Object::Set() repeatedly
113113
// The array is in the format
114114
// [model, speed, (5 entries of cpu_times), model2, speed2, ...]
115-
std::vector<Local<Value>> result(count * 7);
116-
for (int i = 0, j = 0; i < count; i++) {
115+
std::vector<Local<Value>> result;
116+
result.reserve(count * 7);
117+
for (int i = 0; i < count; i++) {
117118
uv_cpu_info_t* ci = cpu_infos + i;
118-
result[j++] = OneByteString(isolate, ci->model);
119-
result[j++] = Number::New(isolate, ci->speed);
120-
result[j++] = Number::New(isolate, ci->cpu_times.user);
121-
result[j++] = Number::New(isolate, ci->cpu_times.nice);
122-
result[j++] = Number::New(isolate, ci->cpu_times.sys);
123-
result[j++] = Number::New(isolate, ci->cpu_times.idle);
124-
result[j++] = Number::New(isolate, ci->cpu_times.irq);
119+
result.emplace_back(OneByteString(isolate, ci->model));
120+
result.emplace_back(Number::New(isolate, ci->speed));
121+
result.emplace_back(Number::New(isolate, ci->cpu_times.user));
122+
result.emplace_back(Number::New(isolate, ci->cpu_times.nice));
123+
result.emplace_back(Number::New(isolate, ci->cpu_times.sys));
124+
result.emplace_back(Number::New(isolate, ci->cpu_times.idle));
125+
result.emplace_back(Number::New(isolate, ci->cpu_times.irq));
125126
}
126127

127128
uv_free_cpu_info(cpu_infos, count);
@@ -182,7 +183,8 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) {
182183
}
183184

184185
Local<Value> no_scope_id = Integer::New(isolate, -1);
185-
std::vector<Local<Value>> result(count * 7);
186+
std::vector<Local<Value>> result;
187+
result.reserve(count * 7);
186188
for (i = 0; i < count; i++) {
187189
const char* const raw_name = interfaces[i].name;
188190

@@ -216,18 +218,18 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) {
216218
family = env->unknown_string();
217219
}
218220

219-
result[i * 7] = name;
220-
result[i * 7 + 1] = OneByteString(isolate, ip);
221-
result[i * 7 + 2] = OneByteString(isolate, netmask);
222-
result[i * 7 + 3] = family;
223-
result[i * 7 + 4] = FIXED_ONE_BYTE_STRING(isolate, mac);
224-
result[i * 7 + 5] =
225-
interfaces[i].is_internal ? True(isolate) : False(isolate);
221+
result.emplace_back(name);
222+
result.emplace_back(OneByteString(isolate, ip));
223+
result.emplace_back(OneByteString(isolate, netmask));
224+
result.emplace_back(family);
225+
result.emplace_back(FIXED_ONE_BYTE_STRING(isolate, mac));
226+
result.emplace_back(
227+
interfaces[i].is_internal ? True(isolate) : False(isolate));
226228
if (interfaces[i].address.address4.sin_family == AF_INET6) {
227229
uint32_t scopeid = interfaces[i].address.address6.sin6_scope_id;
228-
result[i * 7 + 6] = Integer::NewFromUnsigned(isolate, scopeid);
230+
result.emplace_back(Integer::NewFromUnsigned(isolate, scopeid));
229231
} else {
230-
result[i * 7 + 6] = no_scope_id;
232+
result.emplace_back(no_scope_id);
231233
}
232234
}
233235

0 commit comments

Comments
 (0)