Skip to content

Commit 610761e

Browse files
authored
Merge pull request #5539 from thomasspriggs/tas/arrays_optimisation
Performance optimisation - Use `unordered_map` in `numbering` instead of `map`
2 parents d14e078 + 2bf69bf commit 610761e

17 files changed

+37
-44
lines changed

src/analyses/custom_bitvector_analysis.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class custom_bitvector_analysist:public ait<custom_bitvector_domaint>
163163

164164
unsigned get_bit_nr(const exprt &);
165165

166-
typedef numbering<irep_idt> bitst;
166+
typedef numberingt<irep_idt> bitst;
167167
bitst bits;
168168

169169
protected:

src/analyses/escape_analysis.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class escape_analysist:public ait<escape_domaint>
120120
{
121121
}
122122

123-
numbering<irep_idt> bits;
123+
numberingt<irep_idt> bits;
124124

125125
void insert_cleanup(
126126
goto_functionst::goto_functiont &,

src/analyses/invariant_set.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class inv_object_storet
5555
protected:
5656
const namespacet &ns;
5757

58-
typedef hash_numbering<irep_idt, irep_id_hash> mapt;
58+
typedef numberingt<irep_idt> mapt;
5959
mapt map;
6060

6161
struct entryt

src/analyses/local_bitvector_analysis.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ class local_bitvector_analysist
183183

184184
typedef std::stack<unsigned> work_queuet;
185185

186-
numbering<irep_idt> pointers;
186+
numberingt<irep_idt> pointers;
187187

188188
// pointers -> flagst
189189
// This is a vector, so it's fast.

src/analyses/local_may_alias.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class local_may_aliast
6060

6161
typedef std::stack<local_cfgt::node_nrt> work_queuet;
6262

63-
mutable numbering<exprt> objects;
63+
mutable numberingt<exprt, irep_hash> objects;
6464

6565
typedef unsigned_union_find alias_sett;
6666

src/goto-programs/vcd_goto_trace.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void output_vcd(
8282

8383
// we first collect all variables that are assigned
8484

85-
numbering<irep_idt> n;
85+
numberingt<irep_idt> n;
8686

8787
for(const auto &step : goto_trace.steps)
8888
{

src/pointer-analysis/object_numbering.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ Author: Daniel Kroening, [email protected]
2525
#include <util/expr.h>
2626
#include <util/numbering.h>
2727

28-
typedef hash_numbering<exprt, irep_hash> object_numberingt;
28+
typedef numberingt<exprt, irep_hash> object_numberingt;
2929

3030
#endif // CPROVER_POINTER_ANALYSIS_OBJECT_NUMBERING_H

src/pointer-analysis/value_set_fi.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Author: Daniel Kroening, [email protected]
2929
const value_set_fit::object_map_dt value_set_fit::object_map_dt::blank{};
3030

3131
object_numberingt value_set_fit::object_numbering;
32-
hash_numbering<irep_idt, irep_id_hash> value_set_fit::function_numbering;
32+
numberingt<irep_idt> value_set_fit::function_numbering;
3333

3434
static const char *alloc_adapter_prefix="alloc_adaptor::";
3535

src/pointer-analysis/value_set_fi.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class value_set_fit
3939
unsigned to_function, from_function;
4040
unsigned to_target_index, from_target_index;
4141
static object_numberingt object_numbering;
42-
static hash_numbering<irep_idt, irep_id_hash> function_numbering;
42+
static numberingt<irep_idt> function_numbering;
4343

4444
void set_from(const irep_idt &function, unsigned inx)
4545
{

src/pointer-analysis/value_set_fivr.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class value_set_fivrt
3636
unsigned to_function, from_function;
3737
unsigned to_target_index, from_target_index;
3838
static object_numberingt object_numbering;
39-
static hash_numbering<irep_idt, irep_id_hash> function_numbering;
39+
static numberingt<irep_idt> function_numbering;
4040

4141
void set_from(const irep_idt &function, unsigned inx)
4242
{

src/pointer-analysis/value_set_fivrns.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class value_set_fivrnst
3737
unsigned to_function, from_function;
3838
unsigned to_target_index, from_target_index;
3939
static object_numberingt object_numbering;
40-
static hash_numbering<irep_idt, irep_id_hash> function_numbering;
40+
static numberingt<irep_idt> function_numbering;
4141

4242
void set_from(const irep_idt &function, unsigned inx)
4343
{

src/solvers/flattening/arrays.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class arrayst:public equalityt
6868
array_equalitiest array_equalities;
6969

7070
// this is used to find the clusters of arrays being compared
71-
union_find<exprt> arrays;
71+
union_find<exprt, irep_hash> arrays;
7272

7373
// this tracks the array indicies for each array
7474
typedef std::set<exprt> index_sett;

src/solvers/flattening/boolbv.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ class boolbvt:public arrayst
262262
offset_mapt build_offset_map(const struct_typet &src);
263263

264264
// strings
265-
numbering<irep_idt> string_numbering;
265+
numberingt<irep_idt> string_numbering;
266266
};
267267

268268
#endif // CPROVER_SOLVERS_FLATTENING_BOOLBV_H

src/solvers/flattening/pointer_logic.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class pointer_logict
2323
{
2424
public:
2525
// this numbers the objects
26-
hash_numbering<exprt, irep_hash> objects;
26+
numberingt<exprt, irep_hash> objects;
2727

2828
struct pointert
2929
{

src/util/irep_hash_container.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ class irep_hash_container_baset
6969
std::size_t operator()(const packedt &p) const;
7070
};
7171

72-
typedef hash_numbering<packedt, vector_hasht> numberingt;
73-
numberingt numbering;
72+
numberingt<packedt, vector_hasht> numbering;
7473

7574
void pack(const irept &irep, packedt &);
7675

src/util/numbering.h

+7-13
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@ Author: Daniel Kroening, [email protected]
99
#ifndef CPROVER_UTIL_NUMBERING_H
1010
#define CPROVER_UTIL_NUMBERING_H
1111

12-
#include <map>
1312
#include <unordered_map>
1413
#include <vector>
1514

1615
#include "invariant.h"
1716
#include "optional.h"
1817

19-
/// \tparam Map: a map from a key type to some numeric type
20-
template <typename Map>
21-
class template_numberingt final
18+
/// \tparam keyt: The type of keys which will be numbered.
19+
/// \tparam hasht: The type of hashing functor used to hash keys.
20+
template <typename keyt, typename hasht = std::hash<keyt>>
21+
class numberingt final
2222
{
2323
public:
24-
using number_type = typename Map::mapped_type; // NOLINT
25-
using key_type = typename Map::key_type; // NOLINT
24+
using number_type = std::size_t; // NOLINT
25+
using key_type = keyt; // NOLINT
2626

2727
private:
2828
using data_typet = std::vector<key_type>; // NOLINT
2929
data_typet data_;
30-
Map numbers_;
30+
std::unordered_map<keyt, number_type, hasht> numbers_;
3131

3232
public:
3333
using size_type = typename data_typet::size_type; // NOLINT
@@ -109,11 +109,5 @@ class template_numberingt final
109109
}
110110
};
111111

112-
template <typename Key>
113-
using numbering = template_numberingt<std::map<Key, std::size_t>>; // NOLINT
114-
115-
template <typename Key, typename Hash>
116-
using hash_numbering = // NOLINT
117-
template_numberingt<std::unordered_map<Key, std::size_t, Hash>>;
118112

119113
#endif // CPROVER_UTIL_NUMBERING_H

src/util/union_find.h

+15-15
Original file line numberDiff line numberDiff line change
@@ -132,23 +132,25 @@ class unsigned_union_find
132132
size_type get_other(size_type a);
133133
};
134134

135-
template <typename T>
135+
/// \tparam T: The type of values stored.
136+
/// \tparam hasht: The type of hash used for looking up the value numbering.
137+
template <typename T, typename hasht = std::hash<T>>
136138
// NOLINTNEXTLINE(readability/identifiers)
137139
class union_find final
138140
{
139-
typedef numbering<T> numbering_typet;
141+
using numbering_typet = numberingt<T, hasht>;
140142
numbering_typet numbers;
141143

142144
// NOLINTNEXTLINE(readability/identifiers)
143-
typedef typename numbering_typet::number_type number_type;
145+
using number_type = typename numbering_typet::number_type;
144146

145147
public:
146148
// NOLINTNEXTLINE(readability/identifiers)
147-
typedef typename numbering_typet::size_type size_type;
149+
using size_type = typename numbering_typet::size_type;
148150
// NOLINTNEXTLINE(readability/identifiers)
149-
typedef typename numbering_typet::iterator iterator;
151+
using iterator = typename numbering_typet::iterator;
150152
// NOLINTNEXTLINE(readability/identifiers)
151-
typedef typename numbering_typet::const_iterator const_iterator;
153+
using const_iterator = typename numbering_typet::const_iterator;
152154

153155
// true == already in same set
154156
bool make_union(const T &a, const T &b)
@@ -160,8 +162,7 @@ class union_find final
160162
}
161163

162164
// true == already in same set
163-
bool make_union(typename numbering<T>::const_iterator it_a,
164-
typename numbering<T>::const_iterator it_b)
165+
bool make_union(const_iterator it_a, const_iterator it_b)
165166
{
166167
size_type na=it_a-numbers.begin(), nb=it_b-numbers.begin();
167168
bool is_union=find_number(na)==find_number(nb);
@@ -183,13 +184,12 @@ class union_find final
183184
}
184185

185186
// are 'a' and 'b' in the same set?
186-
bool same_set(typename numbering<T>::const_iterator it_a,
187-
typename numbering<T>::const_iterator it_b) const
187+
bool same_set(const_iterator it_a, const_iterator it_b) const
188188
{
189189
return uuf.same_set(it_a-numbers.begin(), it_b-numbers.begin());
190190
}
191191

192-
const T &find(typename numbering<T>::const_iterator it) const
192+
const T &find(const_iterator it) const
193193
{
194194
return numbers[find_number(it-numbers.begin())];
195195
}
@@ -199,7 +199,7 @@ class union_find final
199199
return numbers[find_number(number(a))];
200200
}
201201

202-
size_type find_number(typename numbering<T>::const_iterator it) const
202+
size_type find_number(const_iterator it) const
203203
{
204204
return find_number(it-numbers.begin());
205205
}
@@ -228,7 +228,7 @@ class union_find final
228228
return uuf.is_root(na);
229229
}
230230

231-
bool is_root(typename numbering<T>::const_iterator it) const
231+
bool is_root(const_iterator it) const
232232
{
233233
return uuf.is_root(it-numbers.begin());
234234
}
@@ -251,7 +251,7 @@ class union_find final
251251
uuf.clear();
252252
}
253253

254-
void isolate(typename numbering<T>::const_iterator it)
254+
void isolate(const_iterator it)
255255
{
256256
uuf.isolate(it-numbers.begin());
257257
}
@@ -281,7 +281,7 @@ class union_find final
281281

282282
protected:
283283
unsigned_union_find uuf;
284-
typedef numbering<T> subt;
284+
using subt = numbering_typet;
285285
};
286286

287287
#endif // CPROVER_UTIL_UNION_FIND_H

0 commit comments

Comments
 (0)