Skip to content

Commit 95f975b

Browse files
NathanJPhillipssmowton
authored andcommitted
Rework CI-lazy-methods' symbol table garbage collection to avoid swap
This is useful because in a forthcoming commit symbol_tablet will become an interface, and therefore lose its `swap` method. In the case where the particular implementation journals its operations this will also provide a more accurate impression of what has taken place (many symbols were erased, cf. this table was swapped with another which experienced many insertions)
1 parent dfb11c2 commit 95f975b

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/java_bytecode/ci_lazy_methods.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ bool ci_lazy_methodst::operator()(
172172
while(any_new_methods);
173173

174174
// Remove symbols for methods that were declared but never used:
175-
symbol_tablet keep_symbols;
175+
std::unordered_set<irep_idt, irep_id_hash> unreachable_symbols;
176+
for(const auto &sym : symbol_table.symbols)
177+
unreachable_symbols.insert(sym.first);
176178

177179
for(const auto &sym : symbol_table.symbols)
178180
{
@@ -184,16 +186,19 @@ bool ci_lazy_methodst::operator()(
184186
continue;
185187
}
186188
if(sym.second.type.id()==ID_code)
187-
gather_needed_globals(sym.second.value, symbol_table, keep_symbols);
188-
keep_symbols.add(sym.second);
189+
{
190+
gather_needed_globals(
191+
sym.second.value, symbol_table, unreachable_symbols);
192+
}
193+
unreachable_symbols.erase(sym.first);
189194
}
190195

191-
debug() << "CI lazy methods: removed "
192-
<< symbol_table.symbols.size() - keep_symbols.symbols.size()
193-
<< " unreaclass_hierarchyable methods and globals"
194-
<< eom;
196+
debug()
197+
<< "CI lazy methods: removed " << unreachable_symbols.size()
198+
<< " unreachable methods and globals" << eom;
195199

196-
symbol_table.swap(keep_symbols);
200+
for(const irep_idt &symbol_name : unreachable_symbols)
201+
symbol_table.remove(symbol_name);
197202

198203
return false;
199204
}
@@ -425,7 +430,7 @@ void ci_lazy_methodst::get_virtual_method_targets(
425430
void ci_lazy_methodst::gather_needed_globals(
426431
const exprt &e,
427432
const symbol_tablet &symbol_table,
428-
symbol_tablet &needed)
433+
std::unordered_set<irep_idt, irep_id_hash> &unreachable)
429434
{
430435
if(e.id()==ID_symbol)
431436
{
@@ -438,12 +443,12 @@ void ci_lazy_methodst::gather_needed_globals(
438443
if(findit!=symbol_table.symbols.end() &&
439444
findit->second.is_static_lifetime)
440445
{
441-
needed.add(findit->second);
446+
unreachable.erase(findit->first);
442447
}
443448
}
444449
else
445450
forall_operands(opit, e)
446-
gather_needed_globals(*opit, symbol_table, needed);
451+
gather_needed_globals(*opit, symbol_table, unreachable);
447452
}
448453

449454
/// See param lazy_methods

src/java_bytecode/ci_lazy_methods.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#define CPROVER_JAVA_BYTECODE_GATHER_METHODS_LAZILY_H
1414

1515
#include <map>
16+
#include <unordered_set>
1617
#include <functional>
1718

1819
#include <util/irep.h>
@@ -90,7 +91,7 @@ class ci_lazy_methodst:public messaget
9091
void gather_needed_globals(
9192
const exprt &e,
9293
const symbol_tablet &symbol_table,
93-
symbol_tablet &needed);
94+
std::unordered_set<irep_idt, irep_id_hash> &unreachable);
9495

9596
void gather_field_types(const typet &class_type,
9697
const namespacet &ns,

0 commit comments

Comments
 (0)