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:
parent
a679ab1c4b
commit
e0eb6e138d
6 changed files with 30 additions and 15 deletions
|
|
@ -1,9 +1,32 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Common.hpp"
|
#include "Archiver.hpp"
|
||||||
|
|
||||||
namespace tp {
|
namespace tp {
|
||||||
|
|
||||||
|
class SaveSizeCounter : public ArchiverTemplate<false> {
|
||||||
|
public:
|
||||||
|
SaveSizeCounter() = default;
|
||||||
|
|
||||||
|
template < typename tType>
|
||||||
|
static ualni calc(const tType& val) {
|
||||||
|
SaveSizeCounter cnt;
|
||||||
|
cnt << val;
|
||||||
|
return cnt.mSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void writeBytes(const int1* val, ualni size) override { mSize += size; }
|
||||||
|
void readBytes(int1* val, ualni size) override {}
|
||||||
|
|
||||||
|
[[nodiscard]] ualni getSize() const { return mSize; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
ualni mSize = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
template <bool tRecursive>
|
template <bool tRecursive>
|
||||||
class SizeCounter {
|
class SizeCounter {
|
||||||
|
|
|
||||||
|
|
@ -221,7 +221,7 @@ namespace obj {
|
||||||
ndf << tp::String(in->type->name);
|
ndf << tp::String(in->type->name);
|
||||||
|
|
||||||
// allocate for object file header
|
// 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
|
// calc max size needed for saving all hierarchy of types
|
||||||
tp::alni file_alloc_size = objsize_file_util(in, in->type);
|
tp::alni file_alloc_size = objsize_file_util(in, in->type);
|
||||||
|
|
|
||||||
|
|
@ -91,8 +91,7 @@ tp::alni ScriptSection::save_script_table_to_file_size(ScriptSection* self, Arch
|
||||||
}
|
}
|
||||||
|
|
||||||
// instructions
|
// instructions
|
||||||
ASSERT(false)
|
size += tp::SaveSizeCounter::calc(iter->mBytecode.mInstructions);
|
||||||
// size += iter->mBytecode.mInstructions.saveSize();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size += sizeof(tp::alni); // objects mem offset
|
size += sizeof(tp::alni); // objects mem offset
|
||||||
|
|
@ -125,7 +124,6 @@ void ScriptSection::save_script_table_to_file(ScriptSection* self, ArchiverOut&
|
||||||
}
|
}
|
||||||
|
|
||||||
// mInstructions
|
// mInstructions
|
||||||
ASSERT(false)
|
|
||||||
file << iter->mBytecode.mInstructions;
|
file << iter->mBytecode.mInstructions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -163,8 +161,7 @@ void load_constants(ScriptSection* self, ArchiverIn& file, tp::alni start_addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// instructions
|
// instructions
|
||||||
ASSERT(false)
|
file.setAddress(file.getAddress() + tp::SaveSizeCounter::calc(script->mBytecode.mInstructions));
|
||||||
//file.setAddress(file.getAddress() + script->mBytecode.mInstructions.saveSize());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
file.setAddress(addres);
|
file.setAddress(addres);
|
||||||
|
|
|
||||||
|
|
@ -44,9 +44,7 @@ alni DictObject::save_size(DictObject* self) {
|
||||||
|
|
||||||
for (auto item : self->items) {
|
for (auto item : self->items) {
|
||||||
// string length
|
// string length
|
||||||
auto size = item->key.size();
|
save_size += tp::SaveSizeCounter::calc(item->key);
|
||||||
save_size += size * sizeof(*item->key.read());
|
|
||||||
save_size += sizeof(decltype(size));
|
|
||||||
|
|
||||||
// object file adress
|
// object file adress
|
||||||
save_size += sizeof(alni);
|
save_size += sizeof(alni);
|
||||||
|
|
|
||||||
|
|
@ -50,11 +50,7 @@ alnf StringObject::to_float(StringObject* self) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static alni save_size(StringObject* self) {
|
static alni save_size(StringObject* self) {
|
||||||
alni save_size = 0;
|
return tp::SaveSizeCounter::calc(self->val);
|
||||||
auto size = self->val.size();
|
|
||||||
save_size += (size) * sizeof(*self->val.read());
|
|
||||||
save_size += sizeof(decltype(size));
|
|
||||||
return save_size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void save(StringObject* self, ArchiverOut& file_self) {
|
static void save(StringObject* self, ArchiverOut& file_self) {
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@
|
||||||
#include "core/typemethods.h"
|
#include "core/typemethods.h"
|
||||||
|
|
||||||
#include "Archiver.hpp"
|
#include "Archiver.hpp"
|
||||||
|
#include "SizeCounter.hpp"
|
||||||
|
|
||||||
//#include "interpreter/interpreter.h"
|
//#include "interpreter/interpreter.h"
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue