Initial work on replacing std::unordered_map with a specialized hash table for looking up chunks based on their 3D position.

This commit is contained in:
David Williams 2015-04-09 23:44:25 +02:00
parent 27a59f34bc
commit a2fe1944af
2 changed files with 55 additions and 4 deletions

View File

@ -310,6 +310,8 @@ namespace PolyVox
// The size of the volume // The size of the volume
//Region m_regValidRegionInChunks; //Region m_regValidRegionInChunks;
mutable std::unique_ptr< Chunk > m_arrayChunks[16384];
// The size of the chunks // The size of the chunks
uint16_t m_uChunkSideLength; uint16_t m_uChunkSideLength;
uint8_t m_uChunkSideLengthPower; uint8_t m_uChunkSideLengthPower;

View File

@ -273,7 +273,7 @@ namespace PolyVox
Vector3DInt32 v3dChunkPos(uChunkX, uChunkY, uChunkZ); Vector3DInt32 v3dChunkPos(uChunkX, uChunkY, uChunkZ);
// The chunk was not the same as last time, but we can now hope it is in the set of most recently used chunks. // The chunk was not the same as last time, but we can now hope it is in the set of most recently used chunks.
Chunk* pChunk = nullptr; /*Chunk* pChunk = nullptr;
auto itChunk = m_mapChunks.find(v3dChunkPos); auto itChunk = m_mapChunks.find(v3dChunkPos);
// Check whether the chunk was found. // Check whether the chunk was found.
@ -283,6 +283,45 @@ namespace PolyVox
pChunk = itChunk->second.get(); pChunk = itChunk->second.get();
POLYVOX_ASSERT(pChunk, "Recent chunk list shold never contain a null pointer."); POLYVOX_ASSERT(pChunk, "Recent chunk list shold never contain a null pointer.");
pChunk->m_uChunkLastAccessed = ++m_uTimestamper; pChunk->m_uChunkLastAccessed = ++m_uTimestamper;
}*/
Chunk* pChunk = nullptr;
const int32_t startIndex = (((uChunkX & 0xFF)) | ((uChunkY & 0x0F) << 4) | ((uChunkZ & 0x0F) << 8) << 2 );
for (uint32_t ct = startIndex; ct < 16384; ct++)
{
if (m_arrayChunks[ct])
{
Vector3DInt32& entryPos = m_arrayChunks[ct]->m_v3dChunkSpacePosition;
if (entryPos.getX() == uChunkX && entryPos.getY() == uChunkY && entryPos.getZ() == uChunkZ)
{
pChunk = m_arrayChunks[ct].get();
break;
}
}
}
if (pChunk == nullptr)
{
for (uint32_t ct = 0; ct < startIndex; ct++)
{
if (m_arrayChunks[ct])
{
Vector3DInt32& entryPos = m_arrayChunks[ct]->m_v3dChunkSpacePosition;
if (entryPos.getX() == uChunkX && entryPos.getY() == uChunkY && entryPos.getZ() == uChunkZ)
{
pChunk = m_arrayChunks[ct].get();
break;
}
}
}
}
// Check whether the chunk was found.
if (pChunk)
{
// The chunk was found so we can use it.
POLYVOX_ASSERT(pChunk, "Recent chunk list shold never contain a null pointer.");
pChunk->m_uChunkLastAccessed = ++m_uTimestamper;
} }
// If we still haven't found the chunk then it's time to create a new one and page it in from disk. // If we still haven't found the chunk then it's time to create a new one and page it in from disk.
@ -291,10 +330,18 @@ namespace PolyVox
// The chunk was not found so we will create a new one. // The chunk was not found so we will create a new one.
pChunk = new PagedVolume<VoxelType>::Chunk(v3dChunkPos, m_uChunkSideLength, m_pPager); pChunk = new PagedVolume<VoxelType>::Chunk(v3dChunkPos, m_uChunkSideLength, m_pPager);
pChunk->m_uChunkLastAccessed = ++m_uTimestamper; // Important, as we may soon delete the oldest chunk pChunk->m_uChunkLastAccessed = ++m_uTimestamper; // Important, as we may soon delete the oldest chunk
m_mapChunks.insert(std::make_pair(v3dChunkPos, std::unique_ptr<Chunk>(pChunk))); //m_mapChunks.insert(std::make_pair(v3dChunkPos, std::unique_ptr<Chunk>(pChunk)));
for (uint32_t ct = startIndex; ct < 16384; ct++)
{
if (m_arrayChunks[ct] == nullptr)
{
m_arrayChunks[ct] = std::move(std::unique_ptr< Chunk >(pChunk));
break;
}
}
// As we are loading a new chunk we should try to ensure we don't go over our target memory usage. // As we are loading a new chunk we should try to ensure we don't go over our target memory usage.
while (m_mapChunks.size() > m_uChunkCountLimit) /*while (m_mapChunks.size() > m_uChunkCountLimit)
{ {
// Find the least recently used chunk. Hopefully this isn't too slow. // Find the least recently used chunk. Hopefully this isn't too slow.
auto itUnloadChunk = m_mapChunks.begin(); auto itUnloadChunk = m_mapChunks.begin();
@ -308,7 +355,9 @@ namespace PolyVox
// Erase the least recently used chunk // Erase the least recently used chunk
m_mapChunks.erase(itUnloadChunk); m_mapChunks.erase(itUnloadChunk);
} }*/
} }
m_pLastAccessedChunk = pChunk; m_pLastAccessedChunk = pChunk;