Enhance ABM performance a little bit by removing two std::set copy (#5815)
authorLoïc Blot <nerzhul@users.noreply.github.com>
Thu, 25 May 2017 14:43:55 +0000 (16:43 +0200)
committerGitHub <noreply@github.com>
Thu, 25 May 2017 14:43:55 +0000 (16:43 +0200)
* 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

src/script/lua_api/l_env.h
src/serverenvironment.cpp
src/serverenvironment.h

index 629c6bc39e53591a0839d19c2e06d7b20e63ea62..7ce19b085f7984e6a882cd482af117a4f350bfd7 100644 (file)
@@ -203,11 +203,11 @@ public:
                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;
        }
index 892c6c55bec31ccca596efb2b0944504ad10ed05..cbdc747d1f9e8cd375a59bbcf79d425a65c7dcea 100644 (file)
@@ -721,7 +721,7 @@ public:
                                chance = 1;
                        ActiveABM aabm;
                        aabm.abm = abm;
-                       if(abm->getSimpleCatchUp()) {
+                       if (abm->getSimpleCatchUp()) {
                                float intervals = actual_interval / trigger_interval;
                                if(intervals == 0)
                                        continue;
@@ -731,25 +731,23 @@ public:
                        } 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);
index 0e31aa41a2b0727f77a69428c3be899bbd9f0dca..7c370fd5495cedc725e6433d45b3a3ba7998a669 100644 (file)
@@ -51,11 +51,10 @@ public:
        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