From 807d3c42be29d4ab2d8533c699c743c041f4ce6b Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Sat, 21 Oct 2017 13:01:57 +0200 Subject: [PATCH] Use slab allocator for server allocations. --- server/TracySlab.hpp | 6 +++++ server/TracyView.cpp | 52 ++++++++++++++++++++++++++++---------------- server/TracyView.hpp | 2 ++ 3 files changed, 41 insertions(+), 19 deletions(-) diff --git a/server/TracySlab.hpp b/server/TracySlab.hpp index cb85b663..ef2f5048 100644 --- a/server/TracySlab.hpp +++ b/server/TracySlab.hpp @@ -57,6 +57,12 @@ public: return (T*)AllocRaw( sizeof( T ) ); } + template + T* Alloc( size_t size ) + { + return (T*)AllocRaw( sizeof( T ) * size ); + } + void Unalloc( size_t size ) { assert( size <= m_offset ); diff --git a/server/TracyView.cpp b/server/TracyView.cpp index 111c0589..02368362 100644 --- a/server/TracyView.cpp +++ b/server/TracyView.cpp @@ -42,12 +42,6 @@ namespace tracy enum { MinVisSize = 3 }; -static TextData* GetTextData( Event& zone ) -{ - if( !zone.text ) zone.text = new TextData {}; - return zone.text; -} - static View* s_instance = nullptr; View::View( const char* addr ) @@ -174,7 +168,7 @@ View::View( FileRead& f ) f.Read( &ptr, sizeof( ptr ) ); uint64_t ssz; f.Read( &ssz, sizeof( ssz ) ); - auto dst = new char[ssz+1]; + auto dst = m_slab.Alloc( ssz+1 ); f.Read( dst, ssz ); dst[ssz] = '\0'; m_customStrings.emplace( dst ); @@ -224,7 +218,7 @@ View::View( FileRead& f ) { uint64_t ptr, tsz; f.Read( &ptr, sizeof( ptr ) ); - auto msgdata = new MessageData; + auto msgdata = m_slab.Alloc(); f.Read( &msgdata->_time_literal, sizeof( msgdata->_time_literal ) ); if( msgdata->literal ) { @@ -233,7 +227,7 @@ View::View( FileRead& f ) else { f.Read( &tsz, sizeof( tsz ) ); - auto txt = new char[tsz+1]; + auto txt = m_slab.Alloc( tsz+1 ); f.Read( txt, tsz ); txt[tsz] = '\0'; msgdata->txt = txt; @@ -246,7 +240,7 @@ View::View( FileRead& f ) m_threads.reserve( sz ); for( uint64_t i=0; i(); f.Read( &td->id, sizeof( td->id ) ); ReadTimeline( f, td->timeline, nullptr, stringMap ); uint64_t msz; @@ -266,7 +260,7 @@ View::View( FileRead& f ) m_plots.reserve( sz ); for( uint64_t i=0; i(); f.Read( &pd->name, sizeof( pd->name ) ); f.Read( &pd->min, sizeof( pd->min ) ); f.Read( &pd->max, sizeof( pd->max ) ); @@ -757,7 +751,7 @@ void View::ProcessMessage( const QueueMessage& ev ) void View::ProcessMessageLiteral( const QueueMessage& ev ) { CheckString( ev.text ); - auto msg = new MessageData; + auto msg = m_slab.Alloc(); msg->time = int64_t( ev.time * m_timerMul ); msg->literal = true; msg->str = ev.text; @@ -831,7 +825,7 @@ void View::AddCustomString( uint64_t ptr, std::string&& str ) if( sit == m_customStrings.end() ) { const auto sz = str.size(); - auto ptr = new char[sz+1]; + auto ptr = m_slab.Alloc( sz+1 ); memcpy( ptr, str.c_str(), sz ); ptr[sz] = '\0'; GetTextData( *pit->second )->userText = ptr; @@ -859,13 +853,13 @@ void View::AddSourceLocation( const QueueSourceLocation& srcloc ) void View::AddMessageData( uint64_t ptr, const char* str, size_t sz ) { - auto txt = new char[sz+1]; + auto txt = m_slab.Alloc( sz+1 ); memcpy( txt, str, sz ); txt[sz] = '\0'; auto it = m_pendingMessages.find( ptr ); assert( it != m_pendingMessages.end() ); - auto msg = new MessageData; + auto msg = m_slab.Alloc(); msg->time = it->second.time; msg->literal = false; msg->txt = txt; @@ -891,7 +885,10 @@ void View::InsertMessageData( MessageData* msg, uint64_t thread ) if( tit == m_threadMap.end() ) { m_threadMap.emplace( thread, (uint32_t)m_threads.size() ); - m_threads.push_back( new ThreadData { thread, true } ); + auto td = m_slab.AllocInit(); + td->id = thread; + td->enabled = true; + m_threads.push_back( td ); vec = &m_threads.back()->messages; } else @@ -919,7 +916,10 @@ void View::NewZone( Event* zone, uint64_t thread ) { CheckThreadString( thread ); m_threadMap.emplace( thread, (uint32_t)m_threads.size() ); - m_threads.push_back( new ThreadData { thread, true } ); + auto td = m_slab.AllocInit(); + td->id = thread; + td->enabled = true; + m_threads.push_back( td ); timeline = &m_threads.back()->timeline; } else @@ -967,7 +967,10 @@ void View::InsertLockEvent( LockMap& lockmap, LockEvent* lev, uint64_t thread ) { CheckThreadString( thread ); m_threadMap.emplace( thread, (uint32_t)m_threads.size() ); - m_threads.push_back( new ThreadData { thread, true } ); + auto td = m_slab.AllocInit(); + td->id = thread; + td->enabled = true; + m_threads.push_back( td ); } auto it = lockmap.threadMap.find( thread ); @@ -2891,6 +2894,17 @@ void View::ZoneTooltip( const Event& ev ) ImGui::EndTooltip(); } +TextData* View::GetTextData( Event& zone ) +{ + if( !zone.text ) + { + zone.text = m_slab.Alloc(); + zone.text->userText = nullptr; + zone.text->zoneName = 0; + } + return zone.text; +} + void View::Write( FileWrite& f ) { f.Write( &m_delay, sizeof( m_delay ) ); @@ -3066,7 +3080,7 @@ void View::ReadTimeline( FileRead& f, Vector& vec, Event* parent, const f.Read( &flag, sizeof( flag ) ); if( flag ) { - zone->text = new TextData; + zone->text = m_slab.Alloc(); uint64_t ptr; f.Read( &ptr, sizeof( ptr ) ); zone->text->userText = ptr == 0 ? nullptr : stringMap.find( ptr )->second; diff --git a/server/TracyView.hpp b/server/TracyView.hpp index 504cfad6..6d0992bd 100644 --- a/server/TracyView.hpp +++ b/server/TracyView.hpp @@ -185,6 +185,8 @@ private: void ZoomToZone( const Event& ev ); void ZoneTooltip( const Event& ev ); + TextData* GetTextData( Event& zone ); + void Write( FileWrite& f ); void WriteTimeline( FileWrite& f, const Vector& vec ); void ReadTimeline( FileRead& f, Vector& vec, Event* parent, const std::unordered_map& stringMap );