clear cache lookups when appropriate

This commit is contained in:
Mark Vejvoda 2013-11-13 19:00:33 +00:00
parent 94b8a346f2
commit 2d8f0f7430
5 changed files with 73 additions and 85 deletions

View File

@ -561,18 +561,16 @@ void Faction::end() {
void Faction::notifyUnitAliveStatusChange(const Unit *unit) {
if(unit != NULL) {
if(unit->isAlive() == true) {
aliveUnitList[unit->getId()] = unit;
aliveUnitListCache[unit->getId()] = unit;
if(unit->getType()->isMobile() == true) {
mobileUnitList[unit->getId()] = unit;
mobileUnitListCache[unit->getId()] = unit;
}
}
else {
aliveUnitList.erase(unit->getId());
if(unit->getType()->isMobile() == true) {
mobileUnitList.erase(unit->getId());
}
aliveUnitListCache.erase(unit->getId());
mobileUnitListCache.erase(unit->getId());
beingBuiltUnitListCache.erase(unit->getId());
}
}
}
@ -580,11 +578,11 @@ void Faction::notifyUnitAliveStatusChange(const Unit *unit) {
void Faction::notifyUnitTypeChange(const Unit *unit, const UnitType *newType) {
if(unit != NULL) {
if(unit->getType()->isMobile() == true) {
mobileUnitList.erase(unit->getId());
mobileUnitListCache.erase(unit->getId());
}
if(newType != NULL && newType->isMobile() == true) {
mobileUnitList[unit->getId()] = unit;
mobileUnitListCache[unit->getId()] = unit;
}
}
}
@ -592,26 +590,26 @@ void Faction::notifyUnitTypeChange(const Unit *unit, const UnitType *newType) {
void Faction::notifyUnitSkillTypeChange(const Unit *unit, const SkillType *newType) {
if(unit != NULL) {
if(unit->isBeingBuilt() == true) {
beingBuiltUnitList.erase(unit->getId());
beingBuiltUnitListCache.erase(unit->getId());
}
if(newType != NULL && newType->getClass() == scBeBuilt) {
beingBuiltUnitList[unit->getId()] = unit;
beingBuiltUnitListCache[unit->getId()] = unit;
}
}
}
bool Faction::hasAliveUnits(bool filterMobileUnits, bool filterBuiltUnits) const {
bool result = false;
if(aliveUnitList.empty() == false) {
if(aliveUnitListCache.empty() == false) {
if(filterMobileUnits == true) {
result = (mobileUnitList.empty() == false);
result = (mobileUnitListCache.empty() == false);
}
else {
result = true;
}
if(result == true && filterBuiltUnits == true) {
result = (beingBuiltUnitList.empty() == true);
result = (beingBuiltUnitListCache.empty() == true);
}
}
return result;
@ -1995,7 +1993,10 @@ bool Faction::canCreateUnit(const UnitType *ut, bool checkBuild, bool checkProdu
void Faction::clearCaches() {
cacheResourceTargetList.clear();
cachedCloseResourceTargetLookupList.clear();
mapSharedPathFinderCache.clear();
//aliveUnitListCache.clear();
//mobileUnitListCache.clear();
//beingBuiltUnitListCache.clear();
unsigned int unitCount = this->getUnitCount();
for(unsigned int i = 0; i < unitCount; ++i) {
@ -2006,9 +2007,12 @@ void Faction::clearCaches() {
}
}
uint64 Faction::getCacheKBytes(uint64 *cache1Size, uint64 *cache2Size) {
uint64 Faction::getCacheKBytes(uint64 *cache1Size, uint64 *cache2Size, uint64 *cache3Size, uint64 *cache4Size, uint64 *cache5Size) {
uint64 cache1Count = 0;
uint64 cache2Count = 0;
uint64 cache3Count = 0;
uint64 cache4Count = 0;
uint64 cache5Count = 0;
for(std::map<Vec2i,int>::iterator iterMap1 = cacheResourceTargetList.begin();
iterMap1 != cacheResourceTargetList.end(); ++iterMap1) {
@ -2018,6 +2022,18 @@ uint64 Faction::getCacheKBytes(uint64 *cache1Size, uint64 *cache2Size) {
iterMap1 != cachedCloseResourceTargetLookupList.end(); ++iterMap1) {
cache2Count++;
}
for(std::map<int,const Unit *>::iterator iterMap1 = aliveUnitListCache.begin();
iterMap1 != aliveUnitListCache.end(); ++iterMap1) {
cache3Count++;
}
for(std::map<int,const Unit *>::iterator iterMap1 = mobileUnitListCache.begin();
iterMap1 != mobileUnitListCache.end(); ++iterMap1) {
cache4Count++;
}
for(std::map<int,const Unit *>::iterator iterMap1 = beingBuiltUnitListCache.begin();
iterMap1 != beingBuiltUnitListCache.end(); ++iterMap1) {
cache5Count++;
}
if(cache1Size) {
*cache1Size = cache1Count;
@ -2025,9 +2041,21 @@ uint64 Faction::getCacheKBytes(uint64 *cache1Size, uint64 *cache2Size) {
if(cache2Size) {
*cache2Size = cache2Count;
}
if(cache3Size) {
*cache3Size = cache3Count;
}
if(cache4Size) {
*cache4Size = cache4Count;
}
if(cache5Size) {
*cache5Size = cache5Count;
}
uint64 totalBytes = cache1Count * sizeof(int);
totalBytes += cache2Count * sizeof(bool);
totalBytes += cache3Count * (sizeof(int) + sizeof(const Unit *));
totalBytes += cache4Count * (sizeof(int) + sizeof(const Unit *));
totalBytes += cache5Count * (sizeof(int) + sizeof(const Unit *));
totalBytes /= 1000;

View File

@ -173,15 +173,13 @@ private:
TechTree *techTree;
const XmlNode *loadWorldNode;
std::map<int,std::map<Field, std::map<Vec2i,bool> > > mapSharedPathFinderCache;
std::vector<string> worldSynchThreadedLogList;
std::map<int,string> crcWorldFrameDetails;
std::map<int,const Unit *> aliveUnitList;
std::map<int,const Unit *> mobileUnitList;
std::map<int,const Unit *> beingBuiltUnitList;
std::map<int,const Unit *> aliveUnitListCache;
std::map<int,const Unit *> mobileUnitListCache;
std::map<int,const Unit *> beingBuiltUnitListCache;
public:
Faction();
@ -222,27 +220,6 @@ public:
}
}
inline const bool * aproxCanMoveSoonCached(int size, Field field, const Vec2i &pos1, const Vec2i &pos2) const {
const bool *result = NULL;
// std::map<int,std::map<Field, std::map<Vec2i,bool> > >::const_iterator iterFind1 = mapSharedPathFinderCache.find(size);
// if(iterFind1 != mapSharedPathFinderCache.end()) {
// std::map<Field, std::map<Vec2i,bool> >::const_iterator iterFind2 = iterFind1->second.find(field);
// if(iterFind2 != iterFind1->second.end()) {
// std::map<Vec2i,bool>::const_iterator iterFind3 = iterFind2->second.find(pos2);
// if(iterFind3 != iterFind2->second.end()) {
// result = &iterFind3->second;
// }
// }
// }
return result;
}
inline void addAproxCanMoveSoonCached(int size, Field field, const Vec2i &pos1, const Vec2i &pos2,bool result) {
//mapSharedPathFinderCache[size][field][pos2]=result;
}
inline void clearAproxCanMoveSoonCached() {
//mapSharedPathFinderCache.clear();
}
inline void addLivingUnits(int id) { livingUnits.insert(id); }
inline void addLivingUnitsp(Unit *unit) { livingUnitsp.insert(unit); }
@ -379,7 +356,7 @@ public:
bool canCreateUnit(const UnitType *ut, bool checkBuild, bool checkProduce, bool checkMorph) const;
string getCacheStats();
uint64 getCacheKBytes(uint64 *cache1Size, uint64 *cache2Size);
uint64 getCacheKBytes(uint64 *cache1Size, uint64 *cache2Size, uint64 *cache3Size, uint64 *cache4Size, uint64 *cache5Size);
std::string toString(bool crcMode=false) const;

View File

@ -4177,6 +4177,11 @@ void Unit::clearCaches() {
cachedFow.surfPosAlphaList.clear();
cachedFowPos = Vec2i(0,0);
cacheExploredCells.exploredCellList.clear();
cacheExploredCells.visibleCellList.clear();
cacheExploredCellsKey.first = Vec2i(-1,-1);
cacheExploredCellsKey.second = -1;
if(unitPath != NULL) {
unitPath->clearCaches();
}

View File

@ -409,56 +409,33 @@ public:
int teamIndex= unit->getTeam();
Field field= unit->getCurrField();
const bool *cachedResult = unit->getFaction()->aproxCanMoveSoonCached(size,field,pos1,pos2);
if(cachedResult != NULL) {
return *cachedResult;
}
//single cell units
if(size == 1) {
if(isAproxFreeCellOrMightBeFreeSoon(unit->getPosNotThreadSafe(),pos2, field, teamIndex) == false) {
//printf("[%s] Line: %d returning false\n",__FUNCTION__,__LINE__);
unit->getFaction()->addAproxCanMoveSoonCached(size,field, pos1, pos2, false);
return false;
}
if(pos1.x != pos2.x && pos1.y != pos2.y) {
if(isAproxFreeCellOrMightBeFreeSoon(unit->getPosNotThreadSafe(),Vec2i(pos1.x, pos2.y), field, teamIndex) == false) {
//Unit *cellUnit = getCell(Vec2i(pos1.x, pos2.y))->getUnit(field);
//Object * obj = getSurfaceCell(toSurfCoords(Vec2i(pos1.x, pos2.y)))->getObject();
//printf("[%s] Line: %d returning false cell [%s] free [%d] cell unitid = %d object class = %d\n",__FUNCTION__,__LINE__,Vec2i(pos1.x, pos2.y).getString().c_str(),this->isFreeCell(Vec2i(pos1.x, pos2.y),field),(cellUnit != NULL ? cellUnit->getId() : -1),(obj != NULL ? obj->getType()->getClass() : -1));
//printf("[%s] Line: %d returning false\n",__FUNCTION__,__LINE__);
unit->getFaction()->addAproxCanMoveSoonCached(size,field, pos1, pos2, false);
return false;
}
if(isAproxFreeCellOrMightBeFreeSoon(unit->getPosNotThreadSafe(),Vec2i(pos2.x, pos1.y), field, teamIndex) == false) {
//printf("[%s] Line: %d returning false\n",__FUNCTION__,__LINE__);
unit->getFaction()->addAproxCanMoveSoonCached(size,field, pos1, pos2, false);
return false;
}
}
bool isBadHarvestPos = false;
//if(unit != NULL) {
Command *command= unit->getCurrCommand();
if(command != NULL) {
const HarvestCommandType *hct = dynamic_cast<const HarvestCommandType*>(command->getCommandType());
if(hct != NULL && unit->isBadHarvestPos(pos2) == true) {
isBadHarvestPos = true;
}
Command *command= unit->getCurrCommand();
if(command != NULL) {
const HarvestCommandType *hct = dynamic_cast<const HarvestCommandType*>(command->getCommandType());
if(hct != NULL && unit->isBadHarvestPos(pos2) == true) {
isBadHarvestPos = true;
}
//}
}
if(unit == NULL || isBadHarvestPos == true) {
//printf("[%s] Line: %d returning false\n",__FUNCTION__,__LINE__);
unit->getFaction()->addAproxCanMoveSoonCached(size,field, pos1, pos2, false);
return false;
}
unit->getFaction()->addAproxCanMoveSoonCached(size,field, pos1, pos2, true);
return true;
}
//multi cell units
@ -470,17 +447,11 @@ public:
if(isInside(cellPos) && isInsideSurface(toSurfCoords(cellPos))) {
if(getCell(cellPos)->getUnit(unit->getCurrField()) != unit) {
if(isAproxFreeCellOrMightBeFreeSoon(unit->getPosNotThreadSafe(),cellPos, field, teamIndex) == false) {
//printf("[%s] Line: %d returning false\n",__FUNCTION__,__LINE__);
unit->getFaction()->addAproxCanMoveSoonCached(size,field, pos1, pos2, false);
return false;
}
}
}
else {
//printf("[%s] Line: %d returning false\n",__FUNCTION__,__LINE__);
unit->getFaction()->addAproxCanMoveSoonCached(size,field, pos1, pos2, false);
return false;
}
}
@ -496,14 +467,10 @@ public:
}
if(isBadHarvestPos == true) {
//printf("[%s] Line: %d returning false\n",__FUNCTION__,__LINE__);
unit->getFaction()->addAproxCanMoveSoonCached(size,field, pos1, pos2, false);
return false;
}
}
unit->getFaction()->addAproxCanMoveSoonCached(size,field, pos1, pos2, true);
return true;
}

View File

@ -599,7 +599,6 @@ void World::updateAllFactionUnits() {
for(int i = 0; i < factionCount; ++i) {
Faction *faction = getFaction(i);
faction->clearUnitsPathfinding();
faction->clearAproxCanMoveSoonCached();
faction->clearWorldSynchThreadedLogList();
}
@ -2702,16 +2701,28 @@ string World::getAllFactionsCacheStats() {
uint64 totalBytes = 0;
uint64 totalCache1Size = 0;
uint64 totalCache2Size = 0;
uint64 totalCache3Size = 0;
uint64 totalCache4Size = 0;
uint64 totalCache5Size = 0;
for(int i = 0; i < getFactionCount(); ++i) {
uint64 cache1Size = 0;
uint64 cache2Size = 0;
totalBytes += getFaction(i)->getCacheKBytes(&cache1Size, &cache2Size);
uint64 cache3Size = 0;
uint64 cache4Size = 0;
uint64 cache5Size = 0;
totalBytes += getFaction(i)->getCacheKBytes(&cache1Size,&cache2Size,&cache3Size,&cache4Size,&cache5Size);
totalCache1Size += cache1Size;
totalCache2Size += cache2Size;
totalCache3Size += cache3Size;
totalCache4Size += cache4Size;
totalCache5Size += cache5Size;
}
char szBuf[8096]="";
snprintf(szBuf,8096,"totalCache1Size [%llu] totalCache1Size [%llu] total KB: %s",(long long unsigned)totalCache1Size,(long long unsigned)totalCache2Size,formatNumber(totalBytes).c_str());
snprintf(szBuf,8096,"Cache1 [%llu] Cache2 [%llu] Cache3 [%llu] Cache4 [%llu] Cache5 [%llu] total KB: %s",
(long long unsigned)totalCache1Size,(long long unsigned)totalCache2Size,(long long unsigned)totalCache3Size,
(long long unsigned)totalCache4Size,(long long unsigned)totalCache5Size,formatNumber(totalBytes).c_str());
result = szBuf;
return result;
}