Lower memory usage of Behavior Parameter Table by 10MB (#627)

* Added caching for table

Added caching for table

Add more caching

Update MasterServer.cpp

grds

Update CDBehaviorParameterTable.cpp

Update CDBehaviorParameterTable.h

Update CDBehaviorTemplateTable.cpp

Update Behavior.cpp

Update Behavior.cpp

change to map

Remove redundant query

* Remove include

* change to enum

* Update Behavior.cpp

* Use already cached table

* Update Behavior.cpp

* Reduce memory usage

Reduces the memory usage for the BehaviorParameter table by 10MB in memory.

* Update CDBehaviorTemplateTable.cpp
This commit is contained in:
David Markowitz 2022-07-11 20:43:09 -07:00 committed by GitHub
parent 06217ad5e3
commit b7497a47e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 15 deletions

View File

@ -9,14 +9,20 @@ CDBehaviorParameterTable::CDBehaviorParameterTable(void) {
hash = 0;
CDBehaviorParameter entry;
entry.behaviorID = tableData.getIntField(0, -1);
entry.parameterID = tableData.getStringField(1, "");
auto candidateStringToAdd = std::string(tableData.getStringField(1, ""));
auto parameter = m_ParametersList.find(candidateStringToAdd);
if (parameter != m_ParametersList.end()) {
entry.parameterID = parameter;
} else {
entry.parameterID = m_ParametersList.insert(candidateStringToAdd).first;
}
entry.value = tableData.getFloatField(2, -1.0f);
GeneralUtils::hash_combine(hash, entry.behaviorID);
GeneralUtils::hash_combine(hash, entry.parameterID);
GeneralUtils::hash_combine(hash, *entry.parameterID);
auto it = m_Entries.find(entry.behaviorID);
m_ParametersList.insert(entry.parameterID);
m_ParametersList.insert(*entry.parameterID);
m_Entries.insert(std::make_pair(hash, entry));
tableData.nextRow();
@ -36,7 +42,7 @@ CDBehaviorParameter CDBehaviorParameterTable::GetEntry(const uint32_t behaviorID
{
CDBehaviorParameter returnValue;
returnValue.behaviorID = 0;
returnValue.parameterID = "";
returnValue.parameterID = m_ParametersList.end();
returnValue.value = defaultValue;
size_t hash = 0;
@ -57,7 +63,7 @@ std::map<std::string, float> CDBehaviorParameterTable::GetParametersByBehaviorID
GeneralUtils::hash_combine(hash, parameterCandidate);
auto infoCandidate = m_Entries.find(hash);
if (infoCandidate != m_Entries.end()) {
returnInfo.insert(std::make_pair(infoCandidate->second.parameterID, infoCandidate->second.value));
returnInfo.insert(std::make_pair(*(infoCandidate->second.parameterID), infoCandidate->second.value));
}
}
return returnInfo;

View File

@ -12,9 +12,9 @@
//! BehaviorParameter Entry Struct
struct CDBehaviorParameter {
unsigned int behaviorID; //!< The Behavior ID
std::string parameterID; //!< The Parameter ID
float value; //!< The value of the behavior template
unsigned int behaviorID; //!< The Behavior ID
std::unordered_set<std::string>::iterator parameterID; //!< The Parameter ID
float value; //!< The value of the behavior template
};
//! BehaviorParameter table

View File

@ -24,7 +24,13 @@ CDBehaviorTemplateTable::CDBehaviorTemplateTable(void) {
entry.behaviorID = tableData.getIntField(0, -1);
entry.templateID = tableData.getIntField(1, -1);
entry.effectID = tableData.getIntField(2, -1);
entry.effectHandle = tableData.getStringField(3, "");
auto candidateToAdd = tableData.getStringField(3, "");
auto parameter = m_EffectHandles.find(candidateToAdd);
if (parameter != m_EffectHandles.end()) {
entry.effectHandle = parameter;
} else {
entry.effectHandle = m_EffectHandles.insert(candidateToAdd).first;
}
this->entries.push_back(entry);
this->entriesMappedByBehaviorID.insert(std::make_pair(entry.behaviorID, entry));
@ -62,7 +68,8 @@ const CDBehaviorTemplate CDBehaviorTemplateTable::GetByBehaviorID(uint32_t behav
if (entry == this->entriesMappedByBehaviorID.end()) {
CDBehaviorTemplate entryToReturn;
entryToReturn.behaviorID = 0;
entryToReturn.effectHandle = "";
entryToReturn.effectHandle = m_EffectHandles.end();
entryToReturn.effectID = 0;
return entryToReturn;
} else {
return entry->second;

View File

@ -3,6 +3,7 @@
// Custom Classes
#include "CDTable.h"
#include <unordered_map>
#include <unordered_set>
/*!
\file CDBehaviorTemplateTable.hpp
@ -11,10 +12,10 @@
//! BehaviorTemplate Entry Struct
struct CDBehaviorTemplate {
unsigned int behaviorID; //!< The Behavior ID
unsigned int templateID; //!< The Template ID (LOT)
unsigned int effectID; //!< The Effect ID attached
std::string effectHandle; //!< The effect handle
unsigned int behaviorID; //!< The Behavior ID
unsigned int templateID; //!< The Template ID (LOT)
unsigned int effectID; //!< The Effect ID attached
std::unordered_set<std::string>::iterator effectHandle; //!< The effect handle
};
@ -23,6 +24,7 @@ class CDBehaviorTemplateTable : public CDTable {
private:
std::vector<CDBehaviorTemplate> entries;
std::unordered_map<uint32_t, CDBehaviorTemplate> entriesMappedByBehaviorID;
std::unordered_set<std::string> m_EffectHandles;
public:
//! Constructor

View File

@ -445,7 +445,7 @@ Behavior::Behavior(const uint32_t behaviorId)
this->m_effectId = templateInDatabase.effectID;
this->m_effectHandle = templateInDatabase.effectHandle != "" ? new std::string(templateInDatabase.effectHandle) : nullptr;
this->m_effectHandle = *templateInDatabase.effectHandle != "" ? new std::string(*templateInDatabase.effectHandle) : nullptr;
}