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 <liuqiang187@huawei.com>
https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/I5LR7W
This commit is contained in:
liu-qiang12 2022-08-11 10:29:54 +08:00
parent b027def273
commit 0ba47cb9bc
5 changed files with 29 additions and 26 deletions

View File

@ -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);
}

View File

@ -40,16 +40,15 @@ bool JSMap::Delete(const JSThread *thread, const JSHandle<JSMap> &map, const JSH
return false;
}
mapHandle->RemoveEntry(thread, entry);
JSHandle<LinkedHashMap> newMap = LinkedHashMap::Shrink(thread, mapHandle);
map->SetLinkedMap(thread, newMap);
return true;
}
void JSMap::Clear(const JSThread *thread, const JSHandle<JSMap> &map)
{
LinkedHashMap *linkedMap = LinkedHashMap::Cast(map->GetLinkedMap().GetTaggedObject());
linkedMap->Clear(thread);
JSHandle<LinkedHashMap> mapHandle(thread, LinkedHashMap::Cast(map->GetLinkedMap().GetTaggedObject()));
JSHandle<LinkedHashMap> newMap = linkedMap->Clear(thread, mapHandle);
map->SetLinkedMap(thread, newMap);
}
bool JSMap::Has(JSTaggedValue key) const

View File

@ -40,15 +40,15 @@ bool JSSet::Delete(const JSThread *thread, const JSHandle<JSSet> &set, const JSH
return false;
}
setHandle->RemoveEntry(thread, entry);
JSHandle<LinkedHashSet> newSet = LinkedHashSet::Shrink(thread, setHandle);
set->SetLinkedSet(thread, newSet);
return true;
}
void JSSet::Clear(const JSThread *thread, const JSHandle<JSSet> &set)
{
LinkedHashSet *linkedSet = LinkedHashSet::Cast(set->GetLinkedSet().GetTaggedObject());
linkedSet->Clear(thread);
JSHandle<LinkedHashSet> setHandle(thread, LinkedHashSet::Cast(set->GetLinkedSet().GetTaggedObject()));
JSHandle<LinkedHashSet> newSet = linkedSet->Clear(thread, setHandle);
set->SetLinkedSet(thread, newSet);
}
bool JSSet::Has(JSTaggedValue value) const

View File

@ -168,15 +168,14 @@ bool LinkedHashMap::Has(JSTaggedValue key) const
return entry != -1;
}
void LinkedHashMap::Clear(const JSThread *thread)
JSHandle<LinkedHashMap> LinkedHashMap::Clear(const JSThread *thread, const JSHandle<LinkedHashMap> &table)
{
int numberOfElements = NumberOfElements() + NumberOfDeletedElements();
for (int entry = 0; entry < numberOfElements; entry++) {
SetKey(thread, entry, JSTaggedValue::Hole());
SetValue(thread, entry, JSTaggedValue::Hole());
JSHandle<LinkedHashMap> 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> LinkedHashMap::Shrink(const JSThread *thread, const JSHandle<LinkedHashMap> &table,
@ -215,14 +214,14 @@ bool LinkedHashSet::Has(JSTaggedValue key) const
return entry != -1;
}
void LinkedHashSet::Clear(const JSThread *thread)
JSHandle<LinkedHashSet> LinkedHashSet::Clear(const JSThread *thread, const JSHandle<LinkedHashSet> &table)
{
int numberOfElements = NumberOfElements() + NumberOfDeletedElements();
for (int entry = 0; entry < numberOfElements; entry++) {
SetKey(thread, entry, JSTaggedValue::Hole());
JSHandle<LinkedHashSet> 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> LinkedHashSet::Shrink(const JSThread *thread, const JSHandle<LinkedHashSet> &table,

View File

@ -86,7 +86,7 @@ public:
if (!IsKey(key)) {
return -1;
}
int hash = static_cast<int>(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<LinkedHashMap> Clear(const JSThread *thread, const JSHandle<LinkedHashMap> &table);
DECL_DUMP()
};
@ -394,7 +394,7 @@ public:
bool Has(JSTaggedValue key) const;
void Clear(const JSThread *thread);
static JSHandle<LinkedHashSet> Clear(const JSThread *thread, const JSHandle<LinkedHashSet> &table);
DECL_DUMP()
};
} // namespace panda::ecmascript