Skip to content

Commit 795cf7f

Browse files
authored
Merge pull request #4163 from tautschnig/irept-deserialisation
irept deserialisation: avoid unnecessary construct/destruct
2 parents 995667e + ba4a005 commit 795cf7f

File tree

3 files changed

+44
-31
lines changed

3 files changed

+44
-31
lines changed

src/goto-programs/read_bin_goto_object.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ static bool read_bin_goto_object(
3636
{
3737
symbolt sym;
3838

39-
irepconverter.reference_convert(in, sym.type);
40-
irepconverter.reference_convert(in, sym.value);
41-
irepconverter.reference_convert(in, sym.location);
39+
sym.type = static_cast<const typet &>(irepconverter.reference_convert(in));
40+
sym.value = static_cast<const exprt &>(irepconverter.reference_convert(in));
41+
sym.location = static_cast<const source_locationt &>(
42+
irepconverter.reference_convert(in));
4243

4344
sym.name = irepconverter.read_string_ref(in);
4445
sym.module = irepconverter.read_string_ref(in);
@@ -100,12 +101,14 @@ static bool read_bin_goto_object(
100101
goto_programt::targett itarget = f.body.add_instruction();
101102
goto_programt::instructiont &instruction=*itarget;
102103

103-
irepconverter.reference_convert(in, instruction.code);
104-
irepconverter.reference_convert(in, instruction.source_location);
104+
instruction.code =
105+
static_cast<const codet &>(irepconverter.reference_convert(in));
106+
instruction.source_location = static_cast<const source_locationt &>(
107+
irepconverter.reference_convert(in));
105108
instruction.type = (goto_program_instruction_typet)
106109
irepconverter.read_gb_word(in);
107-
instruction.guard.make_nil();
108-
irepconverter.reference_convert(in, instruction.guard);
110+
instruction.guard =
111+
static_cast<const exprt &>(irepconverter.reference_convert(in));
109112
instruction.target_number = irepconverter.read_gb_word(in);
110113
if(instruction.is_target() &&
111114
rev_target_map.insert(

src/util/irep_serialization.cpp

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,15 @@ void irep_serializationt::write_irep(
4141
out.put(0); // terminator
4242
}
4343

44-
void irep_serializationt::reference_convert(
45-
std::istream &in,
46-
irept &irep)
44+
const irept &irep_serializationt::reference_convert(std::istream &in)
4745
{
4846
std::size_t id=read_gb_word(in);
4947

50-
if(id<ireps_container.ireps_on_read.size() &&
51-
ireps_container.ireps_on_read[id].first)
52-
{
53-
irep=ireps_container.ireps_on_read[id].second;
54-
}
55-
else
48+
if(
49+
id >= ireps_container.ireps_on_read.size() ||
50+
!ireps_container.ireps_on_read[id].first)
5651
{
57-
read_irep(in, irep);
52+
irept irep = read_irep(in);
5853

5954
if(id >= ireps_container.ireps_on_read.size())
6055
ireps_container.ireps_on_read.resize(1 + id * 2, {false, get_nil_irep()});
@@ -63,42 +58,57 @@ void irep_serializationt::reference_convert(
6358
if(ireps_container.ireps_on_read[id].first)
6459
throw deserialization_exceptiont("irep id read twice.");
6560

66-
ireps_container.ireps_on_read[id] = {true, irep};
61+
ireps_container.ireps_on_read[id] = {true, std::move(irep)};
6762
}
63+
64+
return ireps_container.ireps_on_read[id].second;
6865
}
6966

70-
void irep_serializationt::read_irep(
71-
std::istream &in,
72-
irept &irep)
67+
irept irep_serializationt::read_irep(std::istream &in)
7368
{
74-
irep.clear();
75-
irep.id(read_string_ref(in));
69+
irep_idt id = read_string_ref(in);
70+
irept::subt sub;
71+
irept::named_subt named_sub;
7672

7773
while(in.peek()=='S')
7874
{
7975
in.get();
80-
irep.get_sub().push_back(irept());
81-
reference_convert(in, irep.get_sub().back());
76+
sub.push_back(reference_convert(in));
8277
}
8378

79+
#ifdef NAMED_SUB_IS_FORWARD_LIST
80+
irept::named_subt::iterator before = named_sub.before_begin();
81+
#endif
8482
while(in.peek()=='N')
8583
{
8684
in.get();
87-
irept &r=irep.add(read_string_ref(in));
88-
reference_convert(in, r);
85+
irep_idt id = read_string_ref(in);
86+
#ifdef NAMED_SUB_IS_FORWARD_LIST
87+
named_sub.emplace_after(before, id, reference_convert(in));
88+
++before;
89+
#else
90+
named_sub.emplace(id, reference_convert(in));
91+
#endif
8992
}
9093

9194
while(in.peek()=='C')
9295
{
9396
in.get();
94-
irept &r=irep.add(read_string_ref(in));
95-
reference_convert(in, r);
97+
irep_idt id = read_string_ref(in);
98+
#ifdef NAMED_SUB_IS_FORWARD_LIST
99+
named_sub.emplace_after(before, id, reference_convert(in));
100+
++before;
101+
#else
102+
named_sub.emplace(id, reference_convert(in));
103+
#endif
96104
}
97105

98106
if(in.get()!=0)
99107
{
100108
throw deserialization_exceptiont("irep not terminated");
101109
}
110+
111+
return irept(std::move(id), std::move(named_sub), std::move(sub));
102112
}
103113

104114
/// Serialize an irept

src/util/irep_serialization.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class irep_serializationt
6161
clear();
6262
};
6363

64-
void reference_convert(std::istream &, irept &irep);
64+
const irept &reference_convert(std::istream &);
6565
void reference_convert(const irept &irep, std::ostream &);
6666

6767
irep_idt read_string_ref(std::istream &);
@@ -77,7 +77,7 @@ class irep_serializationt
7777
std::vector<char> read_buffer;
7878

7979
void write_irep(std::ostream &, const irept &irep);
80-
void read_irep(std::istream &, irept &irep);
80+
irept read_irep(std::istream &);
8181
};
8282

8383
#endif // CPROVER_UTIL_IREP_SERIALIZATION_H

0 commit comments

Comments
 (0)