From ccd27a3dfa61a5fdb3dce8f53ef949dd55da1b76 Mon Sep 17 00:00:00 2001 From: Jon Siwek Date: Wed, 31 Mar 2021 14:48:02 -0700 Subject: [PATCH] Avoid redundant map/set searches in various ProfileFunc methods --- src/script_opt/ProfileFunc.cc | 48 +++++++++++++++++++++-------------- src/script_opt/ProfileFunc.h | 5 ++-- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/script_opt/ProfileFunc.cc b/src/script_opt/ProfileFunc.cc index 514a10447c..11077cd862 100644 --- a/src/script_opt/ProfileFunc.cc +++ b/src/script_opt/ProfileFunc.cc @@ -380,11 +380,12 @@ void ProfileFunc::TrackType(const Type* t) if ( ! t ) return; - if ( types.count(t) > 0 ) - // We've already tracke it. + auto [it, inserted] = types.insert(t); + + if ( ! inserted ) + // We've already tracked it. return; - types.insert(t); ordered_types.push_back(t); } @@ -393,11 +394,12 @@ void ProfileFunc::TrackID(const ID* id) if ( ! id ) return; - if ( ids.count(id) > 0 ) + auto [it, inserted] = ids.insert(id); + + if ( ! inserted ) // Already tracked. return; - ids.insert(id); ordered_ids.push_back(id); } @@ -546,22 +548,29 @@ p_hash_type ProfileFuncs::HashType(const Type* t) if ( ! t ) 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*. - return type_hashes[t]; + return it->second; 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 - // though with a different Type*. Reuse its results. - auto seen_t = seen_type_names[tn]; - auto h = type_hashes[seen_t]; + auto seen_it = seen_type_names.find(tn); - type_hashes[t] = h; - type_to_rep[t] = type_to_rep[seen_t]; + if ( seen_it != seen_type_names.end() ) + { + // 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()); @@ -676,16 +685,17 @@ p_hash_type ProfileFuncs::HashType(const Type* t) 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. - type_hash_reps[h] = t; type_to_rep[t] = t; rep_types.push_back(t); } 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; return h; diff --git a/src/script_opt/ProfileFunc.h b/src/script_opt/ProfileFunc.h index 1016145759..6cf6457796 100644 --- a/src/script_opt/ProfileFunc.h +++ b/src/script_opt/ProfileFunc.h @@ -316,8 +316,9 @@ public: // the parameter (which might be the parameter itself). const Type* TypeRep(const Type* orig) { - ASSERT(type_to_rep.count(orig) > 0); - return type_to_rep[orig]; + auto it = type_to_rep.find(orig); + ASSERT(it != type_to_rep.end()); + return it->second; } // Returns the hash associated with the given type, computing it