Skip to content

Commit 2f431d4

Browse files
Declare a lazy_class_to_declared_symbols_mapt class
This is to avoid computed the map several times and make sure it is never computed if it is not actually needed.
1 parent 9494dbb commit 2f431d4

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

jbmc/src/java_bytecode/java_bytecode_language.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@ static prefix_filtert get_context(const optionst &options)
112112
return prefix_filtert(std::move(context_include), std::move(context_exclude));
113113
}
114114

115+
std::unordered_multimap<irep_idt, symbolt> &
116+
lazy_class_to_declared_symbols_mapt::get(const symbol_tablet &symbol_table)
117+
{
118+
if(!initialized)
119+
{
120+
map = class_to_declared_symbols(symbol_table);
121+
initialized = true;
122+
}
123+
return map;
124+
}
125+
115126
/// Consume options that are java bytecode specific.
116127
void java_bytecode_languaget::set_language_options(const optionst &options)
117128
{
@@ -995,12 +1006,17 @@ bool java_bytecode_languaget::do_ci_lazy_method_conversion(
9951006
symbol_table_buildert symbol_table_builder =
9961007
symbol_table_buildert::wrap(symbol_table);
9971008

1009+
lazy_class_to_declared_symbols_mapt class_to_declared_symbols;
1010+
9981011
const method_convertert method_converter =
999-
[this, &symbol_table_builder](
1012+
[this, &symbol_table_builder, &class_to_declared_symbols](
10001013
const irep_idt &function_id,
10011014
ci_lazy_methods_neededt lazy_methods_needed) {
10021015
return convert_single_method(
1003-
function_id, symbol_table_builder, std::move(lazy_methods_needed));
1016+
function_id,
1017+
symbol_table_builder,
1018+
std::move(lazy_methods_needed),
1019+
class_to_declared_symbols);
10041020
};
10051021

10061022
ci_lazy_methodst method_gather(
@@ -1134,7 +1150,8 @@ static void notify_static_method_calls(
11341150
bool java_bytecode_languaget::convert_single_method(
11351151
const irep_idt &function_id,
11361152
symbol_table_baset &symbol_table,
1137-
optionalt<ci_lazy_methods_neededt> needed_lazy_methods)
1153+
optionalt<ci_lazy_methods_neededt> needed_lazy_methods,
1154+
lazy_class_to_declared_symbols_mapt &class_to_declared_symbols)
11381155
{
11391156
// Do not convert if method is not in context
11401157
if(method_in_context && !(*method_in_context)(id2string(function_id)))
@@ -1212,16 +1229,14 @@ bool java_bytecode_languaget::convert_single_method(
12121229
class_name, "user_specified_clinit must be declared by a class.");
12131230
INVARIANT(
12141231
static_values_json.has_value(), "static-values JSON must be available");
1215-
const auto class_to_declared_symbols_map =
1216-
class_to_declared_symbols(symbol_table);
12171232
writable_symbol.value = get_user_specified_clinit_body(
12181233
*class_name,
12191234
*static_values_json,
12201235
symbol_table,
12211236
needed_lazy_methods,
12221237
max_user_array_length,
12231238
references,
1224-
class_to_declared_symbols_map);
1239+
class_to_declared_symbols.get(symbol_table));
12251240
break;
12261241
}
12271242
case synthetic_method_typet::STUB_CLASS_STATIC_INITIALIZER:

jbmc/src/java_bytecode/java_bytecode_language.h

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@ enum lazy_methods_modet
115115
LAZY_METHODS_MODE_EXTERNAL_DRIVER
116116
};
117117

118+
/// Map classes to the symbols they declare but is only computed once it is
119+
/// needed and the map is then kept.
120+
class lazy_class_to_declared_symbols_mapt
121+
{
122+
public:
123+
lazy_class_to_declared_symbols_mapt() = default;
124+
125+
std::unordered_multimap<irep_idt, symbolt> &
126+
get(const symbol_tablet &symbol_table);
127+
128+
private:
129+
bool initialized = false;
130+
std::unordered_multimap<irep_idt, symbolt> map;
131+
};
132+
118133
#define JAVA_CLASS_MODEL_SUFFIX "@class_model"
119134

120135
class java_bytecode_languaget:public languaget
@@ -201,13 +216,18 @@ class java_bytecode_languaget:public languaget
201216
const irep_idt &function_id,
202217
symbol_table_baset &symbol_table)
203218
{
219+
lazy_class_to_declared_symbols_mapt class_to_declared_symbols{};
204220
convert_single_method(
205-
function_id, symbol_table, optionalt<ci_lazy_methods_neededt>());
221+
function_id,
222+
symbol_table,
223+
optionalt<ci_lazy_methods_neededt>(),
224+
class_to_declared_symbols);
206225
}
207226
bool convert_single_method(
208227
const irep_idt &function_id,
209228
symbol_table_baset &symbol_table,
210-
optionalt<ci_lazy_methods_neededt> needed_lazy_methods);
229+
optionalt<ci_lazy_methods_neededt> needed_lazy_methods,
230+
lazy_class_to_declared_symbols_mapt &class_to_declared_symbols);
211231

212232
bool do_ci_lazy_method_conversion(symbol_tablet &);
213233
const select_pointer_typet &get_pointer_type_selector() const;

0 commit comments

Comments
 (0)