Optimize ABC's format

Signed-off-by: hufeng <hufeng20@huawei.com>
Change-Id: I1e581c576c987b042c787c1cda614958b175d61a
This commit is contained in:
hufeng 2024-03-07 13:59:46 +00:00
parent c580a5491b
commit 4ea1428361
3 changed files with 40 additions and 27 deletions

View File

@ -421,9 +421,8 @@ void ItemContainer::DeduplicateItems(bool computeLayout)
uint32_t ItemContainer::ComputeLayout()
{
uint32_t num_classes = class_map_.size();
uint32_t num_literalarrays = literalarray_map_.size();
uint32_t class_idx_offset = sizeof(File::Header);
uint32_t cur_offset = class_idx_offset + (num_classes + num_literalarrays) * ID_SIZE;
uint32_t cur_offset = class_idx_offset + num_classes * ID_SIZE;
UpdateOrderIndexes();
@ -572,12 +571,13 @@ bool ItemContainer::WriteHeaderIndexInfo(Writer *writer)
return false;
}
if (!writer->Write<uint32_t>(literalarray_map_.size())) {
// reserve [num_literalarrays] field
if (!writer->Write<uint32_t>(INVALID_INDEX)) {
return false;
}
uint32_t literalarray_idx_offset = sizeof(File::Header) + class_map_.size() * ID_SIZE;
if (!writer->Write<uint32_t>(literalarray_idx_offset)) {
// reserve [literalarray_idx_off] field
if (!writer->Write<uint32_t>(INVALID_OFFSET)) {
return false;
}
@ -585,7 +585,7 @@ bool ItemContainer::WriteHeaderIndexInfo(Writer *writer)
return false;
}
size_t index_section_off = literalarray_idx_offset + literalarray_map_.size() * ID_SIZE;
size_t index_section_off = sizeof(File::Header) + class_map_.size() * ID_SIZE;
return writer->Write<uint32_t>(index_section_off);
}
@ -651,14 +651,6 @@ bool ItemContainer::Write(Writer *writer, bool deduplicateItems)
}
}
// Write literalArray idx
for (auto &entry : literalarray_map_) {
if (!writer->Write(entry.second->GetOffset())) {
return false;
}
}
// Write index section
if (!index_section_item_.Write(writer)) {
@ -821,13 +813,25 @@ bool ItemContainer::IndexHeaderItem::Write(Writer *writer)
}
for (auto *index_item : indexes_) {
if (!writer->Write<uint32_t>(index_item->GetNumItems())) {
return false;
}
auto itemType = index_item->GetItemType();
if (itemType == ItemTypes::FIELD_INDEX_ITEM || itemType == ItemTypes::PROTO_INDEX_ITEM) {
// reserve [field_idx_size] | [proto_idx_size] field
if (!writer->Write<uint32_t>(INVALID_INDEX)) {
return false;
}
// reserve [field_idx_off] | [proto_idx_off] field
if (!writer->Write<uint32_t>(INVALID_OFFSET)) {
return false;
}
} else {
if (!writer->Write<uint32_t>(index_item->GetNumItems())) {
return false;
}
ASSERT(index_item->GetOffset() != 0);
if (!writer->Write<uint32_t>(index_item->GetOffset())) {
return false;
ASSERT(index_item->GetOffset() != 0);
if (!writer->Write<uint32_t>(index_item->GetOffset())) {
return false;
}
}
}
@ -874,9 +878,11 @@ bool ItemContainer::IndexItem::Write(Writer *writer)
{
ASSERT(GetOffset() == writer->GetOffset());
for (auto *item : index_) {
if (!writer->Write<uint32_t>(item->GetOffset())) {
return false;
if (NeedsEmit()) {
for (auto *item : index_) {
if (!writer->Write<uint32_t>(item->GetOffset())) {
return false;
}
}
}

View File

@ -224,6 +224,9 @@ private:
IndexItem(IndexType type, size_t max_index) : type_(type), max_index_(max_index)
{
ASSERT(type_ != IndexType::NONE);
if (type == IndexType::FIELD || type == IndexType::PROTO) {
SetNeedsEmit(false);
}
}
~IndexItem() override = default;
@ -276,7 +279,10 @@ private:
protected:
size_t CalculateSize() const override
{
return index_.size() * ID_SIZE;
if (NeedsEmit()) {
return index_.size() * ID_SIZE;
}
return 0;
}
private:

View File

@ -501,6 +501,8 @@ ProtoItem::ProtoItem(TypeItem *ret_type, const std::vector<MethodParamItem> &par
for (auto &p : params) {
AddType(p.GetType(), &n);
}
// no need to emit protoItem
SetNeedsEmit(false);
}
void ProtoItem::AddType(TypeItem *type, size_t *n)
@ -569,9 +571,8 @@ bool BaseMethodItem::Write(Writer *writer)
return false;
}
ASSERT(proto_->HasIndex(this));
if (!writer->Write<uint16_t>(proto_->GetIndex(this))) {
// reserve [proto_idx] field, write invalid index
if (!writer->Write<uint16_t>(MAX_INDEX_16)) {
return false;
}