Reduce EnvRef:set_node() time tenfold by postponing the dayNightDiff update until...
authorPerttu Ahola <celeron55@gmail.com>
Wed, 28 Mar 2012 21:28:48 +0000 (00:28 +0300)
committerPerttu Ahola <celeron55@gmail.com>
Wed, 28 Mar 2012 21:28:48 +0000 (00:28 +0300)
src/map.cpp
src/map.h
src/mapblock.cpp
src/mapblock.h
src/server.cpp

index 835aca4fbdebfbf946b1858424f136a132c58d3d..e24bac2f26dab2f2c8d12777a29a84ebb50b6695 100644 (file)
@@ -923,7 +923,7 @@ void Map::updateLighting(core::map<v3s16, MapBlock*> & a_blocks,
                        i.atEnd() == false; i++)
        {
                MapBlock *block = i.getNode()->getValue();
-               block->updateDayNightDiff();
+               block->expireDayNightDiff();
        }
 }
 
@@ -1084,7 +1084,7 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n,
                        i.atEnd() == false; i++)
        {
                MapBlock *block = i.getNode()->getValue();
-               block->updateDayNightDiff();
+               block->expireDayNightDiff();
        }
 
        /*
@@ -1260,7 +1260,7 @@ void Map::removeNodeAndUpdate(v3s16 p,
                        i.atEnd() == false; i++)
        {
                MapBlock *block = i.getNode()->getValue();
-               block->updateDayNightDiff();
+               block->expireDayNightDiff();
        }
 
        /*
@@ -1297,6 +1297,7 @@ void Map::removeNodeAndUpdate(v3s16 p,
 
 bool Map::addNodeWithEvent(v3s16 p, MapNode n)
 {
+       ScopeProfiler sp(g_profiler, "Map::addNodeWithEvent", SPT_AVG);
        MapEditEvent event;
        event.type = MEET_ADDNODE;
        event.p = p;
@@ -1352,12 +1353,12 @@ bool Map::removeNodeWithEvent(v3s16 p)
        return succeeded;
 }
 
-bool Map::dayNightDiffed(v3s16 blockpos)
+bool Map::getDayNightDiff(v3s16 blockpos)
 {
        try{
                v3s16 p = blockpos + v3s16(0,0,0);
                MapBlock *b = getBlockNoCreate(p);
-               if(b->dayNightDiffed())
+               if(b->getDayNightDiff())
                        return true;
        }
        catch(InvalidPositionException &e){}
@@ -1365,21 +1366,21 @@ bool Map::dayNightDiffed(v3s16 blockpos)
        try{
                v3s16 p = blockpos + v3s16(-1,0,0);
                MapBlock *b = getBlockNoCreate(p);
-               if(b->dayNightDiffed())
+               if(b->getDayNightDiff())
                        return true;
        }
        catch(InvalidPositionException &e){}
        try{
                v3s16 p = blockpos + v3s16(0,-1,0);
                MapBlock *b = getBlockNoCreate(p);
-               if(b->dayNightDiffed())
+               if(b->getDayNightDiff())
                        return true;
        }
        catch(InvalidPositionException &e){}
        try{
                v3s16 p = blockpos + v3s16(0,0,-1);
                MapBlock *b = getBlockNoCreate(p);
-               if(b->dayNightDiffed())
+               if(b->getDayNightDiff())
                        return true;
        }
        catch(InvalidPositionException &e){}
@@ -1387,21 +1388,21 @@ bool Map::dayNightDiffed(v3s16 blockpos)
        try{
                v3s16 p = blockpos + v3s16(1,0,0);
                MapBlock *b = getBlockNoCreate(p);
-               if(b->dayNightDiffed())
+               if(b->getDayNightDiff())
                        return true;
        }
        catch(InvalidPositionException &e){}
        try{
                v3s16 p = blockpos + v3s16(0,1,0);
                MapBlock *b = getBlockNoCreate(p);
-               if(b->dayNightDiffed())
+               if(b->getDayNightDiff())
                        return true;
        }
        catch(InvalidPositionException &e){}
        try{
                v3s16 p = blockpos + v3s16(0,0,1);
                MapBlock *b = getBlockNoCreate(p);
-               if(b->dayNightDiffed())
+               if(b->getDayNightDiff())
                        return true;
        }
        catch(InvalidPositionException &e){}
@@ -2294,12 +2295,12 @@ MapBlock* ServerMap::finishBlockMake(mapgen::BlockMakeData *data,
                /*
                        Update day/night difference cache of the MapBlocks
                */
-               block->updateDayNightDiff();
+               block->expireDayNightDiff();
                /*
                        Set block as modified
                */
                block->raiseModified(MOD_STATE_WRITE_NEEDED,
-                               "finishBlockMake updateDayNightDiff");
+                               "finishBlockMake expireDayNightDiff");
        }
 
        /*
index b71f699c04ebc7ac4141bab702f28cbaadefa118..b84e123e4d8fc478da7c40e20083c069de3e843b 100644 (file)
--- a/src/map.h
+++ b/src/map.h
@@ -226,7 +226,7 @@ public:
        /*
                Takes the blocks at the edges into account
        */
-       bool dayNightDiffed(v3s16 blockpos);
+       bool getDayNightDiff(v3s16 blockpos);
 
        //core::aabbox3d<s16> getDisplayedBlockArea();
 
index 70cb0e370c18146d1ea4dc7f375ff3c10a0fcef4..5ad86fde44f1c40d8ed2c8489dfdf3a32d0284e6 100644 (file)
@@ -49,6 +49,7 @@ MapBlock::MapBlock(Map *parent, v3s16 pos, IGameDef *gamedef, bool dummy):
                is_underground(false),
                m_lighting_expired(true),
                m_day_night_differs(false),
+               m_day_night_differs_expired(true),
                m_generated(false),
                m_timestamp(BLOCK_TIMESTAMP_UNDEFINED),
                m_disk_timestamp(BLOCK_TIMESTAMP_UNDEFINED),
@@ -355,9 +356,11 @@ void MapBlock::copyFrom(VoxelManipulator &dst)
                        getPosRelative(), data_size);
 }
 
-void MapBlock::updateDayNightDiff()
+void MapBlock::actuallyUpdateDayNightDiff()
 {
        INodeDefManager *nodemgr = m_gamedef->ndef();
+       // Running this function un-expires m_day_night_differs
+       m_day_night_differs_expired = false;
 
        if(data == NULL)
        {
@@ -404,6 +407,19 @@ void MapBlock::updateDayNightDiff()
        m_day_night_differs = differs;
 }
 
+void MapBlock::expireDayNightDiff()
+{
+       INodeDefManager *nodemgr = m_gamedef->ndef();
+
+       if(data == NULL){
+               m_day_night_differs = false;
+               m_day_night_differs_expired = false;
+               return;
+       }
+
+       m_day_night_differs_expired = true;
+}
+
 s16 MapBlock::getGroundLevel(v2s16 p2d)
 {
        if(isDummy())
@@ -545,7 +561,7 @@ void MapBlock::serialize(std::ostream &os, u8 version, bool disk)
        u8 flags = 0;
        if(is_underground)
                flags |= 0x01;
-       if(m_day_night_differs)
+       if(getDayNightDiff())
                flags |= 0x02;
        if(m_lighting_expired)
                flags |= 0x04;
@@ -614,6 +630,8 @@ void MapBlock::deSerialize(std::istream &is, u8 version, bool disk)
        if(!ser_ver_supported(version))
                throw VersionMismatchException("ERROR: MapBlock format not supported");
 
+       m_day_night_differs_expired = false;
+
        if(version <= 21)
        {
                deSerialize_pre22(is, version, disk);
@@ -800,7 +818,7 @@ void MapBlock::serialize_pre22(std::ostream &os, u8 version, bool disk)
                u8 flags = 0;
                if(is_underground)
                        flags |= 0x01;
-               if(m_day_night_differs)
+               if(getDayNightDiff())
                        flags |= 0x02;
                if(m_lighting_expired)
                        flags |= 0x04;
index 272da8ce34eea754f5e6a820009e2a5c36322951..4f61d6aa78972b1ea9dfcc6c04a6be27066d530b 100644 (file)
@@ -360,10 +360,17 @@ public:
                Sets m_day_night_differs to appropriate value.
                These methods don't care about neighboring blocks.
        */
-       void updateDayNightDiff();
+       void actuallyUpdateDayNightDiff();
+       /*
+               Call this to schedule what the previous function does to be done
+               when the value is actually needed.
+       */
+       void expireDayNightDiff();
 
-       bool dayNightDiffed()
+       bool getDayNightDiff()
        {
+               if(m_day_night_differs_expired)
+                       actuallyUpdateDayNightDiff();
                return m_day_night_differs;
        }
 
@@ -517,6 +524,7 @@ private:
        
        // Whether day and night lighting differs
        bool m_day_night_differs;
+       bool m_day_night_differs_expired;
 
        bool m_generated;
        
index ff250596df6855d068cdc3eb5031267459893808..3aa0bd04bf33acc5f6ab7af91c9cf19bad9f9e77 100644 (file)
@@ -659,7 +659,7 @@ void RemoteClient::GetNextBlocks(Server *server, float dtime,
                                */
                                if(d >= 4)
                                {
-                                       if(block->dayNightDiffed() == false)
+                                       if(block->getDayNightDiff() == false)
                                                continue;
                                }
 #endif