summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDayBr3ak <benj.groeneveld@gmail.com>2014-09-01 22:33:58 +0200
committerDayBr3ak <benj.groeneveld@gmail.com>2014-09-02 01:13:19 +0200
commit77409d47eb98eb691f852728cd8e013d0fc14185 (patch)
tree9ce150da93768cbc14993e77e55bbc398df56881
parentadded multicache class definition (diff)
downloadcuberite-77409d47eb98eb691f852728cd8e013d0fc14185.tar
cuberite-77409d47eb98eb691f852728cd8e013d0fc14185.tar.gz
cuberite-77409d47eb98eb691f852728cd8e013d0fc14185.tar.bz2
cuberite-77409d47eb98eb691f852728cd8e013d0fc14185.tar.lz
cuberite-77409d47eb98eb691f852728cd8e013d0fc14185.tar.xz
cuberite-77409d47eb98eb691f852728cd8e013d0fc14185.tar.zst
cuberite-77409d47eb98eb691f852728cd8e013d0fc14185.zip
-rw-r--r--src/Generating/BioGen.cpp115
-rw-r--r--src/Generating/BioGen.h6
-rw-r--r--src/Generating/ComposableGenerator.cpp2
3 files changed, 119 insertions, 4 deletions
diff --git a/src/Generating/BioGen.cpp b/src/Generating/BioGen.cpp
index 8fad9f5c9..c9aeffa54 100644
--- a/src/Generating/BioGen.cpp
+++ b/src/Generating/BioGen.cpp
@@ -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:
diff --git a/src/Generating/BioGen.h b/src/Generating/BioGen.h
index a53d8bca2..f9564ed24 100644
--- a/src/Generating/BioGen.h
+++ b/src/Generating/BioGen.h
@@ -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
diff --git a/src/Generating/ComposableGenerator.cpp b/src/Generating/ComposableGenerator.cpp
index 6f4007d24..057d7accf 100644
--- a/src/Generating/ComposableGenerator.cpp
+++ b/src/Generating/ComposableGenerator.cpp
@@ -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);
}
}