Skip to content

Commit 3156ad7

Browse files
marek-trtiktautschnig
authored andcommitted
Using cache to speed up generation of GraphML file
Expression-to-string conversion is costly.
1 parent 9e303e2 commit 3156ad7

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/goto-programs/graphml_witness.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Author: Daniel Kroening
1818
#include <util/pointer_predicates.h>
1919
#include <util/prefix.h>
2020
#include <util/ssa_expr.h>
21+
#include <util/string_container.h>
2122

2223
#include <langapi/language_util.h>
2324
#include <langapi/mode.h>
@@ -65,6 +66,15 @@ std::string graphml_witnesst::convert_assign_rec(
6566
const irep_idt &identifier,
6667
const code_assignt &assign)
6768
{
69+
#ifdef USE_DSTRING
70+
const auto cit = cache.find({identifier.get_no(), &assign.read()});
71+
#else
72+
const auto cit =
73+
cache.find({get_string_container()[id2string(identifier)], &assign.read()});
74+
#endif
75+
if(cit != cache.end())
76+
return cit->second;
77+
6878
std::string result;
6979

7080
if(assign.rhs().id() == ID_array_list)
@@ -197,6 +207,12 @@ std::string graphml_witnesst::convert_assign_rec(
197207
result = lhs + " = " + expr_to_string(ns, identifier, clean_rhs) + ";";
198208
}
199209

210+
#ifdef USE_DSTRING
211+
cache.insert({{identifier.get_no(), &assign.read()}, result});
212+
#else
213+
cache.insert(
214+
{{get_string_container()[id2string(identifier)], &assign.read()}, result});
215+
#endif
200216
return result;
201217
}
202218

src/goto-programs/graphml_witness.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,30 @@ class graphml_witnesst
4242
std::string convert_assign_rec(
4343
const irep_idt &identifier,
4444
const code_assignt &assign);
45+
46+
template <typename T>
47+
static void hash_combine(std::size_t &seed, const T &v)
48+
{
49+
std::hash<T> hasher;
50+
seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
51+
}
52+
53+
template <typename S, typename T>
54+
struct pair_hash // NOLINT(readability/identifiers)
55+
{
56+
std::size_t operator()(const std::pair<S, T> &v) const
57+
{
58+
std::size_t seed = 0;
59+
hash_combine(seed, v.first);
60+
hash_combine(seed, v.second);
61+
return seed;
62+
}
63+
};
64+
std::unordered_map<
65+
std::pair<unsigned int, const irept::dt *>,
66+
std::string,
67+
pair_hash<unsigned int, const irept::dt *>>
68+
cache;
4569
};
4670

4771
#endif // CPROVER_GOTO_PROGRAMS_GRAPHML_WITNESS_H

0 commit comments

Comments
 (0)