Skip to content

Commit 0d15f12

Browse files
Declare a lazy_class_to_declared_symbols_mapt class
This is to avoid computing the map several times and make sure it is never computed if it is not actually needed.
1 parent eeb4f86 commit 0d15f12

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

jbmc/src/java_bytecode/java_bytecode_language.cpp

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,23 @@ 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+
126+
void lazy_class_to_declared_symbols_mapt::reinitialize()
127+
{
128+
initialized = false;
129+
map.clear();
130+
}
131+
115132
/// Consume options that are java bytecode specific.
116133
void java_bytecode_languaget::set_language_options(const optionst &options)
117134
{
@@ -995,12 +1012,17 @@ bool java_bytecode_languaget::do_ci_lazy_method_conversion(
9951012
symbol_table_buildert symbol_table_builder =
9961013
symbol_table_buildert::wrap(symbol_table);
9971014

1015+
lazy_class_to_declared_symbols_mapt class_to_declared_symbols;
1016+
9981017
const method_convertert method_converter =
999-
[this, &symbol_table_builder](
1018+
[this, &symbol_table_builder, &class_to_declared_symbols](
10001019
const irep_idt &function_id,
10011020
ci_lazy_methods_neededt lazy_methods_needed) {
10021021
return convert_single_method(
1003-
function_id, symbol_table_builder, std::move(lazy_methods_needed));
1022+
function_id,
1023+
symbol_table_builder,
1024+
std::move(lazy_methods_needed),
1025+
class_to_declared_symbols);
10041026
};
10051027

10061028
ci_lazy_methodst method_gather(
@@ -1134,7 +1156,8 @@ static void notify_static_method_calls(
11341156
bool java_bytecode_languaget::convert_single_method(
11351157
const irep_idt &function_id,
11361158
symbol_table_baset &symbol_table,
1137-
optionalt<ci_lazy_methods_neededt> needed_lazy_methods)
1159+
optionalt<ci_lazy_methods_neededt> needed_lazy_methods,
1160+
lazy_class_to_declared_symbols_mapt &class_to_declared_symbols)
11381161
{
11391162
// Do not convert if method is not in context
11401163
if(method_in_context && !(*method_in_context)(id2string(function_id)))
@@ -1212,16 +1235,14 @@ bool java_bytecode_languaget::convert_single_method(
12121235
class_name, "user_specified_clinit must be declared by a class.");
12131236
INVARIANT(
12141237
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);
12171238
writable_symbol.value = get_user_specified_clinit_body(
12181239
*class_name,
12191240
*static_values_json,
12201241
symbol_table,
12211242
needed_lazy_methods,
12221243
max_user_array_length,
12231244
references,
1224-
class_to_declared_symbols_map);
1245+
class_to_declared_symbols.get(symbol_table));
12251246
break;
12261247
}
12271248
case synthetic_method_typet::STUB_CLASS_STATIC_INITIALIZER:

jbmc/src/java_bytecode/java_bytecode_language.h

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,30 @@ 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+
/// Note that it only includes function and field symbols (and not for example,
121+
/// local variables), these are produced in the convert-class phase.
122+
/// Calling `get` before the symbol table is properly filled with these symbols,
123+
/// would make later calls return an outdated map. The
124+
/// lazy_class_to_declared_symbols_mapt would then need to be reinitialized.
125+
/// Similarly if some transformation creates or deletes function or field
126+
/// symbols the map would get out of date and would need to be reinitialized.
127+
class lazy_class_to_declared_symbols_mapt
128+
{
129+
public:
130+
lazy_class_to_declared_symbols_mapt() = default;
131+
132+
std::unordered_multimap<irep_idt, symbolt> &
133+
get(const symbol_tablet &symbol_table);
134+
135+
void reinitialize();
136+
137+
private:
138+
bool initialized = false;
139+
std::unordered_multimap<irep_idt, symbolt> map;
140+
};
141+
118142
#define JAVA_CLASS_MODEL_SUFFIX "@class_model"
119143

120144
class java_bytecode_languaget:public languaget
@@ -201,13 +225,18 @@ class java_bytecode_languaget:public languaget
201225
const irep_idt &function_id,
202226
symbol_table_baset &symbol_table)
203227
{
228+
lazy_class_to_declared_symbols_mapt class_to_declared_symbols{};
204229
convert_single_method(
205-
function_id, symbol_table, optionalt<ci_lazy_methods_neededt>());
230+
function_id,
231+
symbol_table,
232+
optionalt<ci_lazy_methods_neededt>(),
233+
class_to_declared_symbols);
206234
}
207235
bool convert_single_method(
208236
const irep_idt &function_id,
209237
symbol_table_baset &symbol_table,
210-
optionalt<ci_lazy_methods_neededt> needed_lazy_methods);
238+
optionalt<ci_lazy_methods_neededt> needed_lazy_methods,
239+
lazy_class_to_declared_symbols_mapt &class_to_declared_symbols);
211240

212241
bool do_ci_lazy_method_conversion(symbol_tablet &);
213242
const select_pointer_typet &get_pointer_type_selector() const;

0 commit comments

Comments
 (0)