From c4f2898b68a7b5429fdfec71d5544afe8e0feb6e Mon Sep 17 00:00:00 2001 From: cjihrig Date: Wed, 18 Oct 2017 13:59:52 -0400 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/15380 PR-URL: https://github.com/nodejs/node/pull/16247 Reviewed-By: Ben Noordhuis Reviewed-By: James M Snell --- src/spawn_sync.cc | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/spawn_sync.cc b/src/spawn_sync.cc index 24922ac8daa28c..626ecbb04ff5b7 100644 --- a/src/spawn_sync.cc +++ b/src/spawn_sync.cc @@ -1016,25 +1016,22 @@ int SyncProcessRunner::CopyJsStringArray(Local js_value, Local context = env()->context(); js_array = js_value.As()->Clone().As(); 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*)); }