Skip to content

Commit 65d8504

Browse files
author
David Ungar
committed
WIP non alloc
1 parent 59fc0c2 commit 65d8504

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

include/swift/Driver/SourceComparator.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class SourceComparator {
9797
struct LineIndices : public Indices<LineIndices> {
9898
LineIndices() : Indices(~0, ~0) {}
9999
LineIndices(size_t lhs, size_t rhs) : Indices(lhs, rhs) {}
100+
LineIndices & operator=(const LineIndices&) = default;
100101
LineIndices &operator++() {
101102
++lhs(), ++rhs();
102103
return *this;
@@ -211,9 +212,10 @@ class SourceComparator {
211212

212213
/// Chains matching lines together (I think)
213214
struct PRLink {
214-
const LineIndices lines;
215-
const PRLink *const next;
216-
PRLink(LineIndices lines, const PRLink *next) : lines(lines), next(next) {}
215+
LineIndices lines;
216+
Optional<size_t> next;
217+
PRLink(LineIndices lines, const Optional<size_t> next) : lines(lines), next(next) {}
218+
PRLink() : PRLink(LineIndices(), None) {}
217219
};
218220

219221

@@ -269,13 +271,12 @@ class SourceComparator {
269271
std::unordered_map<std::string, std::vector<size_t>>
270272
buildEquivalenceClasses();
271273

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

274-
std::pair<std::vector<std::unique_ptr<PRLink>>, SortedSequence>
275+
std::pair<std::vector<PRLink>, SortedSequence>
275276
buildDAGOfSubsequences(
276277
std::unordered_map<std::string, std::vector<size_t>> rhsMap);
277278

278-
void scanMatchedLines(std::pair<std::vector<std::unique_ptr<PRLink>>,
279+
void scanMatchedLines(std::pair<std::vector<PRLink>,
279280
SortedSequence> &&linksAndThres);
280281

281282
//==============================================================================

lib/Driver/SourceComparator.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,14 @@ SourceComparator::buildEquivalenceClasses() {
7474
return rhsMap;
7575
}
7676

77-
std::unique_ptr<SourceComparator::PRLink>
78-
SourceComparator::newPRLink(LineIndices lines, PRLink *next) {
79-
return llvm::make_unique<PRLink>(lines, next);
80-
}
8177

82-
std::pair<std::vector<std::unique_ptr<SourceComparator::PRLink>>,
78+
std::pair<std::vector<SourceComparator::PRLink>,
8379
SourceComparator::SortedSequence>
8480
SourceComparator::buildDAGOfSubsequences(
8581
std::unordered_map<std::string, std::vector<size_t>> rhsMap) {
8682
SortedSequence thresh;
8783
const size_t linksSize = regionsToCompare.size().min();
88-
std::vector<std::unique_ptr<PRLink>> links;
84+
std::vector<PRLink> links;
8985
links.resize(linksSize);
9086

9187
for (auto i = regionsToCompare.start.lhs(); i < regionsToCompare.end.lhs();
@@ -108,25 +104,27 @@ SourceComparator::buildDAGOfSubsequences(
108104
auto k = optK.getValue();
109105
// prev match (of what?) was at links[k-1]?
110106
// chain to that match
111-
PRLink *newNext = k == 0 ? nullptr : links[k - 1].get();
112-
links[k] = newPRLink(LineIndices(i, j), newNext);
107+
Optional<size_t> newNext = k == 0 ? None : Optional<size_t>(k - 1);
108+
links.emplace(links.begin() + k, LineIndices(i, j), newNext);
113109
}
114110
}
115111
}
116112
return {std::move(links), thresh};
117113
}
118114

119115
void SourceComparator::scanMatchedLines(
120-
std::pair<std::vector<std::unique_ptr<PRLink>>, SortedSequence>
116+
std::pair<std::vector<PRLink>, SortedSequence>
121117
&&linksAndThres) {
122118
const auto &links = linksAndThres.first;
123119
const auto &thresh = linksAndThres.second;
124120

121+
if (thresh.empty())
122+
return;
125123
// For every match put rhs index in matches[col2 index]
126-
for (const PRLink *lnk = thresh.empty() ? nullptr
127-
: links[thresh.size() - 1].get();
128-
lnk; lnk = lnk->next)
129-
matches[lnk->lines.lhs()] = lnk->lines.rhs();
124+
for (const auto *p = &links[thresh.size() - 1];
125+
p;
126+
p = p->next ? &links[p->next.getValue()] : nullptr)
127+
matches[p->lines.lhs()] = p->lines.rhs();
130128
}
131129

132130
//==============================================================================

0 commit comments

Comments
 (0)