Skip to content

Commit 3d58e8f

Browse files
author
Dan Lambright
committed
use hash rather than manually sorted vector for _usedValues in selectReplicas
1 parent 2d5d695 commit 3d58e8f

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

fdbrpc/ReplicationPolicy.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ bool PolicyAcross::validate(std::vector<LocalityEntry> const& solutionSet,
177177
// Choose new servers from "least utilized" alsoServers and append the new servers to results
178178
// fromserverse are the servers that have already been chosen and
179179
// that should be excluded from being selected as replicas.
180-
// FIXME: Simplify this function, such as removing unnecessary printf
181180
// fromServers are the servers that must have;
182181
// alsoServers are the servers you can choose.
183182
bool PolicyAcross::selectReplicas(Reference<LocalitySet>& fromServers,
@@ -197,8 +196,7 @@ bool PolicyAcross::selectReplicas(Reference<LocalitySet>& fromServers,
197196
for (auto& alsoServer : alsoServers) {
198197
auto value = fromServers->getValueViaGroupKey(alsoServer, groupIndexKey);
199198
if (value.present()) {
200-
auto lowerBound = std::lower_bound(_usedValues.begin(), _usedValues.end(), value.get());
201-
if ((lowerBound == _usedValues.end()) || (*lowerBound != value.get())) {
199+
if (!_usedValues.contains(value.get())) {
202200
//_selected is a set of processes that have the same indexKey and indexValue (value)
203201
_selected = fromServers->restrict(indexKey, value.get());
204202
if (_selected->size()) {
@@ -213,7 +211,7 @@ bool PolicyAcross::selectReplicas(Reference<LocalitySet>& fromServers,
213211
}
214212
if (count >= _count)
215213
break;
216-
_usedValues.insert(lowerBound, value.get());
214+
_usedValues.insert(value.get());
217215
}
218216
}
219217
}
@@ -258,15 +256,14 @@ bool PolicyAcross::selectReplicas(Reference<LocalitySet>& fromServers,
258256
auto& entry = mutableArray[recordIndex];
259257
auto value = fromServers->getValueViaGroupKey(entry, groupIndexKey);
260258
if (value.present()) {
261-
auto lowerBound = std::lower_bound(_usedValues.begin(), _usedValues.end(), value.get());
262-
if ((lowerBound == _usedValues.end()) || (*lowerBound != value.get())) {
259+
if (!_usedValues.contains(value.get())) {
263260
_selected = fromServers->restrict(indexKey, value.get());
264261
if (_selected->size()) {
265262
if (_policy->selectReplicas(_selected, emptyEntryArray, results)) {
266263
count++;
267264
if (count >= _count)
268265
break;
269-
_usedValues.insert(lowerBound, value.get());
266+
_usedValues.insert(value.get());
270267
}
271268
}
272269
}

fdbrpc/include/fdbrpc/ReplicationPolicy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ struct PolicyAcross final : IReplicationPolicy {
161161
Reference<IReplicationPolicy> _policy;
162162

163163
// Cache temporary members
164-
std::vector<AttribValue> _usedValues;
164+
std::unordered_set<AttribValue> _usedValues;
165165
std::vector<LocalityEntry> _newResults;
166166
Reference<LocalitySet> _selected;
167167
VectorRef<std::pair<int, int>> _addedResults;

fdbrpc/include/fdbrpc/ReplicationTypes.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ struct AttribValue {
5454
bool operator<=(AttribValue const& source) const { return !(*this > source); }
5555
bool operator>=(AttribValue const& source) const { return !(*this < source); }
5656
};
57+
namespace std {
58+
template <>
59+
struct hash<AttribValue> {
60+
std::size_t operator()(const AttribValue& v) const noexcept {
61+
return std::hash<int>{}(v._id);
62+
}
63+
};
64+
}
5765
struct LocalityEntry {
5866
int _id;
5967
explicit LocalityEntry() : _id(-1) {}

0 commit comments

Comments
 (0)