Skip to content

Commit e53c793

Browse files
Avoid repeated searches in map
1 parent 79a6af4 commit e53c793

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

jbmc/src/java_bytecode/generic_parameter_specialization_map_keys.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void generic_parameter_specialization_map_keyst::insert_pairs(
4949
{
5050
// Only add the pair if the type is not the parameter itself, e.g.,
5151
// pair.first = pair.second = java::A::T. This can happen for example
52-
// when initiating a pointer to an implicitly java generic class type
52+
// when initializing a pointer to an implicitly generic Java class type
5353
// in gen_nondet_init and would result in a loop when the map is used
5454
// to look up the type of the parameter.
5555
if(
@@ -58,13 +58,13 @@ void generic_parameter_specialization_map_keyst::insert_pairs(
5858
pair.first.get_name()))
5959
{
6060
const irep_idt &key = pair.first.get_name();
61-
if(generic_parameter_specialization_map.count(key) == 0)
62-
generic_parameter_specialization_map.emplace(
63-
key, std::vector<reference_typet>());
64-
(*generic_parameter_specialization_map.find(key))
65-
.second.push_back(pair.second);
61+
const auto map_it = generic_parameter_specialization_map
62+
.emplace(key, std::vector<reference_typet>{})
63+
.first;
64+
map_it->second.push_back(pair.second);
6665

67-
// We added something, so pop it when this is destroyed:
66+
// We added something; pop it when this
67+
// generic_parameter_specialization_map_keyst is destroyed
6868
erase_keys.push_back(key);
6969
}
7070
}

jbmc/src/java_bytecode/generic_parameter_specialization_map_keys.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ class generic_parameter_specialization_map_keyst
3333
{
3434
for(const auto key : erase_keys)
3535
{
36-
PRECONDITION(generic_parameter_specialization_map.count(key) != 0);
37-
auto &val = generic_parameter_specialization_map.find(key)->second;
36+
const auto map_it = generic_parameter_specialization_map.find(key);
37+
PRECONDITION(map_it != generic_parameter_specialization_map.end());
38+
std::vector<reference_typet> &val = map_it->second;
3839
val.pop_back();
3940
if(val.empty())
40-
generic_parameter_specialization_map.erase(key);
41+
generic_parameter_specialization_map.erase(map_it);
4142
}
4243
}
4344

jbmc/src/java_bytecode/select_pointer_type.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,17 @@ pointer_typet select_pointer_typet::specialize_generics(
5858
return result.has_value() ? result.value() : pointer_type;
5959
}
6060

61-
if(generic_parameter_specialization_map.count(parameter_name) == 0)
61+
const auto specialization =
62+
generic_parameter_specialization_map.find(parameter_name);
63+
if(specialization == generic_parameter_specialization_map.end())
6264
{
6365
// this means that the generic pointer_type has not been specialized
6466
// in the current context (e.g., the method under test is generic);
6567
// we return the pointer_type itself which is basically a pointer to
6668
// its upper bound
6769
return pointer_type;
6870
}
69-
const pointer_typet &type =
70-
generic_parameter_specialization_map.find(parameter_name)->second.back();
71+
const pointer_typet &type = specialization->second.back();
7172

7273
// generic parameters can be adopted from outer classes or superclasses so
7374
// we may need to search for the concrete type recursively

0 commit comments

Comments
 (0)