Skip to content
This repository has been archived by the owner on Aug 31, 2018. It is now read-only.

Commit

Permalink
src: combine loops in CopyJsStringArray()
Browse files Browse the repository at this point in the history
In spawn_sync.cc, two consecutive loops are used to convert
data to strings, and compute the size of the data. This commit
merges the two independent loops into one.

Refs: nodejs/node#15380
PR-URL: nodejs/node#16247
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
cjihrig authored and addaleax committed Oct 26, 2017
1 parent 9934df1 commit 760d5e9
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/spawn_sync.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1016,25 +1016,22 @@ int SyncProcessRunner::CopyJsStringArray(Local<Value> js_value,
Local<Context> context = env()->context();
js_array = js_value.As<Array>()->Clone().As<Array>();
length = js_array->Length();
data_size = 0;

// Index has a pointer to every string element, plus one more for a final
// null pointer.
list_size = (length + 1) * sizeof *list;

// Convert all array elements to string. Modify the js object itself if
// needed - it's okay since we cloned the original object.
// needed - it's okay since we cloned the original object. Also compute the
// length of all strings, including room for a null terminator after every
// string. Align strings to cache lines.
for (uint32_t i = 0; i < length; i++) {
auto value = js_array->Get(context, i).ToLocalChecked();

if (!value->IsString())
js_array->Set(context, i, value->ToString(env()->isolate())).FromJust();
}

// Index has a pointer to every string element, plus one more for a final
// null pointer.
list_size = (length + 1) * sizeof *list;

// Compute the length of all strings. Include room for null terminator
// after every string. Align strings to cache lines.
data_size = 0;
for (uint32_t i = 0; i < length; i++) {
auto value = js_array->Get(context, i).ToLocalChecked();
data_size += StringBytes::StorageSize(isolate, value, UTF8) + 1;
data_size = ROUND_UP(data_size, sizeof(void*));
}
Expand Down

0 comments on commit 760d5e9

Please sign in to comment.