Skip to content

Commit fb42722

Browse files
authored
Merge pull request #7615 from tautschnig/cleanup/no-redundant-lookup
Use get_writeable_symbol() to avoid redundant search
2 parents 7ac1554 + 4a93fcf commit fb42722

17 files changed

+103
-108
lines changed

jbmc/src/java_bytecode/java_bytecode_concurrency_instrumentation.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -605,9 +605,9 @@ void convert_synchronized_methods(
605605
message_handlert &message_handler)
606606
{
607607
namespacet ns(symbol_table);
608-
for(const auto &entry : symbol_table)
608+
for(auto s_it = symbol_table.begin(); s_it != symbol_table.end(); ++s_it)
609609
{
610-
const symbolt &symbol = entry.second;
610+
const symbolt &symbol = s_it->second;
611611

612612
if(symbol.type.id() != ID_code)
613613
continue;
@@ -619,11 +619,10 @@ void convert_synchronized_methods(
619619
if(symbol.type.get_bool(ID_is_static))
620620
{
621621
messaget message(message_handler);
622-
message.warning() << "Java method '" << entry.first
622+
message.warning() << "Java method '" << s_it->first
623623
<< "' is static and synchronized."
624624
<< " This is unsupported, the synchronized keyword"
625-
<< " will be ignored."
626-
<< messaget::eom;
625+
<< " will be ignored." << messaget::eom;
627626
continue;
628627
}
629628

@@ -638,7 +637,7 @@ void convert_synchronized_methods(
638637
continue;
639638

640639
// get writeable reference and instrument the method
641-
symbolt &w_symbol = symbol_table.get_writeable_ref(entry.first);
640+
symbolt &w_symbol = s_it.get_writeable_symbol();
642641
instrument_synchronized_code(
643642
symbol_table, w_symbol, it->second.symbol_expr());
644643
}

src/ansi-c/c_typecheck_type.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,13 +1422,12 @@ void c_typecheck_baset::typecheck_c_enum_type(typet &type)
14221422
enum_tag_symbol.type.add_subtype() = underlying_type;
14231423

14241424
// is it in the symbol table already?
1425-
symbol_table_baset::symbolst::const_iterator s_it =
1426-
symbol_table.symbols.find(identifier);
1425+
symbolt *existing_symbol = symbol_table.get_writeable(identifier);
14271426

1428-
if(s_it!=symbol_table.symbols.end())
1427+
if(existing_symbol)
14291428
{
14301429
// Yes.
1431-
const symbolt &symbol=s_it->second;
1430+
const symbolt &symbol = *existing_symbol;
14321431

14331432
if(symbol.type.id() != ID_c_enum)
14341433
{
@@ -1441,7 +1440,7 @@ void c_typecheck_baset::typecheck_c_enum_type(typet &type)
14411440
{
14421441
// Ok, overwrite the type in the symbol table.
14431442
// This gives us the members and the subtype.
1444-
symbol_table.get_writeable_ref(symbol.name).type=enum_tag_symbol.type;
1443+
existing_symbol->type = enum_tag_symbol.type;
14451444
}
14461445
else
14471446
{

src/cpp/cpp_typecheck.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ void cpp_typecheckt::static_and_dynamic_initialization()
158158

159159
for(const irep_idt &d_it : dynamic_initializations)
160160
{
161-
const symbolt &symbol = symbol_table.lookup_ref(d_it);
161+
symbolt &symbol = symbol_table.get_writeable_ref(d_it);
162162

163163
if(symbol.is_extern)
164164
continue;
@@ -182,7 +182,7 @@ void cpp_typecheckt::static_and_dynamic_initialization()
182182

183183
// Make it nil to get zero initialization by
184184
// __CPROVER_initialize
185-
symbol_table.get_writeable_ref(d_it).value.make_nil();
185+
symbol.value.make_nil();
186186
}
187187
else
188188
{
@@ -220,9 +220,9 @@ void cpp_typecheckt::do_not_typechecked()
220220
{
221221
cont = false;
222222

223-
for(const auto &named_symbol : symbol_table.symbols)
223+
for(auto it = symbol_table.begin(); it != symbol_table.end(); ++it)
224224
{
225-
const symbolt &symbol=named_symbol.second;
225+
const symbolt &symbol = it->second;
226226

227227
if(
228228
symbol.value.id() == ID_cpp_not_typechecked &&
@@ -248,30 +248,28 @@ void cpp_typecheckt::do_not_typechecked()
248248
else
249249
UNREACHABLE; // Don't know what to do!
250250

251-
symbolt &writable_symbol =
252-
symbol_table.get_writeable_ref(named_symbol.first);
251+
symbolt &writable_symbol = it.get_writeable_symbol();
253252
writable_symbol.value.swap(value);
254253
convert_function(writable_symbol);
255254
}
256255
}
257256
}
258257
while(cont);
259258

260-
for(const auto &named_symbol : symbol_table.symbols)
259+
for(auto it = symbol_table.begin(); it != symbol_table.end(); ++it)
261260
{
262-
if(named_symbol.second.value.id() == ID_cpp_not_typechecked)
263-
symbol_table.get_writeable_ref(named_symbol.first).value.make_nil();
261+
if(it->second.value.id() == ID_cpp_not_typechecked)
262+
it.get_writeable_symbol().value.make_nil();
264263
}
265264
}
266265

267266
void cpp_typecheckt::clean_up()
268267
{
269-
symbol_table_baset::symbolst::const_iterator it =
270-
symbol_table.symbols.begin();
268+
auto it = symbol_table.begin();
271269

272-
while(it!=symbol_table.symbols.end())
270+
while(it != symbol_table.end())
273271
{
274-
symbol_table_baset::symbolst::const_iterator cur_it = it;
272+
auto cur_it = it;
275273
it++;
276274

277275
const symbolt &symbol=cur_it->second;
@@ -288,8 +286,8 @@ void cpp_typecheckt::clean_up()
288286
symbol.type.id()==ID_union)
289287
{
290288
// remove methods from 'components'
291-
struct_union_typet &struct_union_type=to_struct_union_type(
292-
symbol_table.get_writeable_ref(cur_it->first).type);
289+
struct_union_typet &struct_union_type =
290+
to_struct_union_type(cur_it.get_writeable_symbol().type);
293291

294292
const struct_union_typet::componentst &components=
295293
struct_union_type.components();

src/cpp/cpp_typecheck_declaration.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ codet cpp_typecheckt::convert_anonymous_union(cpp_declarationt &declaration)
6060
new_code.add_to_operands(code_frontend_declt(cpp_symbol_expr(symbol)));
6161

6262
// do scoping
63-
symbolt union_symbol =
63+
symbolt &union_symbol =
6464
symbol_table.get_writeable_ref(follow(symbol.type).get(ID_name));
6565

6666
for(const auto &c : to_union_type(union_symbol.type).components())
@@ -89,8 +89,7 @@ codet cpp_typecheckt::convert_anonymous_union(cpp_declarationt &declaration)
8989
id.is_member=true;
9090
}
9191

92-
symbol_table.get_writeable_ref(union_symbol.name)
93-
.type.set(ID_C_unnamed_object, symbol.base_name);
92+
union_symbol.type.set(ID_C_unnamed_object, symbol.base_name);
9493

9594
return new_code;
9695
}

src/cpp/cpp_typecheck_expr.cpp

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,14 +1230,10 @@ void cpp_typecheckt::typecheck_expr_member(
12301230
if(expr.type().id()==ID_code)
12311231
{
12321232
// Check if the function body has to be typechecked
1233-
symbol_table_baset::symbolst::const_iterator it =
1234-
symbol_table.symbols.find(component_name);
1233+
symbolt &component_symbol = symbol_table.get_writeable_ref(component_name);
12351234

1236-
CHECK_RETURN(it != symbol_table.symbols.end());
1237-
1238-
if(it->second.value.id() == ID_cpp_not_typechecked)
1239-
symbol_table.get_writeable_ref(component_name)
1240-
.value.set(ID_is_used, true);
1235+
if(component_symbol.value.id() == ID_cpp_not_typechecked)
1236+
component_symbol.value.set(ID_is_used, true);
12411237
}
12421238
}
12431239

@@ -1938,9 +1934,9 @@ void cpp_typecheckt::typecheck_method_application(
19381934
exprt member_expr;
19391935
member_expr.swap(expr.function());
19401936

1941-
const symbolt &symbol=lookup(member_expr.get(ID_component_name));
1942-
symbolt &method_symbol=symbol_table.get_writeable_ref(symbol.name);
1943-
const symbolt &tag_symbol = lookup(symbol.type.get(ID_C_member_name));
1937+
symbolt &method_symbol =
1938+
symbol_table.get_writeable_ref(member_expr.get(ID_component_name));
1939+
const symbolt &tag_symbol = lookup(method_symbol.type.get(ID_C_member_name));
19441940

19451941
// build the right template map
19461942
// if this is an instantiated template class method
@@ -1954,21 +1950,21 @@ void cpp_typecheckt::typecheck_method_application(
19541950
static_cast<const cpp_template_args_tct &>(template_args));
19551951
add_method_body(&method_symbol);
19561952
#ifdef DEBUG
1957-
std::cout << "MAP for " << symbol << ":\n";
1953+
std::cout << "MAP for " << method_symbol << ":\n";
19581954
template_map.print(std::cout);
19591955
#endif
19601956
}
19611957
else
19621958
add_method_body(&method_symbol);
19631959

19641960
// build new function expression
1965-
exprt new_function(cpp_symbol_expr(symbol));
1961+
exprt new_function(cpp_symbol_expr(method_symbol));
19661962
new_function.add_source_location()=member_expr.source_location();
19671963
expr.function().swap(new_function);
19681964

19691965
if(!expr.function().type().get_bool(ID_C_is_static))
19701966
{
1971-
const code_typet &func_type=to_code_type(symbol.type);
1967+
const code_typet &func_type = to_code_type(method_symbol.type);
19721968
typet this_type=func_type.parameters().front().type();
19731969

19741970
// Special case. Make it a reference.
@@ -2002,10 +1998,10 @@ void cpp_typecheckt::typecheck_method_application(
20021998
}
20031999

20042000
if(
2005-
symbol.value.id() == ID_cpp_not_typechecked &&
2006-
!symbol.value.get_bool(ID_is_used))
2001+
method_symbol.value.id() == ID_cpp_not_typechecked &&
2002+
!method_symbol.value.get_bool(ID_is_used))
20072003
{
2008-
symbol_table.get_writeable_ref(symbol.name).value.set(ID_is_used, true);
2004+
method_symbol.value.set(ID_is_used, true);
20092005
}
20102006
}
20112007

@@ -2253,13 +2249,11 @@ void cpp_typecheckt::typecheck_expr_function_identifier(exprt &expr)
22532249
if(expr.id()==ID_symbol)
22542250
{
22552251
// Check if the function body has to be typechecked
2256-
symbol_table_baset::symbolst::const_iterator it =
2257-
symbol_table.symbols.find(expr.get(ID_identifier));
2258-
2259-
CHECK_RETURN(it != symbol_table.symbols.end());
2252+
symbolt &function_symbol =
2253+
symbol_table.get_writeable_ref(expr.get(ID_identifier));
22602254

2261-
if(it->second.value.id() == ID_cpp_not_typechecked)
2262-
symbol_table.get_writeable_ref(it->first).value.set(ID_is_used, true);
2255+
if(function_symbol.value.id() == ID_cpp_not_typechecked)
2256+
function_symbol.value.set(ID_is_used, true);
22632257
}
22642258

22652259
c_typecheck_baset::typecheck_expr_function_identifier(expr);

src/cpp/cpp_typecheck_template.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,28 +240,28 @@ void cpp_typecheckt::typecheck_function_template(
240240

241241
// check if we have it already
242242

243-
symbol_table_baset::symbolst::const_iterator previous_symbol =
244-
symbol_table.symbols.find(symbol_name);
243+
symbolt *previous_symbol = symbol_table.get_writeable(symbol_name);
245244

246-
if(previous_symbol!=symbol_table.symbols.end())
245+
if(previous_symbol)
247246
{
248-
bool previous_has_value =
249-
to_cpp_declaration(previous_symbol->second.type).
250-
declarators()[0].find(ID_value).is_not_nil();
247+
bool previous_has_value = to_cpp_declaration(previous_symbol->type)
248+
.declarators()[0]
249+
.find(ID_value)
250+
.is_not_nil();
251251

252252
if(has_value && previous_has_value)
253253
{
254254
error().source_location=cpp_name.source_location();
255255
error() << "function template symbol '" << base_name
256256
<< "' declared previously\n"
257257
<< "location of previous definition: "
258-
<< previous_symbol->second.location << eom;
258+
<< previous_symbol->location << eom;
259259
throw 0;
260260
}
261261

262262
if(has_value)
263263
{
264-
symbol_table.get_writeable_ref(symbol_name).type.swap(declaration);
264+
previous_symbol->type.swap(declaration);
265265
cpp_scopes.id_map[symbol_name]=&template_scope;
266266
}
267267

src/goto-cc/linker_script_merge.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,26 +216,26 @@ int linker_script_merget::pointerize_linker_defined_symbols(
216216

217217
// Next, find all occurrences of linker-defined symbols that are _values_
218218
// of some symbol in the symbol table, and pointerize them too
219-
for(const auto &pair : goto_model.symbol_table.symbols)
219+
for(auto it = goto_model.symbol_table.begin();
220+
it != goto_model.symbol_table.end();
221+
++it)
220222
{
221223
std::list<symbol_exprt> to_pointerize;
222-
symbols_to_pointerize(linker_values, pair.second.value, to_pointerize);
224+
symbols_to_pointerize(linker_values, it->second.value, to_pointerize);
223225

224226
if(to_pointerize.empty())
225227
continue;
226-
log.debug() << "Pointerizing the symbol-table value of symbol "
227-
<< pair.first << messaget::eom;
228+
log.debug() << "Pointerizing the symbol-table value of symbol " << it->first
229+
<< messaget::eom;
228230
int fail = pointerize_subexprs_of(
229-
goto_model.symbol_table.get_writeable_ref(pair.first).value,
230-
to_pointerize,
231-
linker_values);
231+
it.get_writeable_symbol().value, to_pointerize, linker_values);
232232
if(to_pointerize.empty() && fail==0)
233233
continue;
234234
ret=1;
235235
for(const auto &sym : to_pointerize)
236236
{
237237
log.error() << " Could not pointerize '" << sym.get_identifier()
238-
<< "' in symbol table entry " << pair.first << ". Pretty:\n"
238+
<< "' in symbol table entry " << it->first << ". Pretty:\n"
239239
<< sym.pretty() << "\n";
240240
}
241241
log.error() << messaget::eom;

src/goto-instrument/dump_c.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ void dump_ct::operator()(std::ostream &os)
8383
// add copies of struct types when ID_C_transparent_union is only
8484
// annotated to parameter
8585
symbol_tablet additional_symbols;
86-
for(const auto &named_symbol : copied_symbol_table.symbols)
86+
for(auto it = copied_symbol_table.begin(); it != copied_symbol_table.end();
87+
++it)
8788
{
88-
const symbolt &symbol=named_symbol.second;
89+
const symbolt &symbol = it->second;
8990

9091
if(
9192
(symbol.type.id() == ID_union || symbol.type.id() == ID_struct) &&
@@ -99,8 +100,7 @@ void dump_ct::operator()(std::ostream &os)
99100
else
100101
UNREACHABLE;
101102
type_symbolt ts{tag_name, symbol.type, symbol.mode};
102-
typet &type =
103-
copied_symbol_table.get_writeable_ref(named_symbol.first).type;
103+
typet &type = it.get_writeable_symbol().type;
104104
if(ts.type.id() == ID_union)
105105
type = union_tag_typet{ts.name};
106106
else
@@ -111,8 +111,7 @@ void dump_ct::operator()(std::ostream &os)
111111
if(symbol.type.id()!=ID_code)
112112
continue;
113113

114-
code_typet &code_type=to_code_type(
115-
copied_symbol_table.get_writeable_ref(named_symbol.first).type);
114+
code_typet &code_type = to_code_type(it.get_writeable_symbol().type);
116115
code_typet::parameterst &parameters=code_type.parameters();
117116

118117
for(code_typet::parameterst::iterator
@@ -149,9 +148,10 @@ void dump_ct::operator()(std::ostream &os)
149148
// add tags to anonymous union/struct/enum,
150149
// and prepare lexicographic order
151150
std::set<std::string> symbols_sorted;
152-
for(const auto &named_symbol : copied_symbol_table.symbols)
151+
for(auto it = copied_symbol_table.begin(); it != copied_symbol_table.end();
152+
++it)
153153
{
154-
symbolt &symbol = copied_symbol_table.get_writeable_ref(named_symbol.first);
154+
symbolt &symbol = it.get_writeable_symbol();
155155
bool tag_added=false;
156156

157157
// TODO we could get rid of some of the ID_anonymous by looking up
@@ -172,7 +172,7 @@ void dump_ct::operator()(std::ostream &os)
172172
tag_added=true;
173173
}
174174

175-
const std::string name_str=id2string(named_symbol.first);
175+
const std::string name_str = id2string(it->first);
176176
if(symbol.is_type &&
177177
(symbol.type.id()==ID_union ||
178178
symbol.type.id()==ID_struct ||

src/goto-programs/name_mangler.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,17 @@ class function_name_manglert
8888
for(const auto &sym : old_syms)
8989
model.symbol_table.erase(sym);
9090

91-
for(const auto &sym_pair : model.symbol_table)
91+
for(auto it = model.symbol_table.begin(); it != model.symbol_table.end();
92+
++it)
9293
{
93-
const symbolt &sym = sym_pair.second;
94+
const symbolt &sym = it->second;
9495

9596
exprt e = sym.value;
9697
typet t = sym.type;
9798
if(rename(e) && rename(t))
9899
continue;
99100

100-
symbolt &new_sym = model.symbol_table.get_writeable_ref(sym.name);
101+
symbolt &new_sym = it.get_writeable_symbol();
101102
new_sym.value = e;
102103
new_sym.type = t;
103104
}

0 commit comments

Comments
 (0)