Skip to content

Commit 06807c4

Browse files
Make get_user_specified_clinit_body take jsont arg
This avoids reparsing the whole json file for each class that needs a clinit. Locally, on an example with a large json provided for --static-values, execution time went from 10s to 2s.
1 parent d4718cb commit 06807c4

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

jbmc/src/java_bytecode/java_bytecode_language.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,13 @@ void java_bytecode_languaget::set_language_options(const optionst &options)
200200
java_cp_include_files=".*";
201201

202202
nondet_static = options.get_bool_option("nondet-static");
203-
static_values_file = options.get_option("static-values");
203+
if(options.is_set("static-values"))
204+
{
205+
const std::string filename = options.get_option("static-values");
206+
jsont tmp_json;
207+
if(!parse_json(filename, *message_handler, tmp_json))
208+
static_values_json = std::move(tmp_json);
209+
}
204210

205211
if(options.is_set("context-include") || options.is_set("context-exclude"))
206212
method_in_context = get_context(options);
@@ -847,7 +853,7 @@ bool java_bytecode_languaget::typecheck(
847853
symbol_table,
848854
synthetic_methods,
849855
threading_support,
850-
!static_values_file.empty());
856+
static_values_json.has_value());
851857

852858
// Now incrementally elaborate methods
853859
// that are reachable from this entry point.
@@ -1171,7 +1177,7 @@ bool java_bytecode_languaget::convert_single_method(
11711177
function_id,
11721178
symbol_table,
11731179
nondet_static,
1174-
!static_values_file.empty(),
1180+
static_values_json.has_value(),
11751181
object_factory_parameters,
11761182
get_pointer_type_selector(),
11771183
get_message_handler());
@@ -1180,7 +1186,7 @@ bool java_bytecode_languaget::convert_single_method(
11801186
function_id,
11811187
symbol_table,
11821188
nondet_static,
1183-
!static_values_file.empty(),
1189+
static_values_json.has_value(),
11841190
object_factory_parameters,
11851191
get_pointer_type_selector(),
11861192
get_message_handler());
@@ -1193,7 +1199,7 @@ bool java_bytecode_languaget::convert_single_method(
11931199
class_name, "user_specified_clinit must be declared by a class.");
11941200
writable_symbol.value = get_user_specified_clinit_body(
11951201
*class_name,
1196-
static_values_file,
1202+
static_values_json,
11971203
symbol_table,
11981204
get_message_handler(),
11991205
needed_lazy_methods,

jbmc/src/java_bytecode/java_bytecode_language.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,10 @@ class java_bytecode_languaget:public languaget
229229
java_string_library_preprocesst string_preprocess;
230230
std::string java_cp_include_files;
231231
bool nondet_static;
232-
/// Path to a JSON file which contains initial values of static fields (right
232+
/// JSON which contains initial values of static fields (right
233233
/// after the static initializer of the class was run). This is read from the
234234
/// --static-values command-line option.
235-
std::string static_values_file;
235+
optionalt<jsont> static_values_json;
236236

237237
// list of classes to force load even without reference from the entry point
238238
std::vector<irep_idt> java_load_classes;

jbmc/src/java_bytecode/java_static_initializers.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -769,20 +769,17 @@ code_ifthenelset get_clinit_wrapper_body(
769769

770770
code_blockt get_user_specified_clinit_body(
771771
const irep_idt &class_id,
772-
const std::string &static_values_file,
772+
const optionalt<jsont> &static_values_json,
773773
symbol_table_baset &symbol_table,
774774
message_handlert &message_handler,
775775
optionalt<ci_lazy_methods_neededt> needed_lazy_methods,
776776
size_t max_user_array_length,
777777
std::unordered_map<std::string, object_creation_referencet> &references)
778778
{
779-
jsont json;
780779
const irep_idt &real_clinit_name = clinit_function_name(class_id);
781-
if(
782-
!static_values_file.empty() &&
783-
!parse_json(static_values_file, message_handler, json) && json.is_object())
780+
if(static_values_json.has_value() && static_values_json->is_object())
784781
{
785-
const auto &json_object = to_json_object(json);
782+
const auto &json_object = to_json_object(*static_values_json);
786783
const auto class_entry =
787784
json_object.find(id2string(strip_java_namespace_prefix(class_id)));
788785
if(class_entry != json_object.end())

jbmc/src/java_bytecode/java_static_initializers.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,9 @@ code_ifthenelset get_clinit_wrapper_body(
6262
/// assigned default values (zero or null).
6363
/// \param class_id: the id of the class to create a user_specified_clinit
6464
/// function body for.
65-
/// \param static_values_file: input file containing values of static fields.
65+
/// \param static_values_json: JSON object containing values of static fields.
6666
/// The format is expected to be a map whose keys are class names, and whose
67-
/// values are maps from static field names to values. Currently only JSON
68-
/// is supported as a file format.
67+
/// values are maps from static field names to values.
6968
/// \param symbol_table: used to look up and create new symbols
7069
/// \param message_handler: used to log any errors with parsing the input file
7170
/// \param needed_lazy_methods: used to mark any runtime types given in the
@@ -77,7 +76,7 @@ code_ifthenelset get_clinit_wrapper_body(
7776
/// \return the body of the user_specified_clinit function as a code block.
7877
code_blockt get_user_specified_clinit_body(
7978
const irep_idt &class_id,
80-
const std::string &static_values_file,
79+
const optionalt<jsont> &static_values_json,
8180
symbol_table_baset &symbol_table,
8281
message_handlert &message_handler,
8382
optionalt<ci_lazy_methods_neededt> needed_lazy_methods,

0 commit comments

Comments
 (0)