Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize memory usage with DBString and DBArray #379

Merged
merged 37 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
733c940
Remove unessessary template arguments
mateofio Aug 16, 2020
75fb520
ReaderUtil: support StringView
mateofio Aug 16, 2020
123281f
Add DBString class
mateofio Aug 16, 2020
9bbc020
DBString: Dedup strings
mateofio Aug 16, 2020
ad61e3e
Remove hash table and use reference counting
mateofio Aug 17, 2020
6ea21b7
DBString - remove ref counting
mateofio Aug 17, 2020
0c359aa
DBString - allow non-const operations
mateofio Aug 19, 2020
3b3ad3d
WriterLcf Decode - use StringView
mateofio Aug 16, 2020
18e106f
Actor: use DBString
mateofio Aug 16, 2020
8ef1e8b
Skill - use DBString
mateofio Aug 16, 2020
dc05998
Item: use DBString
mateofio Aug 16, 2020
5cf7622
Enemy: use DBString
mateofio Aug 16, 2020
cd74270
Use DBString for troop name
mateofio Aug 16, 2020
d413ee8
Terrain use DBString
mateofio Aug 16, 2020
0a371df
State - use DBString
mateofio Aug 16, 2020
b61b3b7
Terms: Use DBString
mateofio Aug 16, 2020
3e82204
Animation use DBString
mateofio Aug 16, 2020
3b2a30f
Attribute use DBString
mateofio Aug 16, 2020
d853b3f
Chipset use DBString
mateofio Aug 16, 2020
a369e95
Recode - use StringView
mateofio Aug 16, 2020
4e94a32
System - use StringView
mateofio Aug 16, 2020
368d814
Switch, Var, CommonEvent use DBString
mateofio Aug 16, 2020
c899370
Class, BattleCommand, BattlerAnim - Use DBString
mateofio Aug 16, 2020
9c6c007
Chipset name use DBString
mateofio Aug 16, 2020
f32ab51
MapInfo - use DBString
mateofio Aug 16, 2020
a69c16f
EventCommand use DBString
mateofio Aug 16, 2020
a245097
MapInfo - use DBString for background name
mateofio Aug 17, 2020
9956e3c
Map structures - use DBString
mateofio Aug 17, 2020
8a22a63
MoveCommand - use DBString
mateofio Aug 17, 2020
d84ffb7
ReaderLcf: optimize DBString reading
mateofio Aug 19, 2020
d8d9698
Remove some includes
mateofio Aug 19, 2020
e20c0e6
Add DBArray
mateofio Aug 18, 2020
28273cd
Add DBArray support to generate script
mateofio Aug 19, 2020
21dbcc4
EventCommand: use DBArray
mateofio Aug 19, 2020
6139569
Add support for DBArray to parsing framework
mateofio Aug 19, 2020
9fa824b
SizeField - support multiple array types
mateofio Aug 19, 2020
930d21a
Add Void Reader to reader_struct
mateofio Aug 28, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ add_library(lcf)

