From 741234e4a589247db7adbcc7aebbdda935f7e391 Mon Sep 17 00:00:00 2001 From: David Williams Date: Mon, 9 Mar 2015 23:52:56 +0100 Subject: [PATCH] Small speed improvement by storing variables separately (rather than in Vector3D) to void construction/comparison overhead. --- include/PolyVox/PagedVolume.h | 12 ++++++++++-- include/PolyVox/PagedVolume.inl | 14 ++++++++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/include/PolyVox/PagedVolume.h b/include/PolyVox/PagedVolume.h index c58e909b..4e7d7fb2 100644 --- a/include/PolyVox/PagedVolume.h +++ b/include/PolyVox/PagedVolume.h @@ -283,11 +283,19 @@ namespace PolyVox private: Chunk* getChunk(int32_t uChunkX, int32_t uChunkY, int32_t uChunkZ) const; + // Storing these properties individually has proved to be faster than keeping + // them in a Vector3DInt32 as it avoids constructions and comparison overheads. + // They are also at the start of the class in the hope that they will be pulled + // into cache - I've got no idea if this actually makes a difference. + mutable int32_t m_v3dLastAccessedChunkX = 0; + mutable int32_t m_v3dLastAccessedChunkY = 0; + mutable int32_t m_v3dLastAccessedChunkZ = 0; + mutable Chunk* m_pLastAccessedChunk = nullptr; + mutable std::unordered_map > m_mapChunks; mutable uint32_t m_uTimestamper = 0; - mutable Vector3DInt32 m_v3dLastAccessedChunkPos = Vector3DInt32(0, 0, 0); //There are no invalid positions, but initially the m_pLastAccessedChunk pointer will be null - mutable Chunk* m_pLastAccessedChunk = nullptr; + uint32_t m_uChunkCountLimit = 0; // The size of the volume diff --git a/include/PolyVox/PagedVolume.inl b/include/PolyVox/PagedVolume.inl index b2a37ca8..5d1288c0 100644 --- a/include/PolyVox/PagedVolume.inl +++ b/include/PolyVox/PagedVolume.inl @@ -259,17 +259,20 @@ namespace PolyVox template typename PagedVolume::Chunk* PagedVolume::getChunk(int32_t uChunkX, int32_t uChunkY, int32_t uChunkZ) const { - Vector3DInt32 v3dChunkPos(uChunkX, uChunkY, uChunkZ); - //Check if we have the same chunk as last time, if so there's no need to even update //the time stamp. If we updated it everytime then that would be every time we touched //a voxel, which would overflow a uint32_t and require us to use a uint64_t instead. //This check should also provide a significant speed boost as usually it is true. - if((v3dChunkPos == m_v3dLastAccessedChunkPos) && (m_pLastAccessedChunk != 0)) + if ((uChunkX == m_v3dLastAccessedChunkX) && + (uChunkY == m_v3dLastAccessedChunkY) && + (uChunkZ == m_v3dLastAccessedChunkZ) && + (m_pLastAccessedChunk != 0)) { return m_pLastAccessedChunk; } + 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. Chunk* pChunk = nullptr; auto itChunk = m_mapChunks.find(v3dChunkPos); @@ -309,7 +312,10 @@ namespace PolyVox } m_pLastAccessedChunk = pChunk; - m_v3dLastAccessedChunkPos = v3dChunkPos; + //m_v3dLastAccessedChunkPos = v3dChunkPos; + m_v3dLastAccessedChunkX = uChunkX; + m_v3dLastAccessedChunkY = uChunkY; + m_v3dLastAccessedChunkZ = uChunkZ; return pChunk; }