From cc8683a399e9f46e264bad49751aed64173d97fd Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Sun, 22 Oct 2017 16:40:15 +0200 Subject: [PATCH] Store TextData pointer as an index in array. This further reduces ZoneEvent size by 4 bytes. --- server/TracyEvent.hpp | 2 +- server/TracyView.cpp | 111 ++++++++++++++++++++++-------------------- server/TracyView.hpp | 2 + test/test.cpp | 4 ++ 4 files changed, 64 insertions(+), 55 deletions(-) diff --git a/server/TracyEvent.hpp b/server/TracyEvent.hpp index 2a892947..182d5573 100644 --- a/server/TracyEvent.hpp +++ b/server/TracyEvent.hpp @@ -24,7 +24,7 @@ struct ZoneEvent int8_t cpu_start; int8_t cpu_end; - TextData* text; + int32_t text; Vector child; }; diff --git a/server/TracyView.cpp b/server/TracyView.cpp index 39020c7e..19286c23 100644 --- a/server/TracyView.cpp +++ b/server/TracyView.cpp @@ -238,6 +238,18 @@ View::View( FileRead& f ) msgMap.emplace( ptr, msgdata ); } + f.Read( &sz, sizeof( sz ) ); + m_textData.reserve( sz ); + for( uint64_t i=0; i(); + uint64_t ptr; + f.Read( &ptr, sizeof( ptr ) ); + td->userText = ptr == 0 ? nullptr : stringMap.find( ptr )->second; + f.Read( &td->zoneName, sizeof( td->zoneName ) ); + m_textData.push_back( td ); + } + f.Read( &sz, sizeof( sz ) ); m_threads.reserve( sz ); for( uint64_t i=0; isrcloc = ev.srcloc; assert( ev.cpu == 0xFFFFFFFF || ev.cpu <= std::numeric_limits::max() ); zone->cpu_start = ev.cpu == 0xFFFFFFFF ? -1 : (int8_t)ev.cpu; - zone->text = nullptr; + zone->text = -1; std::unique_lock lock( m_lock ); NewZone( zone, ev.thread ); @@ -2008,9 +2020,9 @@ int View::DrawZoneLevel( const Vector& vec, bool hover, double pxns, else { const char* zoneName; - if( ev.text && ev.text->zoneName ) + if( ev.text != -1 && GetTextData( ev )->zoneName ) { - zoneName = GetString( ev.text->zoneName ); + zoneName = GetString( GetTextData( ev )->zoneName ); } else { @@ -2018,10 +2030,11 @@ int View::DrawZoneLevel( const Vector& vec, bool hover, double pxns, } int dmul = 1; - if( ev.text ) + if( ev.text != -1 ) { - if( ev.text->zoneName ) dmul++; - if( ev.text->userText ) dmul++; + auto td = GetTextData( ev ); + if( td->zoneName ) dmul++; + if( td->userText ) dmul++; } bool migration = false; @@ -2678,17 +2691,17 @@ void View::DrawZoneInfoWindow() ImGui::Separator(); - if( ev.text && ev.text->zoneName ) + if( ev.text != -1 && GetTextData( ev )->zoneName ) { - ImGui::Text( "Zone name: %s", GetString( ev.text->zoneName ) ); + ImGui::Text( "Zone name: %s", GetString( GetTextData( ev )->zoneName ) ); dmul++; } auto& srcloc = GetSourceLocation( ev.srcloc ); ImGui::Text( "Function: %s", GetString( srcloc.function ) ); ImGui::Text( "Location: %s:%i", GetString( srcloc.file ), srcloc.line ); - if( ev.text && ev.text->userText ) + if( ev.text != -1 && GetTextData( ev )->userText ) { - ImGui::Text( "User text: %s", ev.text->userText ); + ImGui::Text( "User text: %s", GetTextData( ev )->userText ); dmul++; } @@ -2727,9 +2740,9 @@ void View::DrawZoneInfoWindow() for( int i=0; izoneName ) + if( cev.text != -1 && GetTextData( cev )->zoneName ) { - ImGui::Text( "%s", GetString( cev.text->zoneName ) ); + ImGui::Text( "%s", GetString( GetTextData( cev )->zoneName ) ); } else { @@ -2885,10 +2898,11 @@ void View::ZoomToZone( const ZoneEvent& ev ) void View::ZoneTooltip( const ZoneEvent& ev ) { int dmul = 1; - if( ev.text ) + if( ev.text != -1 ) { - if( ev.text->zoneName ) dmul++; - if( ev.text->userText ) dmul++; + auto td = GetTextData( ev ); + if( td->zoneName ) dmul++; + if( td->userText ) dmul++; } auto& srcloc = GetSourceLocation( ev.srcloc ); @@ -2898,9 +2912,9 @@ void View::ZoneTooltip( const ZoneEvent& ev ) const char* func; const char* zoneName; - if( ev.text && ev.text->zoneName ) + if( ev.text != -1 && GetTextData( ev )->zoneName ) { - zoneName = GetString( ev.text->zoneName ); + zoneName = GetString( GetTextData( ev )->zoneName ); func = GetString( srcloc.function ); } else @@ -2926,10 +2940,10 @@ void View::ZoneTooltip( const ZoneEvent& ev ) ImGui::Text( "CPU: %i -> %i", ev.cpu_start, ev.cpu_end ); } } - if( ev.text && ev.text->userText ) + if( ev.text != -1 && GetTextData( ev )->userText ) { ImGui::Text( "" ); - ImGui::TextColored( ImVec4( 0xCC / 255.f, 0xCC / 255.f, 0x22 / 255.f, 1.f ), "%s", ev.text->userText ); + ImGui::TextColored( ImVec4( 0xCC / 255.f, 0xCC / 255.f, 0x22 / 255.f, 1.f ), "%s", GetTextData( ev )->userText ); } ImGui::EndTooltip(); } @@ -2957,13 +2971,21 @@ const ZoneEvent* View::GetZoneParent( const ZoneEvent& zone ) const TextData* View::GetTextData( ZoneEvent& zone ) { - if( !zone.text ) + if( zone.text == -1 ) { - zone.text = m_slab.Alloc(); - zone.text->userText = nullptr; - zone.text->zoneName = 0; + auto td = m_slab.Alloc(); + td->userText = nullptr; + td->zoneName = 0; + zone.text = m_textData.size(); + m_textData.push_back( td ); } - return zone.text; + return m_textData[zone.text]; +} + +const TextData* View::GetTextData( const ZoneEvent& zone ) const +{ + assert( zone.text != -1 ); + return m_textData[zone.text]; } void View::Write( FileWrite& f ) @@ -3058,6 +3080,15 @@ void View::Write( FileWrite& f ) } } + sz = m_textData.size(); + f.Write( &sz, sizeof( sz ) ); + for( auto& v : m_textData ) + { + const auto ptr = (uint64_t)v->userText; + f.Write( &ptr, sizeof( ptr ) ); + f.Write( &v->zoneName, sizeof( v->zoneName ) ); + } + sz = m_threads.size(); f.Write( &sz, sizeof( sz ) ); for( auto& thread : m_threads ) @@ -3101,20 +3132,7 @@ void View::WriteTimeline( FileWrite& f, const Vector& vec ) f.Write( &v->srcloc, sizeof( v->srcloc ) ); f.Write( &v->cpu_start, sizeof( v->cpu_start ) ); f.Write( &v->cpu_end, sizeof( v->cpu_end ) ); - - if( v->text ) - { - uint8_t flag = 1; - f.Write( &flag, sizeof( flag ) ); - f.Write( &v->text->userText, sizeof( v->text->userText ) ); - f.Write( &v->text->zoneName, sizeof( v->text->zoneName ) ); - } - else - { - uint8_t flag = 0; - f.Write( &flag, sizeof( flag ) ); - } - + f.Write( &v->text, sizeof( v->text ) ); WriteTimeline( f, v->child ); } } @@ -3136,22 +3154,7 @@ void View::ReadTimeline( FileRead& f, Vector& vec, const std::unorde f.Read( &zone->srcloc, sizeof( zone->srcloc ) ); f.Read( &zone->cpu_start, sizeof( zone->cpu_start ) ); f.Read( &zone->cpu_end, sizeof( zone->cpu_end ) ); - - uint8_t flag; - f.Read( &flag, sizeof( flag ) ); - if( flag ) - { - zone->text = m_slab.Alloc(); - uint64_t ptr; - f.Read( &ptr, sizeof( ptr ) ); - zone->text->userText = ptr == 0 ? nullptr : stringMap.find( ptr )->second; - f.Read( &zone->text->zoneName, sizeof( zone->text->zoneName ) ); - } - else - { - zone->text = nullptr; - } - + f.Read( &zone->text, sizeof( zone->text ) ); ReadTimeline( f, zone->child, stringMap ); } } diff --git a/server/TracyView.hpp b/server/TracyView.hpp index dccd9588..64a722fa 100644 --- a/server/TracyView.hpp +++ b/server/TracyView.hpp @@ -201,6 +201,7 @@ private: const ZoneEvent* GetZoneParent( const ZoneEvent& zone ) const; TextData* GetTextData( ZoneEvent& zone ); + const TextData* GetTextData( const ZoneEvent& zone ) const; void Write( FileWrite& f ); void WriteTimeline( FileWrite& f, const Vector& vec ); @@ -221,6 +222,7 @@ private: Vector m_threads; Vector m_plots; Vector m_messages; + Vector m_textData; std::unordered_map m_strings; std::unordered_map m_threadNames; std::unordered_set m_customStrings; diff --git a/test/test.cpp b/test/test.cpp index d87439ed..f73e329a 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -10,6 +10,7 @@ void TestFunction() { std::this_thread::sleep_for( std::chrono::milliseconds( 1 ) ); ZoneScoped; + ZoneName( "Test function" ); std::this_thread::sleep_for( std::chrono::milliseconds( 1 ) ); } } @@ -111,6 +112,9 @@ void DepthTest() for(;;) { std::this_thread::sleep_for( std::chrono::milliseconds( 20 ) ); + ZoneScoped; + const auto txt = "Fibonacci (15)"; + ZoneText( txt, strlen( txt ) ); Fibonacci( 15 ); } }