Skip to content

[ADT] Restore handwritten vector find in SmallSet #110254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions llvm/include/llvm/ADT/SmallSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class SmallSet {
bool erase(const T &V) {
if (!isSmall())
return Set.erase(V);
auto I = std::find(Vector.begin(), Vector.end(), V);
auto I = vfind(V);
if (I != Vector.end()) {
Vector.erase(I);
return true;
Expand Down Expand Up @@ -221,7 +221,7 @@ class SmallSet {
/// Check if the SmallSet contains the given element.
bool contains(const T &V) const {
if (isSmall())
return std::find(Vector.begin(), Vector.end(), V) != Vector.end();
return vfind(V) != Vector.end();
return Set.find(V) != Set.end();
}

Expand All @@ -237,20 +237,28 @@ class SmallSet {
return {const_iterator(I), Inserted};
}

auto I = std::find(Vector.begin(), Vector.end(), V);
auto I = vfind(V);
if (I != Vector.end()) // Don't reinsert if it already exists.
return {const_iterator(I), false};
if (Vector.size() < N) {
Vector.push_back(std::forward<ArgType>(V));
return {const_iterator(std::prev(Vector.end())), true};
}

// Otherwise, grow from vector to set.
Set.insert(std::make_move_iterator(Vector.begin()),
std::make_move_iterator(Vector.end()));
Vector.clear();
return {const_iterator(Set.insert(std::forward<ArgType>(V)).first), true};
}

// Handwritten linear search. The use of std::find might hurt performance as
// its implementation may be optimized for larger containers.
typename SmallVector<T, N>::const_iterator vfind(const T &V) const {
for (auto I = Vector.begin(), E = Vector.end(); I != E; ++I)
if (*I == V)
return I;
return Vector.end();
}
};

/// If this set is of pointer values, transparently switch over to using
Expand Down
Loading