Cache last source location entry.

227 ms -> 138 ms
This commit is contained in:
Bartosz Taudul 2024-09-08 17:01:50 +02:00
parent 0c0e4f554f
commit 8b8ff9363d
No known key found for this signature in database
GPG Key ID: B7FE2008B7575DF3

View File

@ -17,6 +17,9 @@ struct FlameGraphItem
static void BuildFlameGraph( const Worker& worker, Vector<FlameGraphItem>& data, const Vector<short_ptr<ZoneEvent>>& zones ) static void BuildFlameGraph( const Worker& worker, Vector<FlameGraphItem>& data, const Vector<short_ptr<ZoneEvent>>& zones )
{ {
FlameGraphItem* it;
int16_t last = 0;
if( zones.is_magic() ) if( zones.is_magic() )
{ {
auto& vec = *(Vector<ZoneEvent>*)&zones; auto& vec = *(Vector<ZoneEvent>*)&zones;
@ -25,7 +28,18 @@ static void BuildFlameGraph( const Worker& worker, Vector<FlameGraphItem>& data,
if( !v.IsEndValid() ) break; if( !v.IsEndValid() ) break;
const auto srcloc = v.SrcLoc(); const auto srcloc = v.SrcLoc();
const auto duration = v.End() - v.Start(); 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( srcloc == last )
{
it->time += duration;
if( v.HasChildren() )
{
auto& children = worker.GetZoneChildren( v.Child() );
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() ) if( it == data.end() )
{ {
data.push_back( FlameGraphItem { srcloc, duration } ); data.push_back( FlameGraphItem { srcloc, duration } );
@ -34,6 +48,7 @@ static void BuildFlameGraph( const Worker& worker, Vector<FlameGraphItem>& data,
auto& children = worker.GetZoneChildren( v.Child() ); auto& children = worker.GetZoneChildren( v.Child() );
BuildFlameGraph( worker, data.back().children, children ); BuildFlameGraph( worker, data.back().children, children );
} }
it = &data.back();
} }
else else
{ {
@ -44,6 +59,8 @@ static void BuildFlameGraph( const Worker& worker, Vector<FlameGraphItem>& data,
BuildFlameGraph( worker, it->children, children ); BuildFlameGraph( worker, it->children, children );
} }
} }
last = srcloc;
}
} }
} }
else else
@ -53,7 +70,18 @@ static void BuildFlameGraph( const Worker& worker, Vector<FlameGraphItem>& data,
if( !v->IsEndValid() ) break; if( !v->IsEndValid() ) break;
const auto srcloc = v->SrcLoc(); const auto srcloc = v->SrcLoc();
const auto duration = v->End() - v->Start(); 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( srcloc == last )
{
it->time += duration;
if( v->HasChildren() )
{
auto& children = worker.GetZoneChildren( v->Child() );
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() ) if( it == data.end() )
{ {
data.push_back( FlameGraphItem { srcloc, duration } ); data.push_back( FlameGraphItem { srcloc, duration } );
@ -62,6 +90,7 @@ static void BuildFlameGraph( const Worker& worker, Vector<FlameGraphItem>& data,
auto& children = worker.GetZoneChildren( v->Child() ); auto& children = worker.GetZoneChildren( v->Child() );
BuildFlameGraph( worker, data.back().children, children ); BuildFlameGraph( worker, data.back().children, children );
} }
it = &data.back();
} }
else else
{ {
@ -72,6 +101,8 @@ static void BuildFlameGraph( const Worker& worker, Vector<FlameGraphItem>& data,
BuildFlameGraph( worker, it->children, children ); BuildFlameGraph( worker, it->children, children );
} }
} }
last = srcloc;
}
} }
} }
} }