Skip to content

Commit c31ab35

Browse files
committed
Avoid repeated lookups during irep serialisation
irep serialisation spends most of its time in numbering, which in turn spends this time in finding map entries. Make sure finding map entries happens only once per call to number().
1 parent c56da40 commit c31ab35

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

src/util/irep_hash_container.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,18 @@ size_t irep_hash_container_baset::number(const irept &irep)
1818
{
1919
// the ptr-hash provides a speedup of up to 3x
2020

21-
ptr_hasht::const_iterator it=ptr_hash.find(&irep.read());
21+
auto entry = ptr_hash.emplace(
22+
std::piecewise_construct,
23+
std::forward_as_tuple(&irep.read()),
24+
std::forward_as_tuple(0, irep));
2225

23-
if(it!=ptr_hash.end())
24-
return it->second.number;
26+
if(!entry.second && entry.first->second.final_number)
27+
return entry.first->second.number;
2528

26-
packedt packed;
27-
pack(irep, packed);
28-
size_t id=numbering.number(packed);
29+
const size_t id = numbering.number(pack(irep));
2930

30-
ptr_hash.emplace(
31-
std::piecewise_construct,
32-
std::forward_as_tuple(&irep.read()),
33-
std::forward_as_tuple(id, irep));
31+
entry.first->second.number = id;
32+
entry.first->second.final_number = true;
3433

3534
return id;
3635
}
@@ -44,9 +43,7 @@ size_t irep_hash_container_baset::vector_hasht::operator()(
4443
return result;
4544
}
4645

47-
void irep_hash_container_baset::pack(
48-
const irept &irep,
49-
packedt &packed)
46+
irep_hash_container_baset::packedt irep_hash_container_baset::pack(const irept &irep)
5047
{
5148
const irept::subt &sub=irep.get_sub();
5249
const irept::named_subt &named_sub=irep.get_named_sub();
@@ -61,7 +58,7 @@ void irep_hash_container_baset::pack(
6158
#else
6259
const std::size_t named_sub_size = named_sub.size();
6360
#endif
64-
packed.reserve(1 + 1 + sub.size() + 1 + named_sub_size * 2);
61+
packedt packed(1 + 1 + sub.size() + 1 + named_sub_size * 2);
6562

6663
packed.push_back(irep_id_hash()(irep.id()));
6764

@@ -75,6 +72,8 @@ void irep_hash_container_baset::pack(
7572
packed.push_back(irep_id_hash()(sub_irep.first)); // id
7673
packed.push_back(number(sub_irep.second)); // sub-irep
7774
}
75+
76+
return packed;
7877
}
7978
else
8079
{
@@ -83,7 +82,7 @@ void irep_hash_container_baset::pack(
8382

8483
// we pack: the irep id, the sub size, the subs, the named-sub size, and
8584
// each of the non-comment named subs with their ids
86-
packed.reserve(1 + 1 + sub.size() + 1 + non_comment_count * 2);
85+
packedt packed(1 + 1 + sub.size() + 1 + non_comment_count * 2);
8786

8887
packed.push_back(irep_id_hash()(irep.id()));
8988

@@ -98,5 +97,7 @@ void irep_hash_container_baset::pack(
9897
packed.push_back(irep_id_hash()(sub_irep.first)); // id
9998
packed.push_back(number(sub_irep.second)); // sub-irep
10099
}
100+
101+
return packed;
101102
}
102103
}

src/util/irep_hash_container.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class irep_hash_container_baset
4949
{
5050
std::size_t number;
5151
irept irep; // copy to keep addresses stable
52+
bool final_number = false;
5253

5354
irep_entryt(std::size_t _number, const irept &_irep)
5455
: number(_number), irep(_irep)
@@ -72,7 +73,7 @@ class irep_hash_container_baset
7273
typedef hash_numbering<packedt, vector_hasht> numberingt;
7374
numberingt numbering;
7475

75-
void pack(const irept &irep, packedt &);
76+
packedt pack(const irept &irep);
7677

7778
bool full;
7879
};

0 commit comments

Comments
 (0)