Skip to content

Commit 3653550

Browse files
author
Owen Jones
committed
Use unordered set of irep_ids in ci_lazy_methods
Used set instead of vector to remove duplicates, which should prevent investigating the same method multiple times.
1 parent c31d43f commit 3653550

File tree

4 files changed

+29
-34
lines changed

4 files changed

+29
-34
lines changed

src/java_bytecode/ci_lazy_methods.cpp

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ bool ci_lazy_methodst::operator()(
7272
method_bytecodet &method_bytecode,
7373
const method_convertert &method_converter)
7474
{
75-
std::vector<irep_idt> methods_to_convert_later;
75+
std::unordered_set<irep_idt> methods_to_convert_later;
7676

7777
main_function_resultt main_function =
7878
get_main_symbol(symbol_table, main_class, get_message_handler());
@@ -94,39 +94,33 @@ bool ci_lazy_methodst::operator()(
9494
const irep_idt methodid =
9595
"java::" + id2string(class_name) + "." + id2string(method.name)
9696
+ ":" + id2string(method.descriptor);
97-
methods_to_convert_later.push_back(methodid);
97+
methods_to_convert_later.insert(methodid);
9898
}
9999
}
100100
}
101101
else
102-
methods_to_convert_later.push_back(main_function.main_function.name);
102+
methods_to_convert_later.insert(main_function.main_function.name);
103103

104104
// Add any extra entry points specified; we should elaborate these in the
105105
// same way as the main function.
106106
std::vector<irep_idt> extra_entry_points=lazy_methods_extra_entry_points;
107107
resolve_method_names(extra_entry_points, symbol_table);
108108
methods_to_convert_later.insert(
109-
methods_to_convert_later.begin(),
110-
extra_entry_points.begin(),
111-
extra_entry_points.end());
109+
extra_entry_points.begin(), extra_entry_points.end());
112110

113-
std::set<irep_idt> instantiated_classes;
111+
std::unordered_set<irep_idt> instantiated_classes;
114112

115113
{
116-
std::vector<irep_idt> initial_callable_methods;
114+
std::unordered_set<irep_idt> initial_callable_methods;
117115
ci_lazy_methods_neededt initial_lazy_methods(
118-
initial_callable_methods,
119-
instantiated_classes,
120-
symbol_table);
116+
initial_callable_methods, instantiated_classes, symbol_table);
121117
initialize_instantiated_classes(
122118
methods_to_convert_later, namespacet(symbol_table), initial_lazy_methods);
123119
methods_to_convert_later.insert(
124-
methods_to_convert_later.end(),
125-
initial_callable_methods.begin(),
126-
initial_callable_methods.end());
120+
initial_callable_methods.begin(), initial_callable_methods.end());
127121
}
128122

129-
std::set<irep_idt> methods_already_populated;
123+
std::unordered_set<irep_idt> methods_already_populated;
130124
std::vector<const code_function_callt *> virtual_callsites;
131125

