Skip to content

Commit

Permalink
Enhance ABM performance a little bit by removing two std::set copy (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
nerzhul committed May 25, 2017
1 parent 5b33863 commit 4d5ce84
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/script/lua_api/l_env.h
Expand Up @@ -203,11 +203,11 @@ class LuaABM : public ActiveBlockModifier {
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;
}
virtual std::set<std::string> getRequiredNeighbors()
virtual const std::set<std::string> &getRequiredNeighbors() const
{
return m_required_neighbors;
}
Expand Down
30 changes: 14 additions & 16 deletions src/serverenvironment.cpp
Expand Up @@ -721,7 +721,7 @@ class ABMHandler
chance = 1;
ActiveABM aabm;
aabm.abm = abm;
if(abm->getSimpleCatchUp()) {
if (abm->getSimpleCatchUp()) {
float intervals = actual_interval / trigger_interval;
if(intervals == 0)
continue;
Expand All @@ -731,25 +731,23 @@ class ABMHandler
} else {
aabm.chance = chance;
}

// Trigger neighbors
std::set<std::string> required_neighbors_s
= abm->getRequiredNeighbors();
for(std::set<std::string>::iterator
i = required_neighbors_s.begin();
i != required_neighbors_s.end(); ++i)
{
ndef->getIds(*i, aabm.required_neighbors);
const std::set<std::string> &required_neighbors_s =
abm->getRequiredNeighbors();
for (std::set<std::string>::iterator rn = required_neighbors_s.begin();
rn != required_neighbors_s.end(); ++rn) {
ndef->getIds(*rn, aabm.required_neighbors);
}

// Trigger contents
std::set<std::string> contents_s = abm->getTriggerContents();
for(std::set<std::string>::iterator
i = contents_s.begin(); i != contents_s.end(); ++i)
{
const std::set<std::string> &contents_s = abm->getTriggerContents();
for (std::set<std::string>::iterator cs = contents_s.begin();
cs != contents_s.end(); ++cs) {
std::set<content_t> ids;
ndef->getIds(*i, ids);
for(std::set<content_t>::const_iterator k = ids.begin();
k != ids.end(); ++k)
{
ndef->getIds(*cs, ids);
for (std::set<content_t>::const_iterator k = ids.begin();
k != ids.end(); ++k) {
content_t c = *k;
if (c >= m_aabms.size())
m_aabms.resize(c + 256, NULL);
Expand Down
5 changes: 2 additions & 3 deletions src/serverenvironment.h
Expand Up @@ -51,11 +51,10 @@ class ActiveBlockModifier
virtual ~ActiveBlockModifier(){};

// 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)
// Empty = do not check neighbors
virtual std::set<std::string> getRequiredNeighbors()
{ return std::set<std::string>(); }
virtual const std::set<std::string> &getRequiredNeighbors() const = 0;
// Trigger interval in seconds
virtual float getTriggerInterval() = 0;
// Random chance of (1 / return value), 0 is disallowed
Expand Down

0 comments on commit 4d5ce84

Please sign in to comment.