From 0ba47cb9bc54164565357a1e7655053770fd8cb0 Mon Sep 17 00:00:00 2001 From: liu-qiang12 Date: Thu, 11 Aug 2022 10:29:54 +0800 Subject: [PATCH] modify code Optimize clear, delete, has, get, interface performance in builtins_set and builtins_map Modify the ut test case of the map Signed-off-by: liu-qiang12 https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/I5LR7W --- .../builtins/tests/builtins_map_test.cpp | 11 +++++--- ecmascript/js_map.cpp | 7 +++--- ecmascript/js_set.cpp | 6 ++--- ecmascript/linked_hash_table.cpp | 25 +++++++++---------- ecmascript/linked_hash_table.h | 6 ++--- 5 files changed, 29 insertions(+), 26 deletions(-) diff --git a/ecmascript/builtins/tests/builtins_map_test.cpp b/ecmascript/builtins/tests/builtins_map_test.cpp index c23cf7b974..cf25a30796 100644 --- a/ecmascript/builtins/tests/builtins_map_test.cpp +++ b/ecmascript/builtins/tests/builtins_map_test.cpp @@ -262,20 +262,25 @@ HWTEST_F_L0(BuiltinsMapTest, DeleteAndRemove) EXPECT_EQ(result2.GetRawData(), JSTaggedValue::True().GetRawData()); // delete + [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1); JSTaggedValue result3 = BuiltinsMap::Delete(ecmaRuntimeCallInfo1); - + TestHelper::TearDownFrame(thread, prev1); EXPECT_EQ(result3.GetRawData(), JSTaggedValue::True().GetRawData()); // check deleteKey is deleted + [[maybe_unused]] auto prev2 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1); JSTaggedValue result4 = BuiltinsMap::Has(ecmaRuntimeCallInfo1); - + TestHelper::TearDownFrame(thread, prev2); EXPECT_EQ(result4.GetRawData(), JSTaggedValue::False().GetRawData()); + [[maybe_unused]] auto prev3 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1); JSTaggedValue result5 = BuiltinsMap::GetSize(ecmaRuntimeCallInfo1); - + TestHelper::TearDownFrame(thread, prev3); EXPECT_EQ(result5.GetRawData(), JSTaggedValue(39).GetRawData()); // clear + [[maybe_unused]] auto prev4 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1); JSTaggedValue result6 = BuiltinsMap::Clear(ecmaRuntimeCallInfo1); + TestHelper::TearDownFrame(thread, prev4); EXPECT_EQ(result6.GetRawData(), JSTaggedValue::VALUE_UNDEFINED); EXPECT_EQ(map->GetSize(), 0); } diff --git a/ecmascript/js_map.cpp b/ecmascript/js_map.cpp index 16ed9ecf6d..7ac825873c 100644 --- a/ecmascript/js_map.cpp +++ b/ecmascript/js_map.cpp @@ -40,16 +40,15 @@ bool JSMap::Delete(const JSThread *thread, const JSHandle &map, const JSH return false; } mapHandle->RemoveEntry(thread, entry); - - JSHandle newMap = LinkedHashMap::Shrink(thread, mapHandle); - map->SetLinkedMap(thread, newMap); return true; } void JSMap::Clear(const JSThread *thread, const JSHandle &map) { LinkedHashMap *linkedMap = LinkedHashMap::Cast(map->GetLinkedMap().GetTaggedObject()); - linkedMap->Clear(thread); + JSHandle mapHandle(thread, LinkedHashMap::Cast(map->GetLinkedMap().GetTaggedObject())); + JSHandle newMap = linkedMap->Clear(thread, mapHandle); + map->SetLinkedMap(thread, newMap); } bool JSMap::Has(JSTaggedValue key) const diff --git a/ecmascript/js_set.cpp b/ecmascript/js_set.cpp index 26de5f6d03..d26c765d81 100644 --- a/ecmascript/js_set.cpp +++ b/ecmascript/js_set.cpp @@ -40,15 +40,15 @@ bool JSSet::Delete(const JSThread *thread, const JSHandle &set, const JSH return false; } setHandle->RemoveEntry(thread, entry); - JSHandle newSet = LinkedHashSet::Shrink(thread, setHandle); - set->SetLinkedSet(thread, newSet); return true; } void JSSet::Clear(const JSThread *thread, const JSHandle &set) { LinkedHashSet *linkedSet = LinkedHashSet::Cast(set->GetLinkedSet().GetTaggedObject()); - linkedSet->Clear(thread); + JSHandle setHandle(thread, LinkedHashSet::Cast(set->GetLinkedSet().GetTaggedObject())); + JSHandle newSet = linkedSet->Clear(thread, setHandle); + set->SetLinkedSet(thread, newSet); } bool JSSet::Has(JSTaggedValue value) const diff --git a/ecmascript/linked_hash_table.cpp b/ecmascript/linked_hash_table.cpp index 3b2d5f2a47..1a5fe8b61c 100644 --- a/ecmascript/linked_hash_table.cpp +++ b/ecmascript/linked_hash_table.cpp @@ -168,15 +168,14 @@ bool LinkedHashMap::Has(JSTaggedValue key) const return entry != -1; } -void LinkedHashMap::Clear(const JSThread *thread) +JSHandle LinkedHashMap::Clear(const JSThread *thread, const JSHandle &table) { - int numberOfElements = NumberOfElements() + NumberOfDeletedElements(); - for (int entry = 0; entry < numberOfElements; entry++) { - SetKey(thread, entry, JSTaggedValue::Hole()); - SetValue(thread, entry, JSTaggedValue::Hole()); + JSHandle newMap = LinkedHashMap::Create(thread); + if (table->Capacity() > 0) { + table->SetNextTable(thread, newMap.GetTaggedValue()); + table->SetNumberOfDeletedElements(thread, -1); } - SetNumberOfElements(thread, 0); - SetNumberOfDeletedElements(thread, numberOfElements); + return newMap; } JSHandle LinkedHashMap::Shrink(const JSThread *thread, const JSHandle &table, @@ -215,14 +214,14 @@ bool LinkedHashSet::Has(JSTaggedValue key) const return entry != -1; } -void LinkedHashSet::Clear(const JSThread *thread) +JSHandle LinkedHashSet::Clear(const JSThread *thread, const JSHandle &table) { - int numberOfElements = NumberOfElements() + NumberOfDeletedElements(); - for (int entry = 0; entry < numberOfElements; entry++) { - SetKey(thread, entry, JSTaggedValue::Hole()); + JSHandle newSet = LinkedHashSet::Create(thread); + if (table->Capacity() > 0) { + table->SetNextTable(thread, newSet.GetTaggedValue()); + table->SetNumberOfDeletedElements(thread, -1); } - SetNumberOfElements(thread, 0); - SetNumberOfDeletedElements(thread, numberOfElements); + return newSet; } JSHandle LinkedHashSet::Shrink(const JSThread *thread, const JSHandle &table, diff --git a/ecmascript/linked_hash_table.h b/ecmascript/linked_hash_table.h index c90ef5f0d9..5b37b9bced 100644 --- a/ecmascript/linked_hash_table.h +++ b/ecmascript/linked_hash_table.h @@ -86,7 +86,7 @@ public: if (!IsKey(key)) { return -1; } - int hash = static_cast(LinkedHash::Hash(key)); + int hash = LinkedHash::Hash(key); uint32_t bucket = HashToBucket(hash); for (JSTaggedValue entry = GetElement(BucketToIndex(bucket)); !entry.IsHole(); entry = GetNextEntry(entry.GetInt())) { @@ -356,7 +356,7 @@ public: bool Has(JSTaggedValue key) const; - void Clear(const JSThread *thread); + static JSHandle Clear(const JSThread *thread, const JSHandle &table); DECL_DUMP() }; @@ -394,7 +394,7 @@ public: bool Has(JSTaggedValue key) const; - void Clear(const JSThread *thread); + static JSHandle Clear(const JSThread *thread, const JSHandle &table); DECL_DUMP() }; } // namespace panda::ecmascript