Avoid redundant map/set searches in various ProfileFunc methods

This commit is contained in:
Jon Siwek 2021-03-31 14:48:02 -07:00
parent ab1f1f8360
commit ccd27a3dfa
2 changed files with 32 additions and 21 deletions

View file

@ -380,11 +380,12 @@ void ProfileFunc::TrackType(const Type* t)
if ( ! t ) if ( ! t )
return; return;
if ( types.count(t) > 0 ) auto [it, inserted] = types.insert(t);
// We've already tracke it.
if ( ! inserted )
// We've already tracked it.
return; return;
types.insert(t);
ordered_types.push_back(t); ordered_types.push_back(t);
} }
@ -393,11 +394,12 @@ void ProfileFunc::TrackID(const ID* id)
if ( ! id ) if ( ! id )
return; return;
if ( ids.count(id) > 0 ) auto [it, inserted] = ids.insert(id);
if ( ! inserted )
// Already tracked. // Already tracked.
return; return;
ids.insert(id);
ordered_ids.push_back(id); ordered_ids.push_back(id);
} }
@ -546,22 +548,29 @@ p_hash_type ProfileFuncs::HashType(const Type* t)
if ( ! t ) if ( ! t )
return 0; return 0;
if ( type_hashes.count(t) > 0 ) auto it = type_hashes.find(t);
if ( it != type_hashes.end() )
// We've already done this Type*. // We've already done this Type*.
return type_hashes[t]; return it->second;
auto& tn = t->GetName(); auto& tn = t->GetName();
if ( tn.size() > 0 && seen_type_names.count(tn) > 0 ) if ( ! tn.empty() )
{ {
// We've already done a type with the same name, even auto seen_it = seen_type_names.find(tn);
// though with a different Type*. Reuse its results.
auto seen_t = seen_type_names[tn];
auto h = type_hashes[seen_t];
type_hashes[t] = h; if ( seen_it != seen_type_names.end() )
type_to_rep[t] = type_to_rep[seen_t]; {
// We've already done a type with the same name, even
// though with a different Type*. Reuse its results.
auto seen_t = seen_it->second;
auto h = type_hashes[seen_t];
return h; type_hashes[t] = h;
type_to_rep[t] = type_to_rep[seen_t];
return h;
}
} }
auto h = p_hash(t->Tag()); auto h = p_hash(t->Tag());
@ -676,16 +685,17 @@ p_hash_type ProfileFuncs::HashType(const Type* t)
type_hashes[t] = h; type_hashes[t] = h;
if ( type_hash_reps.count(h) == 0 ) auto [rep_it, rep_inserted] = type_hash_reps.emplace(h, t);
if ( rep_inserted )
{ // No previous rep, so use this Type* for that. { // No previous rep, so use this Type* for that.
type_hash_reps[h] = t;
type_to_rep[t] = t; type_to_rep[t] = t;
rep_types.push_back(t); rep_types.push_back(t);
} }
else else
type_to_rep[t] = type_hash_reps[h]; type_to_rep[t] = rep_it->second;
if ( tn.size() > 0 ) if ( ! tn.empty() )
seen_type_names[tn] = t; seen_type_names[tn] = t;
return h; return h;

View file

@ -316,8 +316,9 @@ public:
// the parameter (which might be the parameter itself). // the parameter (which might be the parameter itself).
const Type* TypeRep(const Type* orig) const Type* TypeRep(const Type* orig)
{ {
ASSERT(type_to_rep.count(orig) > 0); auto it = type_to_rep.find(orig);
return type_to_rep[orig]; ASSERT(it != type_to_rep.end());
return it->second;
} }
// Returns the hash associated with the given type, computing it // Returns the hash associated with the given type, computing it