Reduce EnvRef:set_node() time tenfold by postponing the dayNightDiff update until it is actually needed

This commit is contained in:
Perttu Ahola 2012-03-29 00:28:48 +03:00
parent 418041d906
commit 02c035c548
5 changed files with 47 additions and 20 deletions

View File

@ -923,7 +923,7 @@ void Map::updateLighting(core::map<v3s16, MapBlock*> & a_blocks,
i.atEnd() == false; i++) i.atEnd() == false; i++)
{ {
MapBlock *block = i.getNode()->getValue(); MapBlock *block = i.getNode()->getValue();
block->updateDayNightDiff(); block->expireDayNightDiff();
} }
} }
@ -1084,7 +1084,7 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n,
i.atEnd() == false; i++) i.atEnd() == false; i++)
{ {
MapBlock *block = i.getNode()->getValue(); MapBlock *block = i.getNode()->getValue();
block->updateDayNightDiff(); block->expireDayNightDiff();
} }
/* /*
@ -1260,7 +1260,7 @@ void Map::removeNodeAndUpdate(v3s16 p,
i.atEnd() == false; i++) i.atEnd() == false; i++)
{ {
MapBlock *block = i.getNode()->getValue(); 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) bool Map::addNodeWithEvent(v3s16 p, MapNode n)
{ {
ScopeProfiler sp(g_profiler, "Map::addNodeWithEvent", SPT_AVG);
MapEditEvent event; MapEditEvent event;
event.type = MEET_ADDNODE; event.type = MEET_ADDNODE;
event.p = p; event.p = p;
@ -1352,12 +1353,12 @@ bool Map::removeNodeWithEvent(v3s16 p)
return succeeded; return succeeded;
} }
bool Map::dayNightDiffed(v3s16 blockpos) bool Map::getDayNightDiff(v3s16 blockpos)
{ {
try{ try{
v3s16 p = blockpos + v3s16(0,0,0); v3s16 p = blockpos + v3s16(0,0,0);
MapBlock *b = getBlockNoCreate(p); MapBlock *b = getBlockNoCreate(p);
if(b->dayNightDiffed()) if(b->getDayNightDiff())
return true; return true;
} }
catch(InvalidPositionException &e){} catch(InvalidPositionException &e){}
@ -1365,21 +1366,21 @@ bool Map::dayNightDiffed(v3s16 blockpos)
try{ try{
v3s16 p = blockpos + v3s16(-1,0,0); v3s16 p = blockpos + v3s16(-1,0,0);
MapBlock *b = getBlockNoCreate(p); MapBlock *b = getBlockNoCreate(p);
if(b->dayNightDiffed()) if(b->getDayNightDiff())
return true; return true;
} }
catch(InvalidPositionException &e){} catch(InvalidPositionException &e){}
try{ try{
v3s16 p = blockpos + v3s16(0,-1,0); v3s16 p = blockpos + v3s16(0,-1,0);
MapBlock *b = getBlockNoCreate(p); MapBlock *b = getBlockNoCreate(p);
if(b->dayNightDiffed()) if(b->getDayNightDiff())
return true; return true;
} }
catch(InvalidPositionException &e){} catch(InvalidPositionException &e){}
try{ try{
v3s16 p = blockpos + v3s16(0,0,-1); v3s16 p = blockpos + v3s16(0,0,-1);
MapBlock *b = getBlockNoCreate(p); MapBlock *b = getBlockNoCreate(p);
if(b->dayNightDiffed()) if(b->getDayNightDiff())
return true; return true;
} }
catch(InvalidPositionException &e){} catch(InvalidPositionException &e){}
@ -1387,21 +1388,21 @@ bool Map::dayNightDiffed(v3s16 blockpos)
try{ try{
v3s16 p = blockpos + v3s16(1,0,0); v3s16 p = blockpos + v3s16(1,0,0);
MapBlock *b = getBlockNoCreate(p); MapBlock *b = getBlockNoCreate(p);
if(b->dayNightDiffed()) if(b->getDayNightDiff())
return true; return true;
} }
catch(InvalidPositionException &e){} catch(InvalidPositionException &e){}
try{ try{
v3s16 p = blockpos + v3s16(0,1,0); v3s16 p = blockpos + v3s16(0,1,0);
MapBlock *b = getBlockNoCreate(p); MapBlock *b = getBlockNoCreate(p);
if(b->dayNightDiffed()) if(b->getDayNightDiff())
return true; return true;
} }
catch(InvalidPositionException &e){} catch(InvalidPositionException &e){}
try{ try{
v3s16 p = blockpos + v3s16(0,0,1); v3s16 p = blockpos + v3s16(0,0,1);
MapBlock *b = getBlockNoCreate(p); MapBlock *b = getBlockNoCreate(p);
if(b->dayNightDiffed()) if(b->getDayNightDiff())
return true; return true;
} }
catch(InvalidPositionException &e){} catch(InvalidPositionException &e){}
@ -2294,12 +2295,12 @@ MapBlock* ServerMap::finishBlockMake(mapgen::BlockMakeData *data,
/* /*
Update day/night difference cache of the MapBlocks Update day/night difference cache of the MapBlocks
*/ */
block->updateDayNightDiff(); block->expireDayNightDiff();
/* /*
Set block as modified Set block as modified
*/ */
block->raiseModified(MOD_STATE_WRITE_NEEDED, block->raiseModified(MOD_STATE_WRITE_NEEDED,
"finishBlockMake updateDayNightDiff"); "finishBlockMake expireDayNightDiff");
} }
/* /*

View File

@ -226,7 +226,7 @@ public:
/* /*
Takes the blocks at the edges into account Takes the blocks at the edges into account
*/ */
bool dayNightDiffed(v3s16 blockpos); bool getDayNightDiff(v3s16 blockpos);
//core::aabbox3d<s16> getDisplayedBlockArea(); //core::aabbox3d<s16> getDisplayedBlockArea();

View File

@ -49,6 +49,7 @@ MapBlock::MapBlock(Map *parent, v3s16 pos, IGameDef *gamedef, bool dummy):
is_underground(false), is_underground(false),
m_lighting_expired(true), m_lighting_expired(true),
m_day_night_differs(false), m_day_night_differs(false),
m_day_night_differs_expired(true),
m_generated(false), m_generated(false),
m_timestamp(BLOCK_TIMESTAMP_UNDEFINED), m_timestamp(BLOCK_TIMESTAMP_UNDEFINED),
m_disk_timestamp(BLOCK_TIMESTAMP_UNDEFINED), m_disk_timestamp(BLOCK_TIMESTAMP_UNDEFINED),
@ -355,9 +356,11 @@ void MapBlock::copyFrom(VoxelManipulator &dst)
getPosRelative(), data_size); getPosRelative(), data_size);
} }
void MapBlock::updateDayNightDiff() void MapBlock::actuallyUpdateDayNightDiff()
{ {
INodeDefManager *nodemgr = m_gamedef->ndef(); INodeDefManager *nodemgr = m_gamedef->ndef();
// Running this function un-expires m_day_night_differs
m_day_night_differs_expired = false;
if(data == NULL) if(data == NULL)
{ {
@ -404,6 +407,19 @@ void MapBlock::updateDayNightDiff()
m_day_night_differs = differs; 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) s16 MapBlock::getGroundLevel(v2s16 p2d)
{ {
if(isDummy()) if(isDummy())
@ -545,7 +561,7 @@ void MapBlock::serialize(std::ostream &os, u8 version, bool disk)
u8 flags = 0; u8 flags = 0;
if(is_underground) if(is_underground)
flags |= 0x01; flags |= 0x01;
if(m_day_night_differs) if(getDayNightDiff())
flags |= 0x02; flags |= 0x02;
if(m_lighting_expired) if(m_lighting_expired)
flags |= 0x04; flags |= 0x04;
@ -614,6 +630,8 @@ void MapBlock::deSerialize(std::istream &is, u8 version, bool disk)
if(!ser_ver_supported(version)) if(!ser_ver_supported(version))
throw VersionMismatchException("ERROR: MapBlock format not supported"); throw VersionMismatchException("ERROR: MapBlock format not supported");
m_day_night_differs_expired = false;
if(version <= 21) if(version <= 21)
{ {
deSerialize_pre22(is, version, disk); deSerialize_pre22(is, version, disk);
@ -800,7 +818,7 @@ void MapBlock::serialize_pre22(std::ostream &os, u8 version, bool disk)
u8 flags = 0; u8 flags = 0;
if(is_underground) if(is_underground)
flags |= 0x01; flags |= 0x01;
if(m_day_night_differs) if(getDayNightDiff())
flags |= 0x02; flags |= 0x02;
if(m_lighting_expired) if(m_lighting_expired)
flags |= 0x04; flags |= 0x04;

View File

@ -360,10 +360,17 @@ public:
Sets m_day_night_differs to appropriate value. Sets m_day_night_differs to appropriate value.
These methods don't care about neighboring blocks. 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; return m_day_night_differs;
} }
@ -517,6 +524,7 @@ private:
// Whether day and night lighting differs // Whether day and night lighting differs
bool m_day_night_differs; bool m_day_night_differs;
bool m_day_night_differs_expired;
bool m_generated; bool m_generated;

View File

@ -659,7 +659,7 @@ void RemoteClient::GetNextBlocks(Server *server, float dtime,
*/ */
if(d >= 4) if(d >= 4)
{ {
if(block->dayNightDiffed() == false) if(block->getDayNightDiff() == false)
continue; continue;
} }
#endif #endif