132126
bool any_new_methods = true;
@@ -135,7 +129,7 @@ bool ci_lazy_methodst::operator()(
135129
any_new_methods=false;
136130
while(!methods_to_convert_later.empty())
137131
{
138-
std::vector<irep_idt> methods_to_convert;
132+
std::unordered_set<irep_idt> methods_to_convert;
139133
std::swap(methods_to_convert, methods_to_convert_later);
140134
for(const auto &mname : methods_to_convert)
141135
{
@@ -275,7 +269,7 @@ void ci_lazy_methodst::resolve_method_names(
275269
/// whose references may be passed, directly or indirectly, to any of the
276270
/// functions in `entry_points`.
277271
void ci_lazy_methodst::initialize_instantiated_classes(
278-
const std::vector<irep_idt> &entry_points,
272+
const std::unordered_set<irep_idt> &entry_points,
279273
const namespacet &ns,
280274
ci_lazy_methods_neededt &needed_lazy_methods)
281275
{
@@ -411,8 +405,8 @@ void ci_lazy_methodst::gather_virtual_callsites(
411405
/// defined on classes that are not 'needed' are ignored)
412406
void ci_lazy_methodst::get_virtual_method_targets(
413407
const exprt &called_function,
414-
const std::set<irep_idt> &instantiated_classes,
415-
std::vector<irep_idt> &callable_methods,
408+
const std::unordered_set<irep_idt> &instantiated_classes,
409+
std::unordered_set<irep_idt> &callable_methods,
416410
symbol_tablet &symbol_table)
417411
{
418412
PRECONDITION(called_function.id()==ID_virtual_function);
@@ -434,7 +428,7 @@ void ci_lazy_methodst::get_virtual_method_targets(
434428
const irep_idt method_name = get_virtual_method_target(
435429
instantiated_classes, call_basename, class_name, symbol_table);
436430
if(!method_name.empty())
437-
callable_methods.push_back(method_name);
431+
callable_methods.insert(method_name);
438432
}
439433
}
440434

@@ -540,7 +534,7 @@ void ci_lazy_methodst::gather_field_types(
540534
/// `call_basename` if found and `classname` is present in
541535
/// `instantiated_classes`, or irep_idt() otherwise.
542536
irep_idt ci_lazy_methodst::get_virtual_method_target(
543-
const std::set<irep_idt> &instantiated_classes,
537+
const std::unordered_set<irep_idt> &instantiated_classes,
544538
const irep_idt &call_basename,
545539
const irep_idt &classname,
546540
const symbol_tablet &symbol_table)

src/java_bytecode/ci_lazy_methods.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class ci_lazy_methodst:public messaget
115115
const symbol_tablet &symbol_table);
116116

117117
void initialize_instantiated_classes(
118-
const std::vector<irep_idt> &entry_points,
118+
const std::unordered_set<irep_idt> &entry_points,
119119
const namespacet &ns,
120120
ci_lazy_methods_neededt &needed_lazy_methods);
121121

@@ -135,8 +135,8 @@ class ci_lazy_methodst:public messaget
135135

136136
void get_virtual_method_targets(
137137
const exprt &called_function,
138-
const std::set<irep_idt> &instantiated_classes,
139-
std::vector<irep_idt> &callable_methods,
138+
const std::unordered_set<irep_idt> &instantiated_classes,
139+
std::unordered_set<irep_idt> &callable_methods,
140140
symbol_tablet &symbol_table);
141141

142142
void gather_needed_globals(
@@ -150,7 +150,7 @@ class ci_lazy_methodst:public messaget
150150
ci_lazy_methods_neededt &needed_lazy_methods);
151151

152152
irep_idt get_virtual_method_target(
153-
const std::set<irep_idt> &instantiated_classes,
153+
const std::unordered_set<irep_idt> &instantiated_classes,
154154
const irep_idt &call_basename,
155155
const irep_idt &classname,
156156
const symbol_tablet &symbol_table);

src/java_bytecode/ci_lazy_methods_needed.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Author: Chris Smowton, [email protected]
2020
void ci_lazy_methods_neededt::add_needed_method(
2121
const irep_idt &method_symbol_name)
2222
{
23-
callable_methods.push_back(method_symbol_name);
23+
callable_methods.insert(method_symbol_name);
2424
}
2525

2626
/// Notes class `class_symbol_name` will be instantiated, or a static field

src/java_bytecode/ci_lazy_methods_needed.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@ Author: Chris Smowton, [email protected]
1414

1515
#include <vector>
1616
#include <set>
17+
#include <unordered_set>
1718
#include <util/symbol_table.h>
1819

1920
class ci_lazy_methods_neededt
2021
{
2122
public:
2223
ci_lazy_methods_neededt(
23-
std::vector<irep_idt> &_callable_methods,
24-
std::set<irep_idt> &_instantiated_classes,
25-
symbol_tablet &_symbol_table):
26-
callable_methods(_callable_methods),
27-
instantiated_classes(_instantiated_classes),
28-
symbol_table(_symbol_table)
24+
std::unordered_set<irep_idt> &_callable_methods,
25+
std::unordered_set<irep_idt> &_instantiated_classes,
26+
symbol_tablet &_symbol_table)
27+
: callable_methods(_callable_methods),
28+
instantiated_classes(_instantiated_classes),
29+
symbol_table(_symbol_table)
2930
{}
3031

3132
void add_needed_method(const irep_idt &);
@@ -38,11 +39,11 @@ class ci_lazy_methods_neededt
3839
// contain all methods that have previously been elaborated.
3940
// It should be changed to a set if we develop the need to use
4041
// it that way.
41-
std::vector<irep_idt> &callable_methods;
42+
std::unordered_set<irep_idt> &callable_methods;
4243
// instantiated_classes on the other hand is a true set of every class
4344
// found so far, so we can use a membership test to avoid
4445
// repeatedly exploring a class hierarchy.
45-
std::set<irep_idt> &instantiated_classes;
46+
std::unordered_set<irep_idt> &instantiated_classes;
4647
symbol_tablet &symbol_table;
4748
};
4849

0 commit comments

Comments
 (0)