[llvm] [ADT] Make set_subtract more efficient when subtrahend is larger (NFC) (PR #98702)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 15 11:46:26 PDT 2024


================
@@ -94,7 +94,24 @@ S1Ty set_difference(const S1Ty &S1, const S2Ty &S2) {
 
 /// set_subtract(A, B) - Compute A := A - B
 ///
+/// Selects the set to iterate based on the relative sizes of A and B for better
+/// efficiency.
+///
 template <class S1Ty, class S2Ty> void set_subtract(S1Ty &S1, const S2Ty &S2) {
+  using ElemTy = decltype(*S1.begin());
+  // A couple callers pass a vector for S2, which doesn't support count(), and
+  // wouldn't be efficient if it did. In the absence of a more direct check,
+  // ensure the type supports the contains or find interfaces.
+  if constexpr (detail::HasMemberContains<S2Ty, ElemTy> ||
+                detail::HasMemberFind<S2Ty, ElemTy>) {
----------------
kazutakahirata wrote:

I think the `detail::HasMemberContains` and `detail::HasMemberFind` checks should go with `is_contained` in place of `count` below.  This way, `is_contained` will pick `C.contains(E)` or `C.find(E) != C.end()` for you depending on the availability.

https://github.com/llvm/llvm-project/pull/98702


More information about the llvm-commits mailing list