set(LCF_SOURCES
src/data.cpp
src/dbarray.cpp
src/encoder.cpp
src/ini.cpp
src/inireader.cpp
Expand Down Expand Up @@ -194,6 +195,9 @@ set(LCF_SOURCES

set(LCF_HEADERS
src/lcf/data.h
src/lcf/dbarray.h
src/lcf/dbarrayalloc.h
src/lcf/dbstring.h
src/lcf/encoder.h
src/lcf/enum_tags.h
src/lcf/flag_set.h
Expand Down
4 changes: 4 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ liblcf_la_LDFLAGS = \
-no-undefined
liblcf_la_SOURCES = \
src/data.cpp \
src/dbarray.cpp \
src/encoder.cpp \
src/ini.cpp \
src/inireader.cpp \
Expand Down Expand Up @@ -201,6 +202,9 @@ liblcf_la_SOURCES = \

lcfinclude_HEADERS = \
src/lcf/data.h \
src/lcf/dbarray.h \
src/lcf/dbarrayalloc.h \
src/lcf/dbstring.h \
src/lcf/encoder.h \
src/lcf/enum_tags.h \
src/lcf/flag_set.h \
Expand Down
416 changes: 208 additions & 208 deletions generator/csv/fields.csv

Large diffs are not rendered by default.

15 changes: 13 additions & 2 deletions generator/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
'Int16': 'int16_t',
'Int32': 'int32_t',
'String': 'std::string',
'DBString': 'DBString',
}

# Additional Jinja 2 functions
Expand Down Expand Up @@ -70,6 +71,9 @@ def cpp_type(ty, prefix=True):
m = re.match(r'(Vector|Array)<(.*)>', ty)
if m:
return 'std::vector<%s>' % cpp_type(m.group(2), prefix)
m = re.match(r'DBArray<(.*)>', ty)
if m:
return 'DBArray<%s>' % cpp_type(m.group(1), prefix)

m = re.match(r'Ref<(.*):(.*)>', ty)
if m:
Expand Down Expand Up @@ -100,7 +104,7 @@ def pod_default(field):
ftype = field.type

# Not a POD, no default
if dfl == '' or dfl == '\'\'' or ftype.startswith('Vector') or ftype.startswith('Array'):
if dfl == '' or dfl == '\'\'' or ftype.startswith('Vector') or ftype.startswith('Array') or ftype.startswith('DBArray'):
return ""

if ftype == 'Boolean':
Expand Down Expand Up @@ -164,6 +168,9 @@ def struct_headers(ty, header_map):
if ty == 'String':
return ['<string>']

if ty == 'DBString':
return ['"lcf/dbstring.h"']

if ty in int_types or ty == "DatabaseVersion":
return ['<stdint.h>']

Expand Down Expand Up @@ -191,6 +198,10 @@ def struct_headers(ty, header_map):
if m:
return ['<vector>'] + struct_headers(m.group(2), header_map)

m = re.match(r'DBArray<(.*)>', ty)
if m:
return ['<lcf/dbarray.h>'] + struct_headers(m.group(1), header_map)

header = header_map.get(ty)
if header is not None:
return ['"lcf/rpg/%s.h"' % header]
Expand Down Expand Up @@ -336,7 +347,7 @@ def needs_ctor(struct_name):
for method, hdrs in setup[struct_name])

def type_is_array(ty):
return re.match(r'(Vector|Array)<(.*)>', ty)
return re.match(r'(Vector|Array|DBArray)<(.*)>', ty)

def is_monotonic_from_0(enum):
expected = 0
Expand Down
91 changes: 91 additions & 0 deletions src/dbarray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include "lcf/dbarrayalloc.h"
#include "lcf/dbarray.h"
#include "lcf/dbstring.h"
#include <cassert>
#include <cstddef>

//#define LCF_DEBUG_DBARRAY

#ifdef LCF_DEBUG_DBARRAY
#include <iostream>
#endif

namespace lcf {

constexpr DBArrayAlloc::size_type DBArrayAlloc::_empty_buf;
constexpr DBString::size_type DBString::npos;

static ptrdiff_t HeaderSize(size_t align) {
return std::max(sizeof(DBArrayAlloc::size_type), align);
}

static size_t AllocSize(size_t size, size_t align) {
return HeaderSize(align) + size;
}

static void* Adjust(void* p, ptrdiff_t off) {
return reinterpret_cast<void*>(reinterpret_cast<intptr_t>(p) + off);
}

void* DBArrayAlloc::alloc(size_type size, size_type field_size, size_type align) {
if (field_size == 0) {
return empty_buf();
}
assert(align <= alignof(std::max_align_t));
auto* raw = ::operator new(AllocSize(size, align));
auto* p = Adjust(raw, HeaderSize(align));
*get_size_ptr(p) = field_size;
#ifdef LCF_DEBUG_DBARRAY
std::cout << "DBArray: Allocated"
<< " size=" << size
<< " field_size=" << *get_size_ptr(p)
<< " align=" << align
<< " ptr=" << raw
<< " adjusted=" << p
<< std::endl;
#endif
return p;
}

void DBArrayAlloc::free(void* p, size_type align) noexcept {
assert(p != nullptr);
if (p != empty_buf()) {
auto* raw = Adjust(p, -HeaderSize(align));
#ifdef LCF_DEBUG_DBARRAY
std::cout << "DBArray: Free"
<< " align=" << align
<< " ptr=" << raw
<< " adjusted=" << p
<< " field_size=" << *get_size_ptr(p)
<< std::endl;
#endif
::operator delete(raw);
}
}

char* DBString::construct_z(const char* s, size_t len) {
auto* p = alloc(len);
if (len) {
std::memcpy(p, s, len + 1);
}
return p;
}

char* DBString::construct_sv(const char* s, size_t len) {
auto* p = alloc(len);
if (len) {
std::memcpy(p, s, len);
p[len] = '\0';
}
return p;
}

DBString& DBString::operator=(const DBString& o) {
if (this != &o) {
destroy();
_storage = construct_z(o.data(), o.size());
}
return *this;
}

} // namespace lcf
12 changes: 6 additions & 6 deletions src/generated/lcf/rpg/actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

// Headers
#include <stdint.h>
#include <string>
#include <vector>
#include "lcf/dbstring.h"
#include "lcf/rpg/equipment.h"
#include "lcf/rpg/learning.h"
#include "lcf/rpg/parameters.h"
Expand All @@ -31,16 +31,16 @@ namespace rpg {
public:
void Setup();
int ID = 0;
std::string name;
std::string title;
std::string character_name;
DBString name;
DBString title;
DBString character_name;
int32_t character_index = 0;
bool transparent = false;
int32_t initial_level = 1;
int32_t final_level = -1;
bool critical_hit = true;
int32_t critical_hit_chance = 30;
std::string face_name;
DBString face_name;
int32_t face_index = 0;
bool two_weapon = false;
bool lock_equipment = false;
Expand All @@ -58,7 +58,7 @@ namespace rpg {
int32_t battler_animation = 1;
std::vector<Learning> skills;
bool rename_skill = false;
std::string skill_name;
DBString skill_name;
std::vector<uint8_t> state_ranks;
std::vector<uint8_t> attribute_ranks;
std::vector<int32_t> battle_commands;
Expand Down
6 changes: 3 additions & 3 deletions src/generated/lcf/rpg/animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#define LCF_RPG_ANIMATION_H

// Headers
#include <string>
#include <vector>
#include "lcf/dbstring.h"
#include "lcf/enum_tags.h"
#include "lcf/rpg/animationframe.h"
#include "lcf/rpg/animationtiming.h"
Expand Down Expand Up @@ -48,8 +48,8 @@ namespace rpg {
);

int ID = 0;
std::string name;
std::string animation_name;
DBString name;
DBString animation_name;
bool large = false;
std::vector<AnimationTiming> timings;
int32_t scope = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/generated/lcf/rpg/attribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

// Headers
#include <stdint.h>
#include <string>
#include "lcf/dbstring.h"
#include "lcf/enum_tags.h"
#include <ostream>
#include <type_traits>
Expand All @@ -36,7 +36,7 @@ namespace rpg {
);

int ID = 0;
std::string name;
DBString name;
int32_t type = 0;
int32_t a_rate = 300;
int32_t b_rate = 200;
Expand Down
4 changes: 2 additions & 2 deletions src/generated/lcf/rpg/battlecommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#define LCF_RPG_BATTLECOMMAND_H

// Headers
#include <string>
#include "lcf/dbstring.h"
#include "lcf/enum_tags.h"
#include <ostream>
#include <type_traits>
Expand Down Expand Up @@ -45,7 +45,7 @@ namespace rpg {
);

int ID = 0;
std::string name;
DBString name;
int32_t type = 0;
};
inline std::ostream& operator<<(std::ostream& os, BattleCommand::Type code) {
Expand Down
4 changes: 2 additions & 2 deletions src/generated/lcf/rpg/battleranimation.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#define LCF_RPG_BATTLERANIMATION_H

// Headers
#include <string>
#include <vector>
#include "lcf/dbstring.h"
#include "lcf/enum_tags.h"
#include "lcf/rpg/battleranimationextension.h"
#include <ostream>
Expand All @@ -34,7 +34,7 @@ namespace rpg {
};

int ID = 0;
std::string name;
DBString name;
int32_t speed = 0;
std::vector<BattlerAnimationExtension> base_data;
std::vector<BattlerAnimationExtension> weapon_data;
Expand Down
6 changes: 3 additions & 3 deletions src/generated/lcf/rpg/battleranimationextension.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

// Headers
#include <stdint.h>
#include <string>
#include "lcf/dbstring.h"
#include "lcf/enum_tags.h"
#include <ostream>
#include <type_traits>
Expand All @@ -36,8 +36,8 @@ namespace rpg {
);

int ID = 0;
std::string name;
std::string battler_name;
DBString name;
DBString battler_name;
int32_t battler_index = 0;
int32_t animation_type = 0;
int32_t animation_id = 1;
Expand Down
6 changes: 3 additions & 3 deletions src/generated/lcf/rpg/chipset.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

// Headers
#include <stdint.h>
#include <string>
#include <vector>
#include "lcf/dbstring.h"
#include "lcf/enum_tags.h"
#include <ostream>
#include <type_traits>
Expand All @@ -39,8 +39,8 @@ namespace rpg {
Chipset();
void Init();
int ID = 0;
std::string name;
std::string chipset_name;
DBString name;
DBString chipset_name;
std::vector<int16_t> terrain_data;
std::vector<uint8_t> passable_data_lower;
std::vector<uint8_t> passable_data_upper;
Expand Down
4 changes: 2 additions & 2 deletions src/generated/lcf/rpg/class.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

// Headers
#include <stdint.h>
#include <string>
#include <vector>
#include "lcf/dbstring.h"
#include "lcf/rpg/learning.h"
#include "lcf/rpg/parameters.h"
#include <ostream>
Expand All @@ -29,7 +29,7 @@ namespace rpg {
class Class {
public:
int ID = 0;
std::string name;
DBString name;
bool two_weapon = false;
bool lock_equipment = false;
bool auto_battle = false;
Expand Down
4 changes: 2 additions & 2 deletions src/generated/lcf/rpg/commonevent.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#define LCF_RPG_COMMONEVENT_H

// Headers
#include <string>
#include <vector>
#include "lcf/dbstring.h"
#include "lcf/enum_tags.h"
#include "lcf/rpg/eventcommand.h"
#include <ostream>
Expand All @@ -34,7 +34,7 @@ namespace rpg {
};

int ID = 0;
std::string name;
DBString name;
int32_t trigger = 0;
bool switch_flag = false;
int32_t switch_id = 1;
Expand Down
Loading