Skip to content

Commit 3924933

Browse files
committed
Pass-by-value in *typet constructors
Profiling does not show any advantage of using rvalue constructors over passing by value (and then moving in place), and this requires at most as many copies as the previous const-reference case.
1 parent c267421 commit 3924933

File tree

2 files changed

+22
-33
lines changed

2 files changed

+22
-33
lines changed

src/util/std_types.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ const struct_tag_typet &struct_typet::baset::type() const
8080
return to_struct_tag_type(exprt::type());
8181
}
8282

83-
struct_typet::baset::baset(const struct_tag_typet &base) : exprt(ID_base, base)
83+
struct_typet::baset::baset(struct_tag_typet base)
84+
: exprt(ID_base, std::move(base))
8485
{
8586
}
8687

src/util/std_types.h

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ class struct_union_typet:public typet
6969
public:
7070
componentt() = default;
7171

72-
componentt(const irep_idt &_name, const typet &_type)
72+
componentt(const irep_idt &_name, typet _type)
7373
{
7474
set_name(_name);
75-
type()=_type;
75+
type().swap(_type);
7676
}
7777

7878
const irep_idt &get_name() const
@@ -148,8 +148,7 @@ class struct_union_typet:public typet
148148

149149
typedef std::vector<componentt> componentst;
150150

151-
struct_union_typet(const irep_idt &_id, componentst &&_components)
152-
: typet(_id)
151+
struct_union_typet(const irep_idt &_id, componentst _components) : typet(_id)
153152
{
154153
components() = std::move(_components);
155154
}
@@ -244,7 +243,7 @@ class struct_typet:public struct_union_typet
244243
{
245244
}
246245

247-
explicit struct_typet(componentst &&_components)
246+
explicit struct_typet(componentst _components)
248247
: struct_union_typet(ID_struct, std::move(_components))
249248
{
250249
}
@@ -263,7 +262,7 @@ class struct_typet:public struct_union_typet
263262
public:
264263
struct_tag_typet &type();
265264
const struct_tag_typet &type() const;
266-
explicit baset(const struct_tag_typet &base);
265+
explicit baset(struct_tag_typet base);
267266
};
268267

269268
typedef std::vector<baset> basest;
@@ -398,7 +397,7 @@ class union_typet:public struct_union_typet
398397
{
399398
}
400399

401-
explicit union_typet(componentst &&_components)
400+
explicit union_typet(componentst _components)
402401
: struct_union_typet(ID_union, std::move(_components))
403402
{
404403
}
@@ -621,8 +620,8 @@ inline enumeration_typet &to_enumeration_type(typet &type)
621620
class c_enum_typet:public type_with_subtypet
622621
{
623622
public:
624-
explicit c_enum_typet(const typet &_subtype):
625-
type_with_subtypet(ID_c_enum, _subtype)
623+
explicit c_enum_typet(typet _subtype)
624+
: type_with_subtypet(ID_c_enum, std::move(_subtype))
626625
{
627626
}
628627

@@ -743,22 +742,12 @@ class code_typet:public typet
743742
/// Constructs a new code type, i.e., function type.
744743
/// \param _parameters: The vector of function parameters.
745744
/// \param _return_type: The return type.
746-
code_typet(parameterst &&_parameters, typet &&_return_type) : typet(ID_code)
745+
code_typet(parameterst _parameters, typet _return_type) : typet(ID_code)
747746
{
748747
parameters().swap(_parameters);
749748
return_type().swap(_return_type);
750749
}
751750

752-
/// Constructs a new code type, i.e., function type.
753-
/// \param _parameters: The vector of function parameters.
754-
/// \param _return_type: The return type.
755-
code_typet(parameterst &&_parameters, const typet &_return_type)
756-
: typet(ID_code)
757-
{
758-
parameters().swap(_parameters);
759-
return_type() = _return_type;
760-
}
761-
762751
/// \deprecated
763752
DEPRECATED("Use the two argument constructor instead")
764753
code_typet():typet(ID_code)
@@ -986,11 +975,10 @@ inline code_typet &to_code_type(typet &type)
986975
class array_typet:public type_with_subtypet
987976
{
988977
public:
989-
array_typet(
990-
const typet &_subtype,
991-
const exprt &_size):type_with_subtypet(ID_array, _subtype)
978+
array_typet(typet _subtype, exprt _size)
979+
: type_with_subtypet(ID_array, std::move(_subtype))
992980
{
993-
size()=_size;
981+
add(ID_size, std::move(_size));
994982
}
995983

996984
const exprt &size() const
@@ -1458,10 +1446,10 @@ inline floatbv_typet &to_floatbv_type(typet &type)
14581446
class c_bit_field_typet:public bitvector_typet
14591447
{
14601448
public:
1461-
explicit c_bit_field_typet(const typet &_subtype, std::size_t width)
1449+
explicit c_bit_field_typet(typet _subtype, std::size_t width)
14621450
: bitvector_typet(ID_c_bit_field, width)
14631451
{
1464-
subtype() = _subtype;
1452+
subtype().swap(_subtype);
14651453
}
14661454

14671455
// These have a sub-type
@@ -1503,10 +1491,10 @@ inline c_bit_field_typet &to_c_bit_field_type(typet &type)
15031491
class pointer_typet:public bitvector_typet
15041492
{
15051493
public:
1506-
pointer_typet(const typet &_subtype, std::size_t width)
1494+
pointer_typet(typet _subtype, std::size_t width)
15071495
: bitvector_typet(ID_pointer, width)
15081496
{
1509-
subtype() = _subtype;
1497+
subtype().swap(_subtype);
15101498
}
15111499

15121500
signedbv_typet difference_type() const
@@ -1561,8 +1549,8 @@ inline pointer_typet &to_pointer_type(typet &type)
15611549
class reference_typet:public pointer_typet
15621550
{
15631551
public:
1564-
reference_typet(const typet &_subtype, std::size_t _width):
1565-
pointer_typet(_subtype, _width)
1552+
reference_typet(typet _subtype, std::size_t _width)
1553+
: pointer_typet(std::move(_subtype), _width)
15661554
{
15671555
set(ID_C_reference, true);
15681556
}
@@ -1798,8 +1786,8 @@ class complex_typet:public type_with_subtypet
17981786
{
17991787
}
18001788

1801-
explicit complex_typet(const typet &_subtype):
1802-
type_with_subtypet(ID_complex, _subtype)
1789+
explicit complex_typet(typet _subtype)
1790+
: type_with_subtypet(ID_complex, std::move(_subtype))
18031791
{
18041792
}
18051793
};

0 commit comments

Comments
 (0)