Skip to content

Commit 2f1514e

Browse files
authored
Merge pull request #4155 from tautschnig/merge-irept-skip-same
merge_irept: insert once and then const_cast to edit in place
2 parents b43753a + 3b95d0a commit 2f1514e

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

src/util/merge_irep.cpp

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -145,37 +145,47 @@ void merge_irept::operator()(irept &irep)
145145

146146
const irept &merge_irept::merged(const irept &irep)
147147
{
148-
irep_storet::const_iterator entry=irep_store.find(irep);
149-
if(entry!=irep_store.end())
150-
return *entry;
151-
152-
irept new_irep(irep.id());
148+
auto entry = irep_store.insert(irep);
149+
if(!entry.second)
150+
return *entry.first;
153151

154152
const irept::subt &src_sub=irep.get_sub();
155-
irept::subt &dest_sub=new_irep.get_sub();
156-
dest_sub.reserve(src_sub.size());
153+
irept::subt *dest_sub_ptr = nullptr;
157154

155+
std::size_t index = 0;
158156
forall_irep(it, src_sub)
159-
dest_sub.push_back(merged(*it)); // recursive call
157+
{
158+
const irept &op = merged(*it); // recursive call
159+
if(&op.read() != &(it->read()))
160+
{
161+
if(!dest_sub_ptr)
162+
dest_sub_ptr = &(const_cast<irept &>(*entry.first)).get_sub();
163+
(*dest_sub_ptr)[index] = op;
164+
}
165+
++index;
166+
}
160167

161168
const irept::named_subt &src_named_sub=irep.get_named_sub();
162-
irept::named_subt &dest_named_sub=new_irep.get_named_sub();
169+
irept::named_subt *dest_named_sub_ptr = nullptr;
163170

164-
#ifdef NAMED_SUB_IS_FORWARD_LIST
165-
irept::named_subt::iterator before = dest_named_sub.before_begin();
166-
#endif
171+
std::ptrdiff_t advance_by = 0;
167172
forall_named_irep(it, src_named_sub)
168173
{
169-
#ifdef NAMED_SUB_IS_FORWARD_LIST
170-
dest_named_sub.emplace_after(
171-
before, it->first, merged(it->second)); // recursive call
172-
++before;
173-
#else
174-
dest_named_sub[it->first]=merged(it->second); // recursive call
175-
#endif
174+
if(!irept::is_comment(it->first))
175+
{
176+
const irept &op = merged(it->second); // recursive call
177+
if(&op.read() != &(it->second.read()))
178+
{
179+
if(!dest_named_sub_ptr)
180+
dest_named_sub_ptr =
181+
&(const_cast<irept &>(*entry.first)).get_named_sub();
182+
std::next(dest_named_sub_ptr->begin(), advance_by)->second = op;
183+
}
184+
}
185+
++advance_by;
176186
}
177187

178-
return *irep_store.insert(std::move(new_irep)).first;
188+
return *entry.first;
179189
}
180190

181191
void merge_full_irept::operator()(irept &irep)

0 commit comments

Comments
 (0)