arkcompiler_ets_runtime/ecmascript/layout_info.cpp
zhangyukun8 eef37db2c3 Optimize for-in loop
1. Add EnumCache for simple properties and no-elements properties
2. Add fastpath for non-special objects
3. Avoid duplicated copy for slowpath
4. Transition hclass when detele prop in TS

Issue:https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/I84DMO?from=project-issue

Signed-off-by: zhangyukun8 <zhangyukun8@huawei.com>
Change-Id: I81648b44cc83a907c50ec90c9e8de3bbb104cef8
2023-10-12 10:00:43 +08:00

240 lines
8.4 KiB
C++

/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecmascript/layout_info-inl.h"
#include "ecmascript/ecma_string.h"
#include "ecmascript/filter_helper.h"
#include "ecmascript/js_object-inl.h"
#include "ecmascript/js_symbol.h"
#include "ecmascript/mem/assert_scope.h"
#include "pgo_profiler/pgo_profiler_layout.h"
namespace panda::ecmascript {
using PGOHandler = pgo::PGOHandler;
void LayoutInfo::Initialize(const JSThread *thread, int num)
{
SetExtraLength(num);
int propNum = GetPropertiesCapacity();
auto attr = PropertyAttributes();
for (int i = 0; i < propNum; i++) {
SetPropertyInit(thread, i, JSTaggedValue::Hole(), attr);
}
}
void LayoutInfo::AddKey(const JSThread *thread, [[maybe_unused]] int index, const JSTaggedValue &key,
const PropertyAttributes &attr)
{
DISALLOW_GARBAGE_COLLECTION;
int number = NumberOfElements();
ASSERT(attr.GetOffset() == static_cast<uint32_t>(number));
ASSERT(number + 1 <= GetPropertiesCapacity());
ASSERT(number == index);
SetNumberOfElements(thread, number + 1);
SetPropertyInit(thread, number, key, attr);
uint32_t keyHash = key.GetKeyHashCode();
int insertIndex = number;
for (; insertIndex > 0; --insertIndex) {
JSTaggedValue prevKey = GetSortedKey(insertIndex - 1);
if (prevKey.GetKeyHashCode() <= keyHash) {
break;
}
SetSortedIndex(thread, insertIndex, GetSortedIndex(insertIndex - 1));
}
SetSortedIndex(thread, insertIndex, number);
}
void LayoutInfo::GetAllKeys(const JSThread *thread, int end, int offset, TaggedArray *keyArray,
const JSHandle<JSObject> object)
{
ASSERT(end <= NumberOfElements());
ASSERT_PRINT(offset + end <= static_cast<int>(keyArray->GetLength()),
"keyArray capacity is not enough for dictionary");
DISALLOW_GARBAGE_COLLECTION;
int enumKeys = 0;
for (int i = 0; i < end; i++) {
JSTaggedValue key = GetKey(i);
if (key.IsString()) {
if (IsUninitializedProperty(*object, i)) {
continue;
}
keyArray->Set(thread, enumKeys + offset, key);
enumKeys++;
}
}
if (enumKeys < end) {
for (int i = 0; i < end; i++) {
JSTaggedValue key = GetKey(i);
if (key.IsSymbol()) {
keyArray->Set(thread, enumKeys + offset, key);
enumKeys++;
}
}
}
}
void LayoutInfo::GetAllKeysByFilter(const JSThread *thread, uint32_t numberOfProps, uint32_t &keyArrayEffectivelength,
TaggedArray *keyArray, const JSHandle<JSObject> object, uint32_t filter)
{
ASSERT(numberOfProps <= static_cast<uint32_t>(NumberOfElements()));
ASSERT_PRINT(keyArrayEffectivelength + numberOfProps <= keyArray->GetLength(),
"keyArray capacity is not enough for dictionary");
DISALLOW_GARBAGE_COLLECTION;
uint32_t enumKeys = 0;
for (uint32_t i = 0; i < numberOfProps; i++) {
JSTaggedValue key = GetKey(static_cast<int>(i));
if (key.IsString() && !(filter & NATIVE_KEY_SKIP_STRINGS)) {
if (IsUninitializedProperty(*object, i)) {
continue;
}
PropertyAttributes attr = GetAttr(static_cast<int>(i));
bool bIgnore = FilterHelper::IgnoreKeyByFilter<PropertyAttributes>(attr, filter);
if (bIgnore) {
continue;
}
keyArray->Set(thread, keyArrayEffectivelength, key);
keyArrayEffectivelength++;
enumKeys++;
}
}
if (enumKeys < numberOfProps) {
for (uint32_t i = 0; i < numberOfProps; i++) {
JSTaggedValue key = GetKey(static_cast<int>(i));
if (key.IsSymbol() && !(filter & NATIVE_KEY_SKIP_SYMBOLS)) {
PropertyAttributes attr = GetAttr(static_cast<int>(i));
bool bIgnore = FilterHelper::IgnoreKeyByFilter<PropertyAttributes>(attr, filter);
if (bIgnore) {
continue;
}
keyArray->Set(thread, keyArrayEffectivelength, key);
keyArrayEffectivelength++;
}
}
}
}
void LayoutInfo::GetAllKeysForSerialization(int end, std::vector<JSTaggedValue> &keyVector)
{
ASSERT(end <= NumberOfElements());
for (int i = 0; i < end; i++) {
JSTaggedValue key = GetKey(i);
if (key.IsString() || key.IsSymbol()) {
keyVector.emplace_back(key);
}
}
}
std::pair<uint32_t, uint32_t> LayoutInfo::GetNumOfEnumKeys(int end, const JSObject *object) const
{
ASSERT(end <= NumberOfElements());
uint32_t enumKeys = 0;
uint32_t shadowKeys = 0;
for (int i = 0; i < end; i++) {
JSTaggedValue key = GetKey(i);
if (!key.IsString()) {
continue;
}
if (IsUninitializedProperty(object, i)) {
continue;
}
if (GetAttr(i).IsEnumerable()) {
enumKeys++;
} else {
shadowKeys++;
}
}
return std::make_pair(enumKeys, shadowKeys);
}
void LayoutInfo::GetAllEnumKeys(JSThread *thread, int end, int offset, JSHandle<TaggedArray> keyArray,
uint32_t *keys, JSHandle<TaggedQueue> shadowQueue, const JSHandle<JSObject> object,
int32_t lastLength)
{
ASSERT(end <= NumberOfElements());
ASSERT_PRINT(offset <= static_cast<int>(keyArray->GetLength()),
"keyArray capacity is not enough for dictionary");
JSMutableHandle<JSTaggedValue> keyHandle(thread, JSTaggedValue::Undefined());
int enumKeys = 0;
for (int i = 0; i < end; i++) {
keyHandle.Update(GetKey(i));
if (!keyHandle->IsString()) {
continue;
}
if (IsUninitializedProperty(*object, i)) {
continue;
}
if (GetAttr(i).IsEnumerable()) {
bool isDuplicated = JSObject::IsDepulicateKeys(thread, keyArray, lastLength, shadowQueue, keyHandle);
if (isDuplicated) {
continue;
}
keyArray->Set(thread, enumKeys + offset, keyHandle);
enumKeys++;
} else {
TaggedQueue::PushFixedQueue(thread, shadowQueue, keyHandle);
}
}
*keys += enumKeys;
}
void LayoutInfo::GetAllEnumKeys(JSThread *thread, int end, int offset, JSHandle<TaggedArray> keyArray,
uint32_t *keys, const JSHandle<JSObject> object)
{
ASSERT(end <= NumberOfElements());
ASSERT_PRINT(offset <= static_cast<int>(keyArray->GetLength()),
"keyArray capacity is not enough for dictionary");
JSMutableHandle<JSTaggedValue> keyHandle(thread, JSTaggedValue::Undefined());
int enumKeys = 0;
for (int i = 0; i < end; i++) {
keyHandle.Update(GetKey(i));
if (keyHandle->IsString() && GetAttr(i).IsEnumerable()) {
if (IsUninitializedProperty(*object, i)) {
continue;
}
keyArray->Set(thread, enumKeys + offset, keyHandle);
enumKeys++;
}
}
*keys += enumKeys;
}
bool LayoutInfo::IsUninitializedProperty(const JSObject *object, uint32_t index) const
{
PropertyAttributes attr = GetAttr(index);
if (!attr.IsInlinedProps()) {
return false;
}
JSTaggedValue val = object->GetPropertyInlinedProps(attr.GetOffset());
return val.IsHole();
}
void LayoutInfo::DumpFieldIndexForProfile(int index, PGOHClassLayoutDesc &desc, PGOObjKind kind)
{
auto key = GetKey(index);
if (key.IsString()) {
auto attr = GetAttr(index);
TrackType type = attr.GetTrackType();
bool isAccessor = attr.IsAccessor();
auto keyString = EcmaStringAccessor(key).ToCString();
desc.UpdateKeyAndDesc(keyString, PGOHandler(type, isAccessor), kind);
}
}
} // namespace panda::ecmascript