From 84423369b4f8eb4134ef7cea6338e52dca6c578e Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Mon, 22 Nov 2021 15:28:15 -0800 Subject: [PATCH] use iterator-based idiom for check-if-present-then-access --- src/script_opt/CPP/Attrs.cc | 10 +++++---- src/script_opt/CPP/Compile.h | 28 ++++++++++++++---------- src/script_opt/CPP/Consts.cc | 12 +++++----- src/script_opt/CPP/Driver.cc | 10 +++++---- src/script_opt/CPP/Exprs.cc | 23 ++++++++++--------- src/script_opt/CPP/GenFunc.cc | 10 +++++---- src/script_opt/CPP/Inits.cc | 10 +++++---- src/script_opt/CPP/InitsInfo.cc | 7 ++++-- src/script_opt/CPP/RuntimeInitSupport.cc | 10 +++++---- src/script_opt/CPP/Tracker.cc | 5 +++-- src/script_opt/CPP/Types.cc | 5 +++-- src/script_opt/CPP/Vars.cc | 15 ++++++++----- 12 files changed, 86 insertions(+), 59 deletions(-) diff --git a/src/script_opt/CPP/Attrs.cc b/src/script_opt/CPP/Attrs.cc index 6c2594be2d..437270a7d4 100644 --- a/src/script_opt/CPP/Attrs.cc +++ b/src/script_opt/CPP/Attrs.cc @@ -13,9 +13,10 @@ shared_ptr CPPCompile::RegisterAttributes(const AttributesPtr& att return nullptr; auto a = attrs.get(); + auto pa = processed_attrs.find(a); - if ( processed_attrs.count(a) > 0 ) - return processed_attrs[a]; + if ( pa != processed_attrs.end() ) + return pa->second; attributes.AddKey(attrs); @@ -41,9 +42,10 @@ shared_ptr CPPCompile::RegisterAttributes(const AttributesPtr& att shared_ptr CPPCompile::RegisterAttr(const AttrPtr& attr) { auto a = attr.get(); + auto pa = processed_attr.find(a); - if ( processed_attr.count(a) > 0 ) - return processed_attr[a]; + if ( pa != processed_attr.end() ) + return pa->second; const auto& e = a->GetExpr(); if ( e && ! IsSimpleInitExpr(e) ) diff --git a/src/script_opt/CPP/Compile.h b/src/script_opt/CPP/Compile.h index 033ed4fa67..dbac78469b 100644 --- a/src/script_opt/CPP/Compile.h +++ b/src/script_opt/CPP/Compile.h @@ -212,26 +212,30 @@ public: // an offset into the global vector that will hold these. int TrackString(std::string s) { - if ( tracked_strings.count(s) == 0 ) - { - tracked_strings[s] = ordered_tracked_strings.size(); - ordered_tracked_strings.emplace_back(s); - } + auto ts = tracked_strings.find(s); + if ( ts != tracked_strings.end() ) + return ts->second; - return tracked_strings[s]; + int offset = ordered_tracked_strings.size(); + tracked_strings[s] = offset; + ordered_tracked_strings.emplace_back(s); + + return offset; } // Tracks a profile hash value needed for initialization. Returns // an offset into the global vector that will hold these. int TrackHash(p_hash_type h) { - if ( tracked_hashes.count(h) == 0 ) - { - tracked_hashes[h] = ordered_tracked_hashes.size(); - ordered_tracked_hashes.emplace_back(h); - } + auto th = tracked_hashes.find(h); + if ( th != tracked_hashes.end() ) + return th->second; - return tracked_hashes[h]; + int offset = ordered_tracked_hashes.size(); + tracked_hashes[h] = offset; + ordered_tracked_hashes.emplace_back(h); + + return offset; } // Returns the hash associated with a given function body. diff --git a/src/script_opt/CPP/Consts.cc b/src/script_opt/CPP/Consts.cc index 30b758df99..c53f5b0395 100644 --- a/src/script_opt/CPP/Consts.cc +++ b/src/script_opt/CPP/Consts.cc @@ -17,12 +17,13 @@ shared_ptr CPPCompile::RegisterConstant(const ValPtr& vp, int& con cv_indices.push_back(vp); auto v = vp.get(); + auto cv = const_vals.find(v); - if ( const_vals.count(v) > 0 ) + if ( cv != const_vals.end() ) { // Already did this one. consts_offset = const_offsets[v]; - return const_vals[v]; + return cv->second; } // Formulate a key that's unique per distinct constant. @@ -50,11 +51,12 @@ shared_ptr CPPCompile::RegisterConstant(const ValPtr& vp, int& con c_desc = d.Description(); } - if ( constants.count(c_desc) > 0 ) + auto c = constants.find(c_desc); + if ( c != constants.end() ) { - const_vals[v] = constants[c_desc]; + const_vals[v] = c->second; consts_offset = const_offsets[v] = constants_offsets[c_desc]; - return const_vals[v]; + return c->second; } auto tag = t->Tag(); diff --git a/src/script_opt/CPP/Driver.cc b/src/script_opt/CPP/Driver.cc index 7e446a302f..dd7348ddca 100644 --- a/src/script_opt/CPP/Driver.cc +++ b/src/script_opt/CPP/Driver.cc @@ -258,8 +258,9 @@ void CPPCompile::RegisterCompiledBody(const string& f) // Build up an initializer of the events relevant to the function. string events; - if ( body_events.count(f) > 0 ) - for ( const auto& e : body_events[f] ) + auto be = body_events.find(f); + if ( be != body_events.end() ) + for ( const auto& e : be->second ) { if ( events.size() > 0 ) events += ", "; @@ -278,8 +279,9 @@ void CPPCompile::RegisterCompiledBody(const string& f) // same binary). h = merge_p_hashes(h, p_hash(cf_locs[f])); - ASSERT(func_index.count(f) > 0); - auto type_signature = casting_index[func_index[f]]; + auto fi = func_index.find(f); + ASSERT(fi != func_index.end()); + auto type_signature = casting_index[fi->second]; Emit("\tCPP_RegisterBody(\"%s\", (void*) %s, %s, %s, %s, std::vector(%s)),", f, f, Fmt(type_signature), Fmt(p), Fmt(h), events); } diff --git a/src/script_opt/CPP/Exprs.cc b/src/script_opt/CPP/Exprs.cc index 3caf7805f3..dd3baa995c 100644 --- a/src/script_opt/CPP/Exprs.cc +++ b/src/script_opt/CPP/Exprs.cc @@ -1173,23 +1173,25 @@ string CPPCompile::GenField(const ExprPtr& rec, int field) // Need to dynamically map the field. int mapping_slot; - if ( record_field_mappings.count(rt) > 0 && record_field_mappings[rt].count(field) > 0 ) + auto rfm = record_field_mappings.find(rt); + if ( rfm != record_field_mappings.end() && rfm->second.count(field) > 0 ) // We're already tracking this field. - mapping_slot = record_field_mappings[rt][field]; + mapping_slot = rfm->second[field]; else { // New mapping. mapping_slot = num_rf_mappings++; - ASSERT(processed_types.count(rt) > 0); - auto rt_offset = processed_types[rt]->Offset(); + auto pt = processed_types.find(rt); + ASSERT(pt != processed_types.end()); + auto rt_offset = pt->second->Offset(); string field_name = rt->FieldName(field); field_decls.emplace_back(pair(rt_offset, rt->FieldDecl(field))); - if ( record_field_mappings.count(rt) > 0 ) + if ( rfm != record_field_mappings.end() ) // We're already tracking this record. - record_field_mappings[rt][field] = mapping_slot; + rfm->second[field] = mapping_slot; else { // Need to start tracking this record. @@ -1214,9 +1216,10 @@ string CPPCompile::GenEnum(const TypePtr& t, const ValPtr& ev) // Need to dynamically map the access. int mapping_slot; - if ( enum_val_mappings.count(et) > 0 && enum_val_mappings[et].count(v) > 0 ) + auto evm = enum_val_mappings.find(et); + if ( evm != enum_val_mappings.end() && evm->second.count(v) > 0 ) // We're already tracking this value. - mapping_slot = enum_val_mappings[et][v]; + mapping_slot = evm->second[v]; else { @@ -1226,10 +1229,10 @@ string CPPCompile::GenEnum(const TypePtr& t, const ValPtr& ev) string enum_name = et->Lookup(v); enum_names.emplace_back(pair(TypeOffset(t), move(enum_name))); - if ( enum_val_mappings.count(et) > 0 ) + if ( evm != enum_val_mappings.end() ) { // We're already tracking this enum. - enum_val_mappings[et][v] = mapping_slot; + evm->second[v] = mapping_slot; } else { diff --git a/src/script_opt/CPP/GenFunc.cc b/src/script_opt/CPP/GenFunc.cc index 4cd57e7c7d..47a9ec33bc 100644 --- a/src/script_opt/CPP/GenFunc.cc +++ b/src/script_opt/CPP/GenFunc.cc @@ -233,12 +233,14 @@ string CPPCompile::BodyName(const FuncInfo& func) p_hash_type CPPCompile::BodyHash(const Stmt* body) { - ASSERT(body_names.count(body) > 0); + auto bn = body_names.find(body); + ASSERT(bn != body_names.end()); - auto& body_name = body_names[body]; - ASSERT(body_hashes.count(body_name) > 0); + auto& body_name = bn->second; + auto bh = body_hashes.find(body_name); + ASSERT(bh != body_hashes.end()); - return body_hashes[body_name]; + return bh->second; } string CPPCompile::GenArgs(const RecordTypePtr& params, const Expr* e) diff --git a/src/script_opt/CPP/Inits.cc b/src/script_opt/CPP/Inits.cc index 61b1c18ca7..2e51b95412 100644 --- a/src/script_opt/CPP/Inits.cc +++ b/src/script_opt/CPP/Inits.cc @@ -18,8 +18,9 @@ std::shared_ptr CPPCompile::RegisterInitExpr(const ExprPtr& ep) { auto ename = InitExprName(ep); - if ( init_infos.count(ename) ) - return init_infos[ename]; + auto ii = init_infos.find(ename); + if ( ii != init_infos.end() ) + return ii->second; auto wrapper_cl = string("wrapper_") + ename + "_cl"; @@ -247,8 +248,9 @@ void CPPCompile::GenStandaloneActivation() // We didn't wind up compiling it. continue; - ASSERT(body_hashes.count(bname) > 0); - func_bodies[f].push_back(body_hashes[bname]); + auto bh = body_hashes.find(bname); + ASSERT(bh != body_hashes.end()); + func_bodies[f].push_back(bh->second); } for ( auto& fb : func_bodies ) diff --git a/src/script_opt/CPP/InitsInfo.cc b/src/script_opt/CPP/InitsInfo.cc index f02f7f04b2..c83b78f5ed 100644 --- a/src/script_opt/CPP/InitsInfo.cc +++ b/src/script_opt/CPP/InitsInfo.cc @@ -306,10 +306,13 @@ AttrInfo::AttrInfo(CPPCompile* _c, const AttrPtr& attr) : CompoundItemInfo(_c) AttrsInfo::AttrsInfo(CPPCompile* _c, const AttributesPtr& _attrs) : CompoundItemInfo(_c) { + const auto& pas = c->ProcessedAttr(); + for ( const auto& a : _attrs->GetAttrs() ) { - ASSERT(c->ProcessedAttr().count(a.get()) > 0); - const auto& gi = c->ProcessedAttr().at(a.get()); + auto pa = pas.find(a.get()); + ASSERT(pa != pas.end()); + const auto& gi = pa->second; init_cohort = max(init_cohort, gi->InitCohort() + 1); vals.emplace_back(Fmt(gi->Offset())); } diff --git a/src/script_opt/CPP/RuntimeInitSupport.cc b/src/script_opt/CPP/RuntimeInitSupport.cc index 87a7fb484e..6a657d0713 100644 --- a/src/script_opt/CPP/RuntimeInitSupport.cc +++ b/src/script_opt/CPP/RuntimeInitSupport.cc @@ -154,8 +154,9 @@ void activate_bodies__CPP(const char* fn, const char* module, bool exported, Typ continue; // Add in the new body. - ASSERT(compiled_scripts.count(h) > 0); - auto cs = compiled_scripts[h]; + auto csi = compiled_scripts.find(h); + ASSERT(csi != compiled_scripts.end()); + auto cs = csi->second; f->AddBody(cs.body, no_inits, num_params, cs.priority); added_bodies[fn].insert(h); @@ -220,9 +221,10 @@ FuncValPtr lookup_func__CPP(string name, vector hashes, const TypeP for ( auto h : hashes ) { - ASSERT(compiled_scripts.count(h) > 0); + auto cs = compiled_scripts.find(h); + ASSERT(cs != compiled_scripts.end()); - const auto& f = compiled_scripts[h]; + const auto& f = cs->second; bodies.push_back(f.body); priorities.push_back(f.priority); diff --git a/src/script_opt/CPP/Tracker.cc b/src/script_opt/CPP/Tracker.cc index 8eeb616e5e..d4491274bd 100644 --- a/src/script_opt/CPP/Tracker.cc +++ b/src/script_opt/CPP/Tracker.cc @@ -52,8 +52,9 @@ template string CPPTracker::KeyName(const T* key) ASSERT(hash != 0); auto rep = reps[hash]; - if ( gi_s.count(rep) > 0 ) - return gi_s[rep]->Name(); + auto gi = gi_s.find(rep); + if ( gi != gi_s.end() ) + return gi->second->Name(); auto index = map2[hash]; diff --git a/src/script_opt/CPP/Types.cc b/src/script_opt/CPP/Types.cc index c55a2287f8..9a482d072e 100644 --- a/src/script_opt/CPP/Types.cc +++ b/src/script_opt/CPP/Types.cc @@ -280,8 +280,9 @@ shared_ptr CPPCompile::RegisterType(const TypePtr& tp) { auto t = TypeRep(tp); - if ( processed_types.count(t) > 0 ) - return processed_types[t]; + auto pt = processed_types.find(t); + if ( pt != processed_types.end() ) + return pt->second; processed_types[t] = nullptr; diff --git a/src/script_opt/CPP/Vars.cc b/src/script_opt/CPP/Vars.cc index 432dce39a9..c62aae0869 100644 --- a/src/script_opt/CPP/Vars.cc +++ b/src/script_opt/CPP/Vars.cc @@ -120,7 +120,9 @@ void CPPCompile::CreateGlobal(const ID* g) std::shared_ptr CPPCompile::RegisterGlobal(const ID* g) { - if ( global_gis.count(g) == 0 ) + auto gg = global_gis.find(g); + + if ( gg == global_gis.end() ) { auto gn = string(g->Name()); @@ -131,9 +133,10 @@ std::shared_ptr CPPCompile::RegisterGlobal(const ID* g) auto gi = make_shared(this, g, globals[gn]); global_id_info->AddInstance(gi); global_gis[g] = gi; + return gi; } - - return global_gis[g]; + else + return gg->second; } void CPPCompile::AddBiF(const ID* b, bool is_var) @@ -184,9 +187,9 @@ const string& CPPCompile::IDNameStr(const ID* id) return globals[g]; } - ASSERT(locals.count(id) > 0); - - return locals[id]; + auto l = locals.find(id); + ASSERT(l != locals.end()); + return l->second; } string CPPCompile::LocalName(const ID* l) const