From d40aab2b400e9396fe2f973f35380358b0649c0d Mon Sep 17 00:00:00 2001 From: ds Date: Sun, 8 Nov 2020 11:41:41 +0300 Subject: [PATCH] crlib: reduce hashmap memory usage. build: fixed clang build. --- ext/crlib/cr-hashmap.h | 17 ++++++++++++----- inc/config.h | 2 +- meson.build | 2 +- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/ext/crlib/cr-hashmap.h b/ext/crlib/cr-hashmap.h index e50721a..9622aca 100644 --- a/ext/crlib/cr-hashmap.h +++ b/ext/crlib/cr-hashmap.h @@ -103,8 +103,15 @@ private: return hash_ (key) % length; } - void rehash () { - auto capacity = (capacity_ << 1); + void rehash (size_t targetCapacity) { + if (length_ + targetCapacity < capacity_) { + return; + } + auto capacity = capacity_ ? capacity_ : 3; + + while (length_ + targetCapacity > capacity) { + capacity *= 2; + } auto contents = cr::makeUnique (capacity); for (size_t i = 0; i < capacity_; ++i) { @@ -188,7 +195,7 @@ public: for (size_t i = 0; i < capacity_; ++i) { contents_[i].used = false; } - rehash (); + rehash (0); } void foreach (Lambda callback) { @@ -201,8 +208,8 @@ public: public: V &operator [] (const K &key) { - if ((length_ << 1) > capacity_) { - rehash (); + if (length_ >= capacity_) { + rehash (length_ << 1); } auto result = put (key, contents_, capacity_); diff --git a/inc/config.h b/inc/config.h index bcad93d..38914fc 100644 --- a/inc/config.h +++ b/inc/config.h @@ -49,7 +49,7 @@ private: StringArray m_logos; StringArray m_avatars; - HashMap > m_language { 256 }; + HashMap > m_language; HashMap m_difficulty; // default tables for personality weapon preferences, overridden by weapon.cfg diff --git a/meson.build b/meson.build index a794d4e..ea36543 100644 --- a/meson.build +++ b/meson.build @@ -95,11 +95,11 @@ if isCLang or isGCC or (isIntel and not isWindows) if isGCC flagsCompiler += '-fgraphite-identity' + flagsLinker += '-flto-partition=none' endif flagsLinker += [ '-flto', - '-flto-partition=none', '-s', '-Wl,--version-script=../version_script.lds', '-Wl,--gc-sections'