Skip to content

Commit 59fc0c2

Browse files
author
David Ungar
committed
Heap-alloc PRLinks
1 parent 46ad660 commit 59fc0c2

File tree

2 files changed

+24
-31
lines changed

2 files changed

+24
-31
lines changed

include/swift/Driver/SourceComparator.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,11 @@ class SourceComparator {
211211

212212
/// Chains matching lines together (I think)
213213
struct PRLink {
214-
LineIndices lines;
215-
PRLink *next;
214+
const LineIndices lines;
215+
const PRLink *const next;
216+
PRLink(LineIndices lines, const PRLink *next) : lines(lines), next(next) {}
216217
};
217218

218-
/// An allocation pool for the PRLinks.
219-
std::vector<PRLink> linkVec;
220-
/// Next link to allocate in the linkVec
221-
size_t nextLink = 0;
222219

223220
//==============================================================================
224221
// MARK: SortedSequence
@@ -272,13 +269,14 @@ class SourceComparator {
272269
std::unordered_map<std::string, std::vector<size_t>>
273270
buildEquivalenceClasses();
274271

275-
PRLink *newPRLink(LineIndices, PRLink *next);
272+
std::unique_ptr<PRLink> newPRLink(LineIndices, PRLink *next);
276273

277-
std::pair<std::vector<PRLink *>, SortedSequence> buildDAGOfSubsequences(
274+
std::pair<std::vector<std::unique_ptr<PRLink>>, SortedSequence>
275+
buildDAGOfSubsequences(
278276
std::unordered_map<std::string, std::vector<size_t>> rhsMap);
279277

280-
void scanMatchedLines(
281-
std::pair<std::vector<PRLink *>, SortedSequence> linksAndThres);
278+
void scanMatchedLines(std::pair<std::vector<std::unique_ptr<PRLink>>,
279+
SortedSequence> &&linksAndThres);
282280

283281
//==============================================================================
284282
// MARK: summarizing

lib/Driver/SourceComparator.cpp

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ using namespace incremental_ranges;
2929
SourceComparator::SourceComparator(StringRef s1, StringRef s2)
3030
: linesToCompare(splitIntoLines(s1), splitIntoLines(s2)),
3131
regionsToCompare(LineSpan({0, 0}, linesToCompare.size())),
32-
matches(linesToCompare.lhs().size(), None),
33-
linkVec(regionsToCompare.end.min(), PRLink()) {}
32+
matches(linesToCompare.lhs().size(), None) {}
3433

3534
std::vector<StringRef> SourceComparator::splitIntoLines(const StringRef s) {
3635
std::vector<StringRef> result;
@@ -75,24 +74,19 @@ SourceComparator::buildEquivalenceClasses() {
7574
return rhsMap;
7675
}
7776

78-
SourceComparator::PRLink *SourceComparator::newPRLink(LineIndices lines,
79-
PRLink *next) {
80-
if (nextLink >= linkVec.size())
81-
linkVec.emplace_back();
82-
assert(nextLink < linkVec.size());
83-
PRLink &link = linkVec[nextLink++];
84-
link.lines = lines;
85-
link.next = next;
86-
return &link;
77+
std::unique_ptr<SourceComparator::PRLink>
78+
SourceComparator::newPRLink(LineIndices lines, PRLink *next) {
79+
return llvm::make_unique<PRLink>(lines, next);
8780
}
8881

89-
std::pair<std::vector<SourceComparator::PRLink *>,
82+
std::pair<std::vector<std::unique_ptr<SourceComparator::PRLink>>,
9083
SourceComparator::SortedSequence>
9184
SourceComparator::buildDAGOfSubsequences(
9285
std::unordered_map<std::string, std::vector<size_t>> rhsMap) {
9386
SortedSequence thresh;
9487
const size_t linksSize = regionsToCompare.size().min();
95-
std::vector<PRLink *> links(linksSize, nullptr);
88+
std::vector<std::unique_ptr<PRLink>> links;
89+
links.resize(linksSize);
9690

9791
for (auto i = regionsToCompare.start.lhs(); i < regionsToCompare.end.lhs();
9892
++i) {
@@ -114,23 +108,24 @@ SourceComparator::buildDAGOfSubsequences(
114108
auto k = optK.getValue();
115109
// prev match (of what?) was at links[k-1]?
116110
// chain to that match
117-
PRLink *newNext = k == 0 ? nullptr : links[k - 1];
111+
PRLink *newNext = k == 0 ? nullptr : links[k - 1].get();
118112
links[k] = newPRLink(LineIndices(i, j), newNext);
119113
}
120114
}
121115
}
122-
return {links, thresh};
116+
return {std::move(links), thresh};
123117
}
124118

125119
void SourceComparator::scanMatchedLines(
126-
std::pair<std::vector<PRLink *>, SortedSequence> linksAndThres) {
127-
auto links = linksAndThres.first;
128-
auto thresh = linksAndThres.second;
120+
std::pair<std::vector<std::unique_ptr<PRLink>>, SortedSequence>
121+
&&linksAndThres) {
122+
const auto &links = linksAndThres.first;
123+
const auto &thresh = linksAndThres.second;
129124

130125
// For every match put rhs index in matches[col2 index]
131-
for (auto lnk = thresh.empty() ? nullptr : links[thresh.size() - 1];
132-
lnk;
133-
lnk = lnk->next)
126+
for (const PRLink *lnk = thresh.empty() ? nullptr
127+
: links[thresh.size() - 1].get();
128+
lnk; lnk = lnk->next)
134129
matches[lnk->lines.lhs()] = lnk->lines.rhs();
135130
}
136131

0 commit comments

Comments
 (0)