Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
src: combine loops in CopyJsStringArray()
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: #15380
PR-URL: #16247
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
cjihrig committed Oct 24, 2017
commit c4f2898b68a7b5429fdfec71d5544afe8e0feb6e
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