Enhance ABM performance a little bit by removing two std::set copy (#5815)

* Enhance ABM performance a little bit by removing two std::set copy

* ActiveBlockModifier::getTriggerContents now returns a const ref
* ActiveBlockModifier::getRequiredNeighbors now returns a const ref
* ActiveBlockModifier::getRequiredNeighbors is now purely virtual

* Little code style fix
master
Loïc Blot 2017-05-25 16:43:55 +02:00 committed by GitHub
parent 5b338638e0
commit 4d5ce8478c
3 changed files with 18 additions and 21 deletions

View File

@ -203,11 +203,11 @@ public:
m_simple_catch_up(simple_catch_up) m_simple_catch_up(simple_catch_up)
{ {
} }
virtual std::set<std::string> getTriggerContents() virtual const std::set<std::string> &getTriggerContents() const
{ {
return m_trigger_contents; return m_trigger_contents;
} }
virtual std::set<std::string> getRequiredNeighbors() virtual const std::set<std::string> &getRequiredNeighbors() const
{ {
return m_required_neighbors; return m_required_neighbors;
} }

View File

@ -721,7 +721,7 @@ public:
chance = 1; chance = 1;
ActiveABM aabm; ActiveABM aabm;
aabm.abm = abm; aabm.abm = abm;
if(abm->getSimpleCatchUp()) { if (abm->getSimpleCatchUp()) {
float intervals = actual_interval / trigger_interval; float intervals = actual_interval / trigger_interval;
if(intervals == 0) if(intervals == 0)
continue; continue;
@ -731,25 +731,23 @@ public:
} else { } else {
aabm.chance = chance; aabm.chance = chance;
} }
// Trigger neighbors // Trigger neighbors
std::set<std::string> required_neighbors_s const std::set<std::string> &required_neighbors_s =
= abm->getRequiredNeighbors(); abm->getRequiredNeighbors();
for(std::set<std::string>::iterator for (std::set<std::string>::iterator rn = required_neighbors_s.begin();
i = required_neighbors_s.begin(); rn != required_neighbors_s.end(); ++rn) {
i != required_neighbors_s.end(); ++i) ndef->getIds(*rn, aabm.required_neighbors);
{
ndef->getIds(*i, aabm.required_neighbors);
} }
// Trigger contents // Trigger contents
std::set<std::string> contents_s = abm->getTriggerContents(); const std::set<std::string> &contents_s = abm->getTriggerContents();
for(std::set<std::string>::iterator for (std::set<std::string>::iterator cs = contents_s.begin();
i = contents_s.begin(); i != contents_s.end(); ++i) cs != contents_s.end(); ++cs) {
{
std::set<content_t> ids; std::set<content_t> ids;
ndef->getIds(*i, ids); ndef->getIds(*cs, ids);
for(std::set<content_t>::const_iterator k = ids.begin(); for (std::set<content_t>::const_iterator k = ids.begin();
k != ids.end(); ++k) k != ids.end(); ++k) {
{
content_t c = *k; content_t c = *k;
if (c >= m_aabms.size()) if (c >= m_aabms.size())
m_aabms.resize(c + 256, NULL); m_aabms.resize(c + 256, NULL);

View File

@ -51,11 +51,10 @@ public:
virtual ~ActiveBlockModifier(){}; virtual ~ActiveBlockModifier(){};
// Set of contents to trigger on // Set of contents to trigger on
virtual std::set<std::string> getTriggerContents()=0; virtual const std::set<std::string> &getTriggerContents() const = 0;
// Set of required neighbors (trigger doesn't happen if none are found) // Set of required neighbors (trigger doesn't happen if none are found)
// Empty = do not check neighbors // Empty = do not check neighbors
virtual std::set<std::string> getRequiredNeighbors() virtual const std::set<std::string> &getRequiredNeighbors() const = 0;
{ return std::set<std::string>(); }
// Trigger interval in seconds // Trigger interval in seconds
virtual float getTriggerInterval() = 0; virtual float getTriggerInterval() = 0;
// Random chance of (1 / return value), 0 is disallowed // Random chance of (1 / return value), 0 is disallowed