From 8b8ff9363d7b8fd1481dc5c0dc90a571e4af759d Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Sun, 8 Sep 2024 17:01:50 +0200 Subject: [PATCH] Cache last source location entry. 227 ms -> 138 ms --- .../src/profiler/TracyView_FlameGraph.cpp | 75 +++++++++++++------ 1 file changed, 53 insertions(+), 22 deletions(-) diff --git a/profiler/src/profiler/TracyView_FlameGraph.cpp b/profiler/src/profiler/TracyView_FlameGraph.cpp index 4b0458a9..ab9685c3 100644 --- a/profiler/src/profiler/TracyView_FlameGraph.cpp +++ b/profiler/src/profiler/TracyView_FlameGraph.cpp @@ -17,6 +17,9 @@ struct FlameGraphItem static void BuildFlameGraph( const Worker& worker, Vector& data, const Vector>& zones ) { + FlameGraphItem* it; + int16_t last = 0; + if( zones.is_magic() ) { auto& vec = *(Vector*)&zones; @@ -25,17 +28,7 @@ static void BuildFlameGraph( const Worker& worker, Vector& data, if( !v.IsEndValid() ) break; const auto srcloc = v.SrcLoc(); const auto duration = v.End() - v.Start(); - auto it = std::find_if( data.begin(), data.end(), [srcloc]( const auto& v ) { return v.srcloc == srcloc; } ); - if( it == data.end() ) - { - data.push_back( FlameGraphItem { srcloc, duration } ); - if( v.HasChildren() ) - { - auto& children = worker.GetZoneChildren( v.Child() ); - BuildFlameGraph( worker, data.back().children, children ); - } - } - else + if( srcloc == last ) { it->time += duration; if( v.HasChildren() ) @@ -44,6 +37,30 @@ static void BuildFlameGraph( const Worker& worker, Vector& data, BuildFlameGraph( worker, it->children, children ); } } + else + { + it = std::find_if( data.begin(), data.end(), [srcloc]( const auto& v ) { return v.srcloc == srcloc; } ); + if( it == data.end() ) + { + data.push_back( FlameGraphItem { srcloc, duration } ); + if( v.HasChildren() ) + { + auto& children = worker.GetZoneChildren( v.Child() ); + BuildFlameGraph( worker, data.back().children, children ); + } + it = &data.back(); + } + else + { + it->time += duration; + if( v.HasChildren() ) + { + auto& children = worker.GetZoneChildren( v.Child() ); + BuildFlameGraph( worker, it->children, children ); + } + } + last = srcloc; + } } } else @@ -53,17 +70,7 @@ static void BuildFlameGraph( const Worker& worker, Vector& data, if( !v->IsEndValid() ) break; const auto srcloc = v->SrcLoc(); const auto duration = v->End() - v->Start(); - auto it = std::find_if( data.begin(), data.end(), [srcloc]( const auto& v ) { return v.srcloc == srcloc; } ); - if( it == data.end() ) - { - data.push_back( FlameGraphItem { srcloc, duration } ); - if( v->HasChildren() ) - { - auto& children = worker.GetZoneChildren( v->Child() ); - BuildFlameGraph( worker, data.back().children, children ); - } - } - else + if( srcloc == last ) { it->time += duration; if( v->HasChildren() ) @@ -72,6 +79,30 @@ static void BuildFlameGraph( const Worker& worker, Vector& data, BuildFlameGraph( worker, it->children, children ); } } + else + { + it = std::find_if( data.begin(), data.end(), [srcloc]( const auto& v ) { return v.srcloc == srcloc; } ); + if( it == data.end() ) + { + data.push_back( FlameGraphItem { srcloc, duration } ); + if( v->HasChildren() ) + { + auto& children = worker.GetZoneChildren( v->Child() ); + BuildFlameGraph( worker, data.back().children, children ); + } + it = &data.back(); + } + else + { + it->time += duration; + if( v->HasChildren() ) + { + auto& children = worker.GetZoneChildren( v->Child() ); + BuildFlameGraph( worker, it->children, children ); + } + } + last = srcloc; + } } } }