From cc6c5e31952fbfb69c2e37060b16cff1acb90fd0 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Mon, 30 Mar 2026 12:20:12 -0700 Subject: [PATCH] Add contains() to custom containers and use where possible. NFC This matches the C++20 API that we cannot quite use yet. At least not until #8218 lands. --- src/passes/DeadArgumentElimination2.cpp | 4 ++-- src/passes/TypeMerging.cpp | 4 ++-- src/support/insert_ordered.h | 2 ++ src/support/small_set.h | 12 ++++++------ src/wasm/parsing.cpp | 2 +- test/example/small_set.cpp | 26 ++++++++++++------------- 6 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/passes/DeadArgumentElimination2.cpp b/src/passes/DeadArgumentElimination2.cpp index 91a4337a2d2..c52b69a37c9 100644 --- a/src/passes/DeadArgumentElimination2.cpp +++ b/src/passes/DeadArgumentElimination2.cpp @@ -1496,7 +1496,7 @@ void DAE2::collectStats() { if (auto* loc = std::get_if(&node)) { auto [funcIndex, paramIndex] = *loc; for (auto loc : parent.funcInfos[funcIndex].callerParams[paramIndex]) { - if (optimizedNodes.count(loc)) { + if (optimizedNodes.contains(loc)) { push(loc); } } @@ -1505,7 +1505,7 @@ void DAE2::collectStats() { if (auto it = parent.typeTreeInfos.find(funcType); it != parent.typeTreeInfos.end()) { for (auto loc : it->second.callerParams[paramIndex]) { - if (optimizedNodes.count(loc)) { + if (optimizedNodes.contains(loc)) { push(loc); } } diff --git a/src/passes/TypeMerging.cpp b/src/passes/TypeMerging.cpp index 5ca40ca687c..03ca4fa1010 100644 --- a/src/passes/TypeMerging.cpp +++ b/src/passes/TypeMerging.cpp @@ -382,7 +382,7 @@ bool TypeMerging::merge(MergeKind kind) { auto chain = type.getDescriptorChain(); bool hasCast = std::any_of(chain.begin(), chain.end(), [&](HeapType t) -> bool { - return castTypes.count(t); + return castTypes.contains(t); }); if (hasCast || !privateTypes.count(type)) { ensurePartition(type); @@ -396,7 +396,7 @@ bool TypeMerging::merge(MergeKind kind) { super && std::any_of(chain.begin(), chain.end(), [&](HeapType t) -> bool { auto super = t.getDeclaredSuperType(); - return super && exactCastTypes.count(*super); + return super && exactCastTypes.contains(*super); }); if (!super || !shapeEq(type, *super) || superHasExactCast) { // Create a new partition for this type and bail. diff --git a/src/support/insert_ordered.h b/src/support/insert_ordered.h index 374278918e6..58b189859b0 100644 --- a/src/support/insert_ordered.h +++ b/src/support/insert_ordered.h @@ -74,6 +74,7 @@ template struct InsertOrderedSet { } size_t count(const T& val) const { return Map.count(val); } + bool contains(const T& val) const { return Map.find(val) != Map.end(); } InsertOrderedSet() = default; InsertOrderedSet(const InsertOrderedSet& other) { *this = other; } @@ -160,6 +161,7 @@ template struct InsertOrderedMap { size_t size() const { return Map.size(); } bool empty() const { return Map.empty(); } size_t count(const Key& k) const { return Map.count(k); } + bool contains(const Key& k) const { return Map.find(k) != Map.end(); } InsertOrderedMap() = default; InsertOrderedMap(const InsertOrderedMap& other) { diff --git a/src/support/small_set.h b/src/support/small_set.h index 7f441ab1315..3da991bf462 100644 --- a/src/support/small_set.h +++ b/src/support/small_set.h @@ -185,17 +185,17 @@ class SmallSetBase { } } - size_t count(const T& x) const { + bool contains(const T& x) const { if (usingFixed()) { // Do a linear search. for (size_t i = 0; i < fixed.used; i++) { if (fixed.storage[i] == x) { - return 1; + return true; } } - return 0; + return false; } else { - return flexible.count(x); + return flexible.find(x) != flexible.end(); } } @@ -222,11 +222,11 @@ class SmallSetBase { if (usingFixed()) { return std::all_of(fixed.storage.begin(), fixed.storage.begin() + fixed.used, - [&other](const T& x) { return other.count(x); }); + [&other](const T& x) { return other.contains(x); }); } else if (other.usingFixed()) { return std::all_of(other.fixed.storage.begin(), other.fixed.storage.begin() + other.fixed.used, - [this](const T& x) { return count(x); }); + [this](const T& x) { return contains(x); }); } else { return flexible == other.flexible; } diff --git a/src/wasm/parsing.cpp b/src/wasm/parsing.cpp index 5d34da78e8a..f2c90fe8696 100644 --- a/src/wasm/parsing.cpp +++ b/src/wasm/parsing.cpp @@ -115,7 +115,7 @@ struct DuplicateNameScanner // TODO: This could be done in a single insert operation that checks // whether we actually inserted, if we improved // SmallSetBase::insert to return a value like std::set does. - if (seen.count(name)) { + if (seen.contains(name)) { // A name has been defined more than once; we'll need to fix that. ok = false; } else { diff --git a/test/example/small_set.cpp b/test/example/small_set.cpp index f1130c5af20..47653393531 100644 --- a/test/example/small_set.cpp +++ b/test/example/small_set.cpp @@ -11,7 +11,7 @@ template void assertContents(T& t, const std::vector& expectedContents) { assert(t.size() == expectedContents.size()); for (auto item : expectedContents) { - assert(t.count(item) == 1); + assert(t.contains(item)); } // Also test this using an iterator and a const iterator to also get // coverage there. @@ -69,29 +69,29 @@ template void testAPI() { assert(t.size() == 3); // unwind by erasing (in the opposite direction from before) - assert(t.count(1) == 1); - assert(t.count(2) == 1); - assert(t.count(3) == 1); - assert(t.count(1337) == 0); + assert(t.contains(1)); + assert(t.contains(2)); + assert(t.contains(3)); + assert(!t.contains(1337)); t.erase(1); - assert(t.count(1) == 0); + assert(!t.contains(1)); assert(t.size() == 2); - assert(t.count(2) == 1); + assert(t.contains(2)); t.erase(2); - assert(t.count(2) == 0); + assert(!t.contains(2)); assert(t.size() == 1); - assert(t.count(3) == 1); + assert(t.contains(3)); t.erase(3); - assert(t.count(1) == 0); - assert(t.count(2) == 0); - assert(t.count(3) == 0); - assert(t.count(1337) == 0); + assert(!t.contains(1)); + assert(!t.contains(2)); + assert(!t.contains(3)); + assert(!t.contains(1337)); assert(t.size() == 0); }