Skip to content

Commit 85728e0

Browse files
authored
Merge pull request #6055 from tautschnig/short-lived
Avoid short-lived heap-allocated objects
2 parents 6157aae + f287288 commit 85728e0

File tree

9 files changed

+33
-25
lines changed

9 files changed

+33
-25
lines changed

src/goto-symex/field_sensitivity.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,10 @@ exprt field_sensitivityt::get_fields(
170170

171171
for(const auto &comp : components)
172172
{
173-
const member_exprt member(struct_op, comp.get_name(), comp.type());
174173
ssa_exprt tmp = ssa_expr;
175174
bool was_l2 = !tmp.get_level_2().empty();
176175
tmp.remove_level_2();
177-
tmp.set_expression(member);
176+
tmp.set_expression(member_exprt{struct_op, comp.get_name(), comp.type()});
178177
if(was_l2)
179178
{
180179
fields.push_back(state.rename(get_fields(ns, state, tmp), ns).get());

src/solvers/flattening/boolbv.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,7 @@ bvt boolbvt::conversion_failed(const exprt &expr)
100100
bvt boolbvt::convert_bitvector(const exprt &expr)
101101
{
102102
if(expr.type().id()==ID_bool)
103-
{
104-
bvt bv;
105-
bv.resize(1);
106-
bv[0]=convert(expr);
107-
return bv;
108-
}
103+
return {convert(expr)};
109104

110105
if(expr.id()==ID_index)
111106
return convert_index(to_index_expr(expr));

src/solvers/sat/cnf.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -412,14 +412,10 @@ bvt cnft::new_variables(std::size_t width)
412412
/// \return set of literals, duplicates removed
413413
bvt cnft::eliminate_duplicates(const bvt &bv)
414414
{
415-
std::set<literalt> s;
416-
417-
bvt dest;
418-
dest.reserve(bv.size());
419-
420-
for(const auto &l : bv)
421-
if(s.insert(l).second)
422-
dest.push_back(l);
415+
bvt dest = bv;
416+
std::sort(dest.begin(), dest.end());
417+
auto last = std::unique(dest.begin(), dest.end());
418+
dest.erase(last, dest.end());
423419

424420
return dest;
425421
}

src/util/pointer_expr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ address_of_exprt::address_of_exprt(const exprt &_op)
122122
{
123123
}
124124

125-
const exprt &object_descriptor_exprt::root_object() const
125+
const exprt &object_descriptor_exprt::root_object(const exprt &expr)
126126
{
127-
const exprt *p = &object();
127+
const exprt *p = &expr;
128128

129129
while(true)
130130
{

src/util/pointer_expr.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,11 @@ class object_descriptor_exprt : public binary_exprt
180180
return op0();
181181
}
182182

183-
const exprt &root_object() const;
183+
static const exprt &root_object(const exprt &expr);
184+
const exprt &root_object() const
185+
{
186+
return root_object(object());
187+
}
184188

185189
exprt &offset()
186190
{

src/util/sharing_node.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,13 @@ SN_TYPE_PAR_DEF class sharing_nodet
346346
const to_mapt &m = get_to_map();
347347
typename to_mapt::const_iterator it = m.find(n);
348348

349+
#if SN_SMALL_MAP == 1
350+
if(it != m.end())
351+
return &it.get_value();
352+
#else
349353
if(it != m.end())
350354
return &it->second;
355+
#endif
351356

352357
return nullptr;
353358
}

src/util/small_map.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,15 @@ class small_mapt
265265
return std::make_shared<valuet>(idx, *(m.p + ii));
266266
}
267267

268+
/// Non-standard direct access to the underlying value instead of creating a
269+
/// \c valuet object. Avoids creating a very short-lived \c valuet object
270+
/// when only the value is needed.
271+
/// \return Reference to value stored in the map.
272+
const T &get_value() const
273+
{
274+
return *(m.p + ii);
275+
}
276+
268277
const_iterator operator++()
269278
{
270279
idx++;

src/util/ssa_expr.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ static void update_identifier(ssa_exprt &ssa)
134134
ssa.set(ID_L1_object_identifier, idpair.second);
135135
}
136136

137-
void ssa_exprt::set_expression(const exprt &expr)
137+
void ssa_exprt::set_expression(exprt expr)
138138
{
139-
type() = expr.type();
140-
add(ID_expression, expr);
139+
type() = as_const(expr).type();
140+
add(ID_expression, std::move(expr));
141141
::update_identifier(*this);
142142
}
143143

@@ -148,8 +148,8 @@ irep_idt ssa_exprt::get_object_name() const
148148
if(original_expr.id() == ID_symbol)
149149
return to_symbol_expr(original_expr).get_identifier();
150150

151-
object_descriptor_exprt ode(original_expr);
152-
return to_symbol_expr(ode.root_object()).get_identifier();
151+
return to_symbol_expr(object_descriptor_exprt::root_object(original_expr))
152+
.get_identifier();
153153
}
154154

155155
const ssa_exprt ssa_exprt::get_l1_object() const

src/util/ssa_expr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class ssa_exprt:public symbol_exprt
3838
/// Replace the underlying, original expression by \p expr while maintaining
3939
/// SSA indices.
4040
/// \param expr: expression to store
41-
void set_expression(const exprt &expr);
41+
void set_expression(exprt expr);
4242

4343
irep_idt get_object_name() const;
4444

0 commit comments

Comments
 (0)