use iterator-based idiom for check-if-present-then-access

This commit is contained in:
Vern Paxson 2021-11-22 15:28:15 -08:00
parent 735d584d9f
commit 84423369b4
12 changed files with 86 additions and 59 deletions

View file

@ -13,9 +13,10 @@ shared_ptr<CPP_InitInfo> CPPCompile::RegisterAttributes(const AttributesPtr& att
return nullptr; return nullptr;
auto a = attrs.get(); auto a = attrs.get();
auto pa = processed_attrs.find(a);
if ( processed_attrs.count(a) > 0 ) if ( pa != processed_attrs.end() )
return processed_attrs[a]; return pa->second;
attributes.AddKey(attrs); attributes.AddKey(attrs);
@ -41,9 +42,10 @@ shared_ptr<CPP_InitInfo> CPPCompile::RegisterAttributes(const AttributesPtr& att
shared_ptr<CPP_InitInfo> CPPCompile::RegisterAttr(const AttrPtr& attr) shared_ptr<CPP_InitInfo> CPPCompile::RegisterAttr(const AttrPtr& attr)
{ {
auto a = attr.get(); auto a = attr.get();
auto pa = processed_attr.find(a);
if ( processed_attr.count(a) > 0 ) if ( pa != processed_attr.end() )
return processed_attr[a]; return pa->second;
const auto& e = a->GetExpr(); const auto& e = a->GetExpr();
if ( e && ! IsSimpleInitExpr(e) ) if ( e && ! IsSimpleInitExpr(e) )

View file

@ -212,26 +212,30 @@ public:
// an offset into the global vector that will hold these. // an offset into the global vector that will hold these.
int TrackString(std::string s) int TrackString(std::string s)
{ {
if ( tracked_strings.count(s) == 0 ) auto ts = tracked_strings.find(s);
{ if ( ts != tracked_strings.end() )
tracked_strings[s] = ordered_tracked_strings.size(); return ts->second;
ordered_tracked_strings.emplace_back(s);
}
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 // Tracks a profile hash value needed for initialization. Returns
// an offset into the global vector that will hold these. // an offset into the global vector that will hold these.
int TrackHash(p_hash_type h) int TrackHash(p_hash_type h)
{ {
if ( tracked_hashes.count(h) == 0 ) auto th = tracked_hashes.find(h);
{ if ( th != tracked_hashes.end() )
tracked_hashes[h] = ordered_tracked_hashes.size(); return th->second;
ordered_tracked_hashes.emplace_back(h);
}
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. // Returns the hash associated with a given function body.

View file

@ -17,12 +17,13 @@ shared_ptr<CPP_InitInfo> CPPCompile::RegisterConstant(const ValPtr& vp, int& con
cv_indices.push_back(vp); cv_indices.push_back(vp);
auto v = vp.get(); 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. // Already did this one.
consts_offset = const_offsets[v]; consts_offset = const_offsets[v];
return const_vals[v]; return cv->second;
} }
// Formulate a key that's unique per distinct constant. // Formulate a key that's unique per distinct constant.
@ -50,11 +51,12 @@ shared_ptr<CPP_InitInfo> CPPCompile::RegisterConstant(const ValPtr& vp, int& con
c_desc = d.Description(); 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]; consts_offset = const_offsets[v] = constants_offsets[c_desc];
return const_vals[v]; return c->second;
} }
auto tag = t->Tag(); auto tag = t->Tag();

View file

@ -258,8 +258,9 @@ void CPPCompile::RegisterCompiledBody(const string& f)
// Build up an initializer of the events relevant to the function. // Build up an initializer of the events relevant to the function.
string events; string events;
if ( body_events.count(f) > 0 ) auto be = body_events.find(f);
for ( const auto& e : body_events[f] ) if ( be != body_events.end() )
for ( const auto& e : be->second )
{ {
if ( events.size() > 0 ) if ( events.size() > 0 )
events += ", "; events += ", ";
@ -278,8 +279,9 @@ void CPPCompile::RegisterCompiledBody(const string& f)
// same binary). // same binary).
h = merge_p_hashes(h, p_hash(cf_locs[f])); h = merge_p_hashes(h, p_hash(cf_locs[f]));
ASSERT(func_index.count(f) > 0); auto fi = func_index.find(f);
auto type_signature = casting_index[func_index[f]]; ASSERT(fi != func_index.end());
auto type_signature = casting_index[fi->second];
Emit("\tCPP_RegisterBody(\"%s\", (void*) %s, %s, %s, %s, std::vector<std::string>(%s)),", f, f, Emit("\tCPP_RegisterBody(\"%s\", (void*) %s, %s, %s, %s, std::vector<std::string>(%s)),", f, f,
Fmt(type_signature), Fmt(p), Fmt(h), events); Fmt(type_signature), Fmt(p), Fmt(h), events);
} }

View file

@ -1173,23 +1173,25 @@ string CPPCompile::GenField(const ExprPtr& rec, int field)
// Need to dynamically map the field. // Need to dynamically map the field.
int mapping_slot; 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. // We're already tracking this field.
mapping_slot = record_field_mappings[rt][field]; mapping_slot = rfm->second[field];
else else
{ {
// New mapping. // New mapping.
mapping_slot = num_rf_mappings++; mapping_slot = num_rf_mappings++;
ASSERT(processed_types.count(rt) > 0); auto pt = processed_types.find(rt);
auto rt_offset = processed_types[rt]->Offset(); ASSERT(pt != processed_types.end());
auto rt_offset = pt->second->Offset();
string field_name = rt->FieldName(field); string field_name = rt->FieldName(field);
field_decls.emplace_back(pair(rt_offset, rt->FieldDecl(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. // We're already tracking this record.
record_field_mappings[rt][field] = mapping_slot; rfm->second[field] = mapping_slot;
else else
{ {
// Need to start tracking this record. // 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. // Need to dynamically map the access.
int mapping_slot; 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. // We're already tracking this value.
mapping_slot = enum_val_mappings[et][v]; mapping_slot = evm->second[v];
else else
{ {
@ -1226,10 +1229,10 @@ string CPPCompile::GenEnum(const TypePtr& t, const ValPtr& ev)
string enum_name = et->Lookup(v); string enum_name = et->Lookup(v);
enum_names.emplace_back(pair(TypeOffset(t), move(enum_name))); 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. // We're already tracking this enum.
enum_val_mappings[et][v] = mapping_slot; evm->second[v] = mapping_slot;
} }
else else
{ {

View file

@ -233,12 +233,14 @@ string CPPCompile::BodyName(const FuncInfo& func)
p_hash_type CPPCompile::BodyHash(const Stmt* body) 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]; auto& body_name = bn->second;
ASSERT(body_hashes.count(body_name) > 0); 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) string CPPCompile::GenArgs(const RecordTypePtr& params, const Expr* e)

View file

@ -18,8 +18,9 @@ std::shared_ptr<CPP_InitInfo> CPPCompile::RegisterInitExpr(const ExprPtr& ep)
{ {
auto ename = InitExprName(ep); auto ename = InitExprName(ep);
if ( init_infos.count(ename) ) auto ii = init_infos.find(ename);
return init_infos[ename]; if ( ii != init_infos.end() )
return ii->second;
auto wrapper_cl = string("wrapper_") + ename + "_cl"; auto wrapper_cl = string("wrapper_") + ename + "_cl";
@ -247,8 +248,9 @@ void CPPCompile::GenStandaloneActivation()
// We didn't wind up compiling it. // We didn't wind up compiling it.
continue; continue;
ASSERT(body_hashes.count(bname) > 0); auto bh = body_hashes.find(bname);
func_bodies[f].push_back(body_hashes[bname]); ASSERT(bh != body_hashes.end());
func_bodies[f].push_back(bh->second);
} }
for ( auto& fb : func_bodies ) for ( auto& fb : func_bodies )

View file

@ -306,10 +306,13 @@ AttrInfo::AttrInfo(CPPCompile* _c, const AttrPtr& attr) : CompoundItemInfo(_c)
AttrsInfo::AttrsInfo(CPPCompile* _c, const AttributesPtr& _attrs) : CompoundItemInfo(_c) AttrsInfo::AttrsInfo(CPPCompile* _c, const AttributesPtr& _attrs) : CompoundItemInfo(_c)
{ {
const auto& pas = c->ProcessedAttr();
for ( const auto& a : _attrs->GetAttrs() ) for ( const auto& a : _attrs->GetAttrs() )
{ {
ASSERT(c->ProcessedAttr().count(a.get()) > 0); auto pa = pas.find(a.get());
const auto& gi = c->ProcessedAttr().at(a.get()); ASSERT(pa != pas.end());
const auto& gi = pa->second;
init_cohort = max(init_cohort, gi->InitCohort() + 1); init_cohort = max(init_cohort, gi->InitCohort() + 1);
vals.emplace_back(Fmt(gi->Offset())); vals.emplace_back(Fmt(gi->Offset()));
} }

View file

@ -154,8 +154,9 @@ void activate_bodies__CPP(const char* fn, const char* module, bool exported, Typ
continue; continue;
// Add in the new body. // Add in the new body.
ASSERT(compiled_scripts.count(h) > 0); auto csi = compiled_scripts.find(h);
auto cs = compiled_scripts[h]; ASSERT(csi != compiled_scripts.end());
auto cs = csi->second;
f->AddBody(cs.body, no_inits, num_params, cs.priority); f->AddBody(cs.body, no_inits, num_params, cs.priority);
added_bodies[fn].insert(h); added_bodies[fn].insert(h);
@ -220,9 +221,10 @@ FuncValPtr lookup_func__CPP(string name, vector<p_hash_type> hashes, const TypeP
for ( auto h : hashes ) 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); bodies.push_back(f.body);
priorities.push_back(f.priority); priorities.push_back(f.priority);

View file

@ -52,8 +52,9 @@ template <class T> string CPPTracker<T>::KeyName(const T* key)
ASSERT(hash != 0); ASSERT(hash != 0);
auto rep = reps[hash]; auto rep = reps[hash];
if ( gi_s.count(rep) > 0 ) auto gi = gi_s.find(rep);
return gi_s[rep]->Name(); if ( gi != gi_s.end() )
return gi->second->Name();
auto index = map2[hash]; auto index = map2[hash];

View file

@ -280,8 +280,9 @@ shared_ptr<CPP_InitInfo> CPPCompile::RegisterType(const TypePtr& tp)
{ {
auto t = TypeRep(tp); auto t = TypeRep(tp);
if ( processed_types.count(t) > 0 ) auto pt = processed_types.find(t);
return processed_types[t]; if ( pt != processed_types.end() )
return pt->second;
processed_types[t] = nullptr; processed_types[t] = nullptr;

View file

@ -120,7 +120,9 @@ void CPPCompile::CreateGlobal(const ID* g)
std::shared_ptr<CPP_InitInfo> CPPCompile::RegisterGlobal(const ID* g) std::shared_ptr<CPP_InitInfo> 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()); auto gn = string(g->Name());
@ -131,9 +133,10 @@ std::shared_ptr<CPP_InitInfo> CPPCompile::RegisterGlobal(const ID* g)
auto gi = make_shared<GlobalInitInfo>(this, g, globals[gn]); auto gi = make_shared<GlobalInitInfo>(this, g, globals[gn]);
global_id_info->AddInstance(gi); global_id_info->AddInstance(gi);
global_gis[g] = gi; global_gis[g] = gi;
return gi;
} }
else
return global_gis[g]; return gg->second;
} }
void CPPCompile::AddBiF(const ID* b, bool is_var) void CPPCompile::AddBiF(const ID* b, bool is_var)
@ -184,9 +187,9 @@ const string& CPPCompile::IDNameStr(const ID* id)
return globals[g]; return globals[g];
} }
ASSERT(locals.count(id) > 0); auto l = locals.find(id);
ASSERT(l != locals.end());
return locals[id]; return l->second;
} }
string CPPCompile::LocalName(const ID* l) const string CPPCompile::LocalName(const ID* l) const