Skip to content

Commit

Permalink
Do a linear search instead of using a map.
Browse files Browse the repository at this point in the history
513 ms -> 227 ms for a trace with 66 unique source locations and 179 million
zones.
  • Loading branch information
wolfpld committed Sep 8, 2024
1 parent d67598f commit 0c0e4f5
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions profiler/src/profiler/TracyView_FlameGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "TracyPrint.hpp"
#include "TracyVector.hpp"
#include "TracyView.hpp"
#include "tracy_robin_hood.h"

namespace tracy
{
Expand All @@ -18,9 +17,6 @@ struct FlameGraphItem

static void BuildFlameGraph( const Worker& worker, Vector<FlameGraphItem>& data, const Vector<short_ptr<ZoneEvent>>& zones )
{
unordered_flat_map<int16_t, uint16_t> map;
for( size_t i=0; i<data.size(); i++ ) map.emplace( data[i].srcloc, i );

if( zones.is_magic() )
{
auto& vec = *(Vector<ZoneEvent>*)&zones;
Expand All @@ -29,10 +25,9 @@ static void BuildFlameGraph( const Worker& worker, Vector<FlameGraphItem>& data,
if( !v.IsEndValid() ) break;
const auto srcloc = v.SrcLoc();
const auto duration = v.End() - v.Start();
auto it = map.find( srcloc );
if( it == map.end() )
auto it = std::find_if( data.begin(), data.end(), [srcloc]( const auto& v ) { return v.srcloc == srcloc; } );
if( it == data.end() )
{
map.emplace( srcloc, data.size() );
data.push_back( FlameGraphItem { srcloc, duration } );
if( v.HasChildren() )
{
Expand All @@ -42,12 +37,11 @@ static void BuildFlameGraph( const Worker& worker, Vector<FlameGraphItem>& data,
}
else
{
auto& item = data[it->second];
item.time += duration;
it->time += duration;
if( v.HasChildren() )
{
auto& children = worker.GetZoneChildren( v.Child() );
BuildFlameGraph( worker, item.children, children );
BuildFlameGraph( worker, it->children, children );
}
}
}
Expand All @@ -59,10 +53,9 @@ static void BuildFlameGraph( const Worker& worker, Vector<FlameGraphItem>& data,
if( !v->IsEndValid() ) break;
const auto srcloc = v->SrcLoc();
const auto duration = v->End() - v->Start();
auto it = map.find( srcloc );
if( it == map.end() )
auto it = std::find_if( data.begin(), data.end(), [srcloc]( const auto& v ) { return v.srcloc == srcloc; } );
if( it == data.end() )
{
map.emplace( srcloc, data.size() );
data.push_back( FlameGraphItem { srcloc, duration } );
if( v->HasChildren() )
{
Expand All @@ -72,12 +65,11 @@ static void BuildFlameGraph( const Worker& worker, Vector<FlameGraphItem>& data,
}
else
{
auto& item = data[it->second];
item.time += duration;
it->time += duration;
if( v->HasChildren() )
{
auto& children = worker.GetZoneChildren( v->Child() );
BuildFlameGraph( worker, item.children, children );
BuildFlameGraph( worker, it->children, children );
}
}
}
Expand Down

0 comments on commit 0c0e4f5

Please sign in to comment.