mirror of
https://github.com/zeek/zeek.git
synced 2025-10-08 17:48:21 +00:00
Speedup ReachingDefs logic by ~15%
Mainly from the potential of ReachingDefs::AddRD() performing a redundant map lookup that it had just done.
This commit is contained in:
parent
2c04054975
commit
d7dfb16909
2 changed files with 32 additions and 22 deletions
|
@ -21,25 +21,21 @@ ReachingDefs::ReachingDefs(RDPtr rd)
|
||||||
|
|
||||||
void ReachingDefs::AddRD(const DefinitionItem* di, const DefinitionPoint& dp)
|
void ReachingDefs::AddRD(const DefinitionItem* di, const DefinitionPoint& dp)
|
||||||
{
|
{
|
||||||
if ( HasPair(di, dp) )
|
auto points = FindItem(di);
|
||||||
|
|
||||||
|
if ( points && HasPoint(dp, *points) )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
CopyMapIfNeeded();
|
if ( ! my_rd_map )
|
||||||
|
|
||||||
auto curr_defs = my_rd_map->find(di);
|
|
||||||
|
|
||||||
if ( curr_defs == my_rd_map->end() )
|
|
||||||
{
|
{
|
||||||
auto dps = std::make_unique<List<DefinitionPoint>>();
|
CopyMap();
|
||||||
dps->push_back(dp);
|
points = FindItem(di);
|
||||||
(*my_rd_map)[di] = std::move(dps);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( points )
|
||||||
|
points->push_back(dp);
|
||||||
else
|
else
|
||||||
{
|
my_rd_map->emplace(di, std::make_unique<DefPoints>(&dp, 1));
|
||||||
auto& dps = curr_defs->second;
|
|
||||||
dps->push_back(dp);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ReachingDefs::AddOrFullyReplace(const DefinitionItem* di,
|
void ReachingDefs::AddOrFullyReplace(const DefinitionItem* di,
|
||||||
|
@ -121,14 +117,25 @@ RDPtr ReachingDefs::IntersectWithConsolidation(const RDPtr& r,
|
||||||
|
|
||||||
bool ReachingDefs::HasPair(const DefinitionItem* di, const DefinitionPoint& dp)
|
bool ReachingDefs::HasPair(const DefinitionItem* di, const DefinitionPoint& dp)
|
||||||
const
|
const
|
||||||
|
{
|
||||||
|
auto points = FindItem(di);
|
||||||
|
return points && HasPoint(dp, *points);
|
||||||
|
}
|
||||||
|
|
||||||
|
DefPoints* ReachingDefs::FindItem(const DefinitionItem* di) const
|
||||||
{
|
{
|
||||||
const auto& map = RDMap();
|
const auto& map = RDMap();
|
||||||
|
auto it = map->find(di);
|
||||||
|
|
||||||
auto l = map->find(di);
|
if ( it == map->end() )
|
||||||
if ( l == map->end() )
|
return nullptr;
|
||||||
return false;
|
|
||||||
|
|
||||||
for ( const auto& l_dp : *l->second )
|
return it->second.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ReachingDefs::HasPoint(const DefinitionPoint& dp, const DefPoints& dps) const
|
||||||
|
{
|
||||||
|
for ( const auto& l_dp : dps )
|
||||||
if ( l_dp.SameAs(dp) )
|
if ( l_dp.SameAs(dp) )
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
@ -142,11 +149,8 @@ void ReachingDefs::AddRDs(const std::shared_ptr<ReachingDefsMap>& rd_m)
|
||||||
AddRD(one_rd.first, dp);
|
AddRD(one_rd.first, dp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ReachingDefs::CopyMapIfNeeded()
|
void ReachingDefs::CopyMap()
|
||||||
{
|
{
|
||||||
if ( my_rd_map )
|
|
||||||
return;
|
|
||||||
|
|
||||||
my_rd_map = std::make_shared<ReachingDefsMap>();
|
my_rd_map = std::make_shared<ReachingDefsMap>();
|
||||||
auto old_const_rd_map = std::move(const_rd_map);
|
auto old_const_rd_map = std::move(const_rd_map);
|
||||||
const_rd_map = nullptr;
|
const_rd_map = nullptr;
|
||||||
|
|
|
@ -126,6 +126,10 @@ protected:
|
||||||
// the given definition point.
|
// the given definition point.
|
||||||
bool HasPair(const DefinitionItem* di, const DefinitionPoint& dp) const;
|
bool HasPair(const DefinitionItem* di, const DefinitionPoint& dp) const;
|
||||||
|
|
||||||
|
DefPoints* FindItem(const DefinitionItem* di) const;
|
||||||
|
|
||||||
|
bool HasPoint(const DefinitionPoint&, const DefPoints& dps) const;
|
||||||
|
|
||||||
// Adds in the given RDs if we don't already have them.
|
// Adds in the given RDs if we don't already have them.
|
||||||
void AddRDs(const std::shared_ptr<ReachingDefsMap>& rd_m);
|
void AddRDs(const std::shared_ptr<ReachingDefsMap>& rd_m);
|
||||||
|
|
||||||
|
@ -134,7 +138,9 @@ protected:
|
||||||
|
|
||||||
// If we don't already have our own map, copy the one we're using
|
// If we don't already have our own map, copy the one we're using
|
||||||
// so that we then do.
|
// so that we then do.
|
||||||
void CopyMapIfNeeded();
|
void CopyMapIfNeeded()
|
||||||
|
{ if ( ! my_rd_map ) CopyMap(); }
|
||||||
|
void CopyMap();
|
||||||
|
|
||||||
void PrintRD(const DefinitionItem* di, const DefPoints* dp) const;
|
void PrintRD(const DefinitionItem* di, const DefPoints* dp) const;
|
||||||
void PrintRD(const DefinitionItem* di, const DefinitionPoint& dp) const;
|
void PrintRD(const DefinitionItem* di, const DefinitionPoint& dp) const;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue