Skip to content

Commit 78a6ef4

Browse files
cjihrigMylesBorins
authored andcommitted
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. Backport-PR-URL: #16426 Refs: #15380 PR-URL: #16247 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 5dbefe1 commit 78a6ef4

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

src/spawn_sync.cc

+8-11
Original file line numberDiff line numberDiff line change
@@ -1016,25 +1016,22 @@ int SyncProcessRunner::CopyJsStringArray(Local<Value> js_value,
10161016
Local<Context> context = env()->context();
10171017
js_array = js_value.As<Array>()->Clone().As<Array>();
10181018
length = js_array->Length();
1019+
data_size = 0;
1020+
1021+
// Index has a pointer to every string element, plus one more for a final
1022+
// null pointer.
1023+
list_size = (length + 1) * sizeof *list;
10191024

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

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

1029-
// Index has a pointer to every string element, plus one more for a final
1030-
// null pointer.
1031-
list_size = (length + 1) * sizeof *list;
1032-
1033-
// Compute the length of all strings. Include room for null terminator
1034-
// after every string. Align strings to cache lines.
1035-
data_size = 0;
1036-
for (uint32_t i = 0; i < length; i++) {
1037-
auto value = js_array->Get(context, i).ToLocalChecked();
10381035
data_size += StringBytes::StorageSize(isolate, value, UTF8) + 1;
10391036
data_size = ROUND_UP(data_size, sizeof(void*));
10401037
}

0 commit comments

Comments
 (0)