[llvm] 578ba5f - [ADT] Avoid map storage for small SmallMapVector (#196473)
via llvm-commits
llvm-commits at lists.llvm.org
Sun May 10 18:12:47 PDT 2026
Author: Mingjie Xu
Date: 2026-05-11T09:12:43+08:00
New Revision: 578ba5f3039e7a1e4e6f3a82891b3838823e37c2
URL: https://github.com/llvm/llvm-project/commit/578ba5f3039e7a1e4e6f3a82891b3838823e37c2
DIFF: https://github.com/llvm/llvm-project/commit/578ba5f3039e7a1e4e6f3a82891b3838823e37c2.diff
LOG: [ADT] Avoid map storage for small SmallMapVector (#196473)
SmallMapVector previously used SmallDenseMap for its index, which still
initializes and maintains map storage even when the number of entries is
tiny.
Teach MapVector to support a vector-only small mode. While the entry
count stays
within the configured small size, operations use the underlying vector
directly.
When the size grows past the threshold, the map index is built and
subsequent
operations use the regular MapVector path.
This mirrors the small-size strategy used by SmallSetVector.
Added:
Modified:
llvm/include/llvm/ADT/MapVector.h
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/MapVector.h b/llvm/include/llvm/ADT/MapVector.h
index 2b2f098dd3abf..8a7f5edf2f710 100644
--- a/llvm/include/llvm/ADT/MapVector.h
+++ b/llvm/include/llvm/ADT/MapVector.h
@@ -18,6 +18,7 @@
#define LLVM_ADT_MAPVECTOR_H
#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include <cassert>
#include <cstddef>
@@ -32,7 +33,8 @@ namespace llvm {
/// mapping is done with DenseMap from Keys to indexes in that vector.
template <typename KeyT, typename ValueT,
typename MapType = DenseMap<KeyT, unsigned>,
- typename VectorType = SmallVector<std::pair<KeyT, ValueT>, 0>>
+ typename VectorType = SmallVector<std::pair<KeyT, ValueT>, 0>,
+ unsigned N = 0>
class MapVector {
public:
using key_type = KeyT;
@@ -108,8 +110,8 @@ class MapVector {
[[nodiscard]] ValueT lookup(const KeyT &Key) const {
static_assert(std::is_copy_constructible_v<ValueT>,
"Cannot call lookup() if ValueT is not copyable.");
- typename MapType::const_iterator Pos = Map.find(Key);
- return Pos == Map.end()? ValueT() : Vector[Pos->second].second;
+ auto I = find(Key);
+ return I == end() ? ValueT() : I->second;
}
template <typename... Ts>
@@ -144,7 +146,7 @@ class MapVector {
}
[[nodiscard]] bool contains(const KeyT &Key) const {
- return Map.find(Key) != Map.end();
+ return find(Key) != end();
}
[[nodiscard]] size_type count(const KeyT &Key) const {
@@ -152,11 +154,19 @@ class MapVector {
}
[[nodiscard]] iterator find(const KeyT &Key) {
+ if constexpr (canBeSmall())
+ if (isSmall())
+ return findInVector(Vector, Key);
+
typename MapType::const_iterator Pos = Map.find(Key);
return Pos == Map.end() ? Vector.end() : (Vector.begin() + Pos->second);
}
[[nodiscard]] const_iterator find(const KeyT &Key) const {
+ if constexpr (canBeSmall())
+ if (isSmall())
+ return findInVector(Vector, Key);
+
typename MapType::const_iterator Pos = Map.find(Key);
return Pos == Map.end() ? Vector.end() : (Vector.begin() + Pos->second);
}
@@ -164,21 +174,27 @@ class MapVector {
/// at - Return the entry for the specified key, or abort if no such
/// entry exists.
[[nodiscard]] ValueT &at(const KeyT &Key) {
- auto Pos = Map.find(Key);
- assert(Pos != Map.end() && "MapVector::at failed due to a missing key");
- return Vector[Pos->second].second;
+ auto I = find(Key);
+ assert(I != end() && "MapVector::at failed due to a missing key");
+ return I->second;
}
/// at - Return the entry for the specified key, or abort if no such
/// entry exists.
[[nodiscard]] const ValueT &at(const KeyT &Key) const {
- auto Pos = Map.find(Key);
- assert(Pos != Map.end() && "MapVector::at failed due to a missing key");
- return Vector[Pos->second].second;
+ auto I = find(Key);
+ assert(I != end() && "MapVector::at failed due to a missing key");
+ return I->second;
}
/// Remove the last element from the vector.
void pop_back() {
+ if constexpr (canBeSmall())
+ if (isSmall()) {
+ Vector.pop_back();
+ return;
+ }
+
typename MapType::iterator Pos = Map.find(Vector.back().first);
Map.erase(Pos);
Vector.pop_back();
@@ -192,6 +208,10 @@ class MapVector {
/// \note This is a deceivingly expensive operation (linear time). It's
/// usually better to use \a remove_if() if possible.
typename VectorType::iterator erase(typename VectorType::iterator Iterator) {
+ if constexpr (canBeSmall())
+ if (isSmall())
+ return Vector.erase(Iterator);
+
Map.erase(Iterator->first);
auto Next = Vector.erase(Iterator);
if (Next == Vector.end())
@@ -225,15 +245,47 @@ class MapVector {
template <class Predicate> void remove_if(Predicate Pred);
private:
+ template <typename VectorT, typename LookupKeyT>
+ [[nodiscard]] static auto findInVector(VectorT &Vec, const LookupKeyT &Key) {
+ return find_if(Vec, [&Key](const auto &P) { return P.first == Key; });
+ }
+
+ [[nodiscard]] static constexpr bool canBeSmall() { return N != 0; }
+
+ [[nodiscard]] bool isSmall() const { return Map.empty(); }
+
+ void makeBig() {
+ if constexpr (canBeSmall()) {
+ unsigned Index = 0;
+ for (const auto &entry : Vector)
+ Map[entry.first] = Index++;
+ }
+ }
+
MapType Map;
VectorType Vector;
+ static_assert(N <= 32, "Small size should be less than or equal to 32!");
+
static_assert(
std::is_integral_v<typename MapType::mapped_type>,
"The mapped_type of the specified Map must be an integral type");
template <typename KeyArgT, typename... Ts>
std::pair<iterator, bool> try_emplace_impl(KeyArgT &&Key, Ts &&...Args) {
+ if constexpr (canBeSmall())
+ if (isSmall()) {
+ auto I = findInVector(Vector, Key);
+ if (I != Vector.end())
+ return {I, false};
+ Vector.emplace_back(std::piecewise_construct,
+ std::forward_as_tuple(std::forward<KeyArgT>(Key)),
+ std::forward_as_tuple(std::forward<Ts>(Args)...));
+ if (Vector.size() > N)
+ makeBig();
+ return {std::prev(end()), true};
+ }
+
auto [It, Inserted] = Map.try_emplace(Key);
if (Inserted) {
It->second = Vector.size();
@@ -246,9 +298,16 @@ class MapVector {
}
};
-template <typename KeyT, typename ValueT, typename MapType, typename VectorType>
+template <typename KeyT, typename ValueT, typename MapType, typename VectorType,
+ unsigned N>
template <class Function>
-void MapVector<KeyT, ValueT, MapType, VectorType>::remove_if(Function Pred) {
+void MapVector<KeyT, ValueT, MapType, VectorType, N>::remove_if(Function Pred) {
+ if constexpr (canBeSmall())
+ if (isSmall()) {
+ Vector.erase(llvm::remove_if(Vector, Pred), Vector.end());
+ return;
+ }
+
auto O = Vector.begin();
for (auto I = O, E = Vector.end(); I != E; ++I) {
if (Pred(*I)) {
@@ -271,9 +330,8 @@ void MapVector<KeyT, ValueT, MapType, VectorType>::remove_if(Function Pred) {
/// A MapVector that performs no allocations if smaller than a certain
/// size.
template <typename KeyT, typename ValueT, unsigned N>
-struct SmallMapVector
- : MapVector<KeyT, ValueT, SmallDenseMap<KeyT, unsigned, N>,
- SmallVector<std::pair<KeyT, ValueT>, N>> {
+struct SmallMapVector : MapVector<KeyT, ValueT, DenseMap<KeyT, unsigned>,
+ SmallVector<std::pair<KeyT, ValueT>, N>, N> {
};
} // end namespace llvm
More information about the llvm-commits
mailing list