1
0

Improved the data structure for storing loaded chunks

Should significantly increase the performance of deciding which chunks to stream
This commit is contained in:
tycho 2015-10-05 16:20:07 +01:00
parent 0786fda18b
commit d8c860ae58
2 changed files with 8 additions and 8 deletions

View File

@ -456,7 +456,7 @@ bool cClientHandle::StreamNextChunk(void)
// If the chunk already loading / loaded -> skip // If the chunk already loading / loaded -> skip
if ( if (
(m_ChunksToSend.find(Coords) != m_ChunksToSend.end()) || (m_ChunksToSend.find(Coords) != m_ChunksToSend.end()) ||
(std::find(m_LoadedChunks.begin(), m_LoadedChunks.end(), Coords) != m_LoadedChunks.end()) (m_LoadedChunks.find(Coords) != m_LoadedChunks.end())
) )
{ {
continue; continue;
@ -494,7 +494,7 @@ bool cClientHandle::StreamNextChunk(void)
// If the chunk already loading / loaded -> skip // If the chunk already loading / loaded -> skip
if ( if (
(m_ChunksToSend.find(Coords) != m_ChunksToSend.end()) || (m_ChunksToSend.find(Coords) != m_ChunksToSend.end()) ||
(std::find(m_LoadedChunks.begin(), m_LoadedChunks.end(), Coords) != m_LoadedChunks.end()) (m_LoadedChunks.find(Coords) != m_LoadedChunks.end())
) )
{ {
continue; continue;
@ -525,7 +525,7 @@ void cClientHandle::UnloadOutOfRangeChunks(void)
cChunkCoordsList ChunksToRemove; cChunkCoordsList ChunksToRemove;
{ {
cCSLock Lock(m_CSChunkLists); cCSLock Lock(m_CSChunkLists);
for (cChunkCoordsList::iterator itr = m_LoadedChunks.begin(); itr != m_LoadedChunks.end();) for (auto itr = m_LoadedChunks.begin(); itr != m_LoadedChunks.end();)
{ {
int DiffX = Diff((*itr).m_ChunkX, ChunkPosX); int DiffX = Diff((*itr).m_ChunkX, ChunkPosX);
int DiffZ = Diff((*itr).m_ChunkZ, ChunkPosZ); int DiffZ = Diff((*itr).m_ChunkZ, ChunkPosZ);
@ -581,7 +581,7 @@ void cClientHandle::StreamChunk(int a_ChunkX, int a_ChunkZ, cChunkSender::eChunk
{ {
{ {
cCSLock Lock(m_CSChunkLists); cCSLock Lock(m_CSChunkLists);
m_LoadedChunks.push_back(cChunkCoords(a_ChunkX, a_ChunkZ)); m_LoadedChunks.emplace(a_ChunkX, a_ChunkZ);
m_ChunksToSend.emplace(a_ChunkX, a_ChunkZ); m_ChunksToSend.emplace(a_ChunkX, a_ChunkZ);
} }
World->SendChunkTo(a_ChunkX, a_ChunkZ, a_Priority, this); World->SendChunkTo(a_ChunkX, a_ChunkZ, a_Priority, this);
@ -1821,15 +1821,15 @@ void cClientHandle::SendData(const char * a_Data, size_t a_Size)
void cClientHandle::RemoveFromWorld(void) void cClientHandle::RemoveFromWorld(void)
{ {
// Remove all associated chunks: // Remove all associated chunks:
cChunkCoordsList Chunks; decltype(m_LoadedChunks) Chunks;
{ {
cCSLock Lock(m_CSChunkLists); cCSLock Lock(m_CSChunkLists);
std::swap(Chunks, m_LoadedChunks); std::swap(Chunks, m_LoadedChunks);
m_ChunksToSend.clear(); m_ChunksToSend.clear();
} }
for (cChunkCoordsList::iterator itr = Chunks.begin(), end = Chunks.end(); itr != end; ++itr) for (auto && Chunk : Chunks)
{ {
m_Protocol->SendUnloadChunk(itr->m_ChunkX, itr->m_ChunkZ); m_Protocol->SendUnloadChunk(Chunk.m_ChunkX, Chunk.m_ChunkZ);
} // for itr - Chunks[] } // for itr - Chunks[]
// Here, we set last streamed values to bogus ones so everything is resent // Here, we set last streamed values to bogus ones so everything is resent

View File

@ -383,7 +383,7 @@ private:
Json::Value m_Properties; Json::Value m_Properties;
cCriticalSection m_CSChunkLists; cCriticalSection m_CSChunkLists;
cChunkCoordsList m_LoadedChunks; // Chunks that the player belongs to std::unordered_set<cChunkCoords, cChunkCoordsHash> m_LoadedChunks; // Chunks that the player belongs to
std::unordered_set<cChunkCoords, cChunkCoordsHash> m_ChunksToSend; // Chunks that need to be sent to the player (queued because they weren't generated yet or there's not enough time to send them) std::unordered_set<cChunkCoords, cChunkCoordsHash> m_ChunksToSend; // Chunks that need to be sent to the player (queued because they weren't generated yet or there's not enough time to send them)
cChunkCoordsList m_SentChunks; // Chunks that are currently sent to the client cChunkCoordsList m_SentChunks; // Chunks that are currently sent to the client