1
0

adding the multicache behavior

This commit is contained in:
DayBr3ak 2014-09-01 22:33:58 +02:00
parent 7695471ff9
commit 77409d47eb
3 changed files with 119 additions and 4 deletions

View File

@ -208,6 +208,121 @@ void cBioGenCache::InitializeBiomeGen(cIniFile & a_IniFile)
////////////////////////////////////////////////////////////////////////////////
// cBioGenMulticache:
cBioGenMulticache::cBioGenMulticache(cBiomeGen * a_BioGenToCache, int a_CacheSize, int a_CachesLength) :
m_BioGenToCache(a_BioGenToCache),
m_CacheSize(a_CacheSize),
m_CachesLength(a_CachesLength),
m_CachesOrder(new int*[a_CachesLength * a_CachesLength]),
m_CachesData(new sCacheData*[a_CachesLength * a_CachesLength]),
m_NumHits(0),
m_NumMisses(0),
m_TotalChain(0)
{
for (int i = 0; i < m_CachesLength * m_CachesLength; i++) {
sCacheData * cacheData = m_CachesData[i] = new sCacheData[m_CacheSize];
int * cacheOrder = m_CachesOrder[i] = new int[m_CacheSize];
for (int j = 0; j < m_CacheSize; j++)
{
cacheOrder[j] = j;
cacheData[j].m_ChunkX = 0x7fffffff;
cacheData[j].m_ChunkZ = 0x7fffffff;
}
}
}
cBioGenMulticache::~cBioGenMulticache()
{
for (int i = 0; i < m_CachesLength * m_CachesLength; i++) {
delete[] m_CachesData[i];
delete[] m_CachesOrder[i];
}
delete[] m_CachesData;
m_CachesData = NULL;
delete[] m_CachesOrder;
m_CachesOrder = NULL;
}
void cBioGenMulticache::GenBiomes(int a_ChunkX, int a_ChunkZ, cChunkDef::BiomeMap & a_BiomeMap)
{
if (((m_NumHits + m_NumMisses) % 1024) == 10)
{
LOGD("BioGenCache: %d hits, %d misses, saved %.2f %%", m_NumHits, m_NumMisses, 100.0 * m_NumHits / (m_NumHits + m_NumMisses));
LOGD("BioGenCache: Avg cache chain length: %.2f", (float)m_TotalChain / m_NumHits);
}
int cacheIdx = ((unsigned int)a_ChunkX % m_CachesLength) * m_CachesLength + ((unsigned int)a_ChunkZ % m_CachesLength);
sCacheData * cacheData = m_CachesData[cacheIdx];
int * cacheOrder = m_CachesOrder[cacheIdx];
for (int i = 0; i < m_CacheSize; i++)
{
if (
(cacheData[cacheOrder[i]].m_ChunkX != a_ChunkX) ||
(cacheData[cacheOrder[i]].m_ChunkZ != a_ChunkZ)
)
{
continue;
}
// Found it in the cache
int Idx = cacheOrder[i];
// Move to front:
for (int j = i; j > 0; j--)
{
cacheOrder[j] = cacheOrder[j - 1];
}
cacheOrder[0] = Idx;
// Use the cached data:
memcpy(a_BiomeMap, cacheData[Idx].m_BiomeMap, sizeof(a_BiomeMap));
m_NumHits++;
m_TotalChain += i;
return;
} // for i - cache
// Not in the cache:
m_NumMisses++;
m_BioGenToCache->GenBiomes(a_ChunkX, a_ChunkZ, a_BiomeMap);
// Insert it as the first item in the MRU order:
int Idx = cacheOrder[m_CacheSize - 1];
for (int i = m_CacheSize - 1; i > 0; i--)
{
cacheOrder[i] = cacheOrder[i - 1];
} // for i - m_CacheOrder[]
cacheOrder[0] = Idx;
memcpy(cacheData[Idx].m_BiomeMap, a_BiomeMap, sizeof(a_BiomeMap));
cacheData[Idx].m_ChunkX = a_ChunkX;
cacheData[Idx].m_ChunkZ = a_ChunkZ;
}
void cBioGenMulticache::InitializeBiomeGen(cIniFile & a_IniFile)
{
super::InitializeBiomeGen(a_IniFile);
m_BioGenToCache->InitializeBiomeGen(a_IniFile);
}
////////////////////////////////////////////////////////////////////////////////
// cBiomeGenList:

View File

@ -84,7 +84,7 @@ class cBioGenMulticache :
typedef cBiomeGen super;
public:
cBioGenMulticache(cBiomeGen * a_BioGenToCache, int a_CacheSize, int a_CacheLength); // Doesn't take ownership of a_BioGenToCache
cBioGenMulticache(cBiomeGen * a_BioGenToCache, int a_CacheSize, int a_CachesLength); // Doesn't take ownership of a_BioGenToCache
~cBioGenMulticache();
protected:
@ -100,9 +100,9 @@ protected:
// To avoid moving large amounts of data for the MRU behavior, we MRU-ize indices to an array of the actual data
int m_CacheSize;
int m_CacheLength;
int m_CachesLength;
int **m_CachesOrder; // MRU-ized order, indices into the multiple m_CachesData array
int ** m_CachesOrder; // MRU-ized order, indices into the multiple m_CachesData array
sCacheData ** m_CachesData;
// Cache statistics

View File

@ -241,7 +241,7 @@ void cComposableGenerator::InitBiomeGen(cIniFile & a_IniFile)
}
LOGD("Using a cache for biomegen of size %d.", CacheSize);
m_UnderlyingBiomeGen = m_BiomeGen;
m_BiomeGen = new cBioGenCache(m_UnderlyingBiomeGen, CacheSize);
m_BiomeGen = new cBioGenMulticache(m_UnderlyingBiomeGen, CacheSize, 4);
}
}