From 4cc6629747b92ee7d78d4e1d79c624b93debbae1 Mon Sep 17 00:00:00 2001 From: lukai Date: Sat, 14 Sep 2024 12:13:53 +0800 Subject: [PATCH] SendableArray.create extends multi-times which wastes memory Allocate target memory directly avoiding extending array multi-times. Issue:https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/IARCZJ Signed-off-by: lukai Change-Id: Iae91068e5805973c797fdfbddadb9cf6772706e6 --- ecmascript/builtins/builtins_shared_array.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/ecmascript/builtins/builtins_shared_array.cpp b/ecmascript/builtins/builtins_shared_array.cpp index e0e6ffc129..6bec454529 100644 --- a/ecmascript/builtins/builtins_shared_array.cpp +++ b/ecmascript/builtins/builtins_shared_array.cpp @@ -353,13 +353,14 @@ JSTaggedValue BuiltinsSharedArray::Create(EcmaRuntimeCallInfo *argv) THROW_TYPE_ERROR_AND_RETURN(thread, "Failed to construct the array.", JSTaggedValue::Exception()); } JSHandle newArrayHandle(thread, newArray); - JSMutableHandle key(thread, JSTaggedValue::Undefined()); + ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); + auto elements = factory->NewSOldSpaceTaggedArray(arrayLength, JSTaggedValue::Hole()); for (uint32_t k = 0; k < arrayLength; k++) { - key.Update(JSTaggedValue(k)); - JSObject::CreateDataPropertyOrThrow(thread, newArrayHandle, key, initValue, SCheckMode::SKIP); + elements->Set(thread, k, initValue); } - key.Update(JSTaggedValue(arrayLength)); - JSSharedArray::LengthSetter(thread, newArrayHandle, key, true); + newArrayHandle->SetElements(thread, elements); + auto len = JSHandle(thread, JSTaggedValue(arrayLength)); + JSSharedArray::LengthSetter(thread, newArrayHandle, len, true); newArrayHandle->GetJSHClass()->SetExtensible(false); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); // Return A.