Refactor save size calculation with SaveSizeCounter

SaveSizeCounter is introduced and used for calculating the save size of objects. The previous method of calculating save size directly in each class was replaced with a call to the SaveSizeCounter's calc method. This results in a cleaner, more maintainable code as the size calculation logic is now centralized in one class. It is more efficient and scalable as any changes to the calculation can be made in one place and will be reflected everywhere.
This commit is contained in:
IlushaShurupov 2023-08-01 07:41:09 +03:00 committed by Ilya Shurupov
parent a679ab1c4b
commit e0eb6e138d
6 changed files with 30 additions and 15 deletions

View file

@ -221,7 +221,7 @@ namespace obj {
ndf << tp::String(in->type->name);
// allocate for object file header
ndf.setFreeAddress(ndf.getFreeAddress() + sizeof(ObjectFileHead) + tp::String::Logic::calcLength(in->type->name) + sizeof(tp::String::Logic::Index));
ndf.setFreeAddress(ndf.getFreeAddress() + sizeof(ObjectFileHead) + tp::SaveSizeCounter::calc(tp::String(in->type->name)));
// calc max size needed for saving all hierarchy of types
tp::alni file_alloc_size = objsize_file_util(in, in->type);

View file

@ -91,8 +91,7 @@ tp::alni ScriptSection::save_script_table_to_file_size(ScriptSection* self, Arch
}
// instructions
ASSERT(false)
// size += iter->mBytecode.mInstructions.saveSize();
size += tp::SaveSizeCounter::calc(iter->mBytecode.mInstructions);
}
size += sizeof(tp::alni); // objects mem offset
@ -125,7 +124,6 @@ void ScriptSection::save_script_table_to_file(ScriptSection* self, ArchiverOut&
}
// mInstructions
ASSERT(false)
file << iter->mBytecode.mInstructions;
}
@ -163,8 +161,7 @@ void load_constants(ScriptSection* self, ArchiverIn& file, tp::alni start_addr)
}
// instructions
ASSERT(false)
//file.setAddress(file.getAddress() + script->mBytecode.mInstructions.saveSize());
file.setAddress(file.getAddress() + tp::SaveSizeCounter::calc(script->mBytecode.mInstructions));
}
file.setAddress(addres);

View file

@ -44,9 +44,7 @@ alni DictObject::save_size(DictObject* self) {
for (auto item : self->items) {
// string length
auto size = item->key.size();
save_size += size * sizeof(*item->key.read());
save_size += sizeof(decltype(size));
save_size += tp::SaveSizeCounter::calc(item->key);
// object file adress
save_size += sizeof(alni);

View file

@ -50,11 +50,7 @@ alnf StringObject::to_float(StringObject* self) {
}
static alni save_size(StringObject* self) {
alni save_size = 0;
auto size = self->val.size();
save_size += (size) * sizeof(*self->val.read());
save_size += sizeof(decltype(size));
return save_size;
return tp::SaveSizeCounter::calc(self->val);
}
static void save(StringObject* self, ArchiverOut& file_self) {