From a632d9e2a3f3af6ddecc92d491a713eb5e42dcd8 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Tue, 26 Mar 2019 22:06:00 +0100 Subject: [PATCH] Add zone vector cache. Zone children will be now collected in staging vectors. When the zone is ended (and no children can be added anymore to it), a size-fitted vector is allocated using slab allocation. The over-allocated vector is then put into cache for use in future zones. This is only active for vectors <= 8192 elements, or 64 KB (chosen arbitrarily), to reduce time spent on copying memory. Overall, this change should have the following effects: - System memory allocation pressure reduction, due to re-usage of vectors, which eliminates the need for constant growth. - Reduction of memory usage, because children vectors are now fitted to required size. - Slight increase of zone processing time, due to memory copying? --- server/TracyWorker.cpp | 27 ++++++++++++++++++++++++++- server/TracyWorker.hpp | 2 ++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/server/TracyWorker.cpp b/server/TracyWorker.cpp index c6f603ae..eb1c4f8f 100644 --- a/server/TracyWorker.cpp +++ b/server/TracyWorker.cpp @@ -1978,7 +1978,18 @@ void Worker::NewZone( ZoneEvent* zone, uint64_t thread ) if( back->child < 0 ) { back->child = int32_t( m_data.zoneChildren.size() ); - m_data.zoneChildren.push_back( Vector( zone ) ); + if( m_data.zoneVectorCache.empty() ) + { + m_data.zoneChildren.push_back( Vector( zone ) ); + } + else + { + Vector vze = std::move( m_data.zoneVectorCache.back_and_pop() ); + assert( !vze.empty() ); + vze.clear(); + vze.push_back_non_empty( zone ); + m_data.zoneChildren.push_back( std::move( vze ) ); + } } else { @@ -2613,6 +2624,20 @@ void Worker::ProcessZoneEnd( const QueueZoneEnd& ev ) m_data.lastTime = std::max( m_data.lastTime, zone->end ); + if( zone->child >= 0 ) + { + auto& childVec = m_data.zoneChildren[zone->child]; + const auto sz = childVec.size(); + if( sz <= 8 * 1024 ) + { + Vector fitVec; + fitVec.reserve_exact( sz, m_slab ); + memcpy( fitVec.data(), childVec.data(), sz * sizeof( ZoneEvent* ) ); + std::swap( fitVec, childVec ); + m_data.zoneVectorCache.push_back( std::move( fitVec ) ); + } + } + #ifndef TRACY_NO_STATISTICS auto timeSpan = zone->end - zone->start; if( timeSpan > 0 ) diff --git a/server/TracyWorker.hpp b/server/TracyWorker.hpp index 5168cf66..748a3da5 100644 --- a/server/TracyWorker.hpp +++ b/server/TracyWorker.hpp @@ -178,6 +178,8 @@ private: Vector> zoneChildren; Vector> gpuChildren; + Vector> zoneVectorCache; + CrashEvent crashEvent; };