mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 11:10:11 +01:00
Replaces the callback-based `ForEach` methods on `RawHashtable`, `Map`,
and
`Set` with a range object supporting range-for loops, structured
bindings, and
the standard range concepts.
- Adds `.entries()` on `Map`, `Set`, and `RawHashtable`, returning a
range that
models `std::ranges::forward_range` and `std::ranges::common_range`.
Obtaining one is an explicit call rather than `begin()`/`end()` on the
container, as scanning a whole table is costly and shouldn't be hidden.
- Iterating a `Map` yields a `std::pair` of key and value references,
which
fits in two registers and is returned without being materialized in
memory.
- `Map::Range` and `Set::Range` are aliases of the raw hashtable's range
rather
than wrappers around it. The raw iterator produces the user-facing
reference
itself -- a `KeyT&` for a set, a pair of references for a map -- picked
by
`StorageEntry`, which is already specialized on whether there is a value
type. That leaves one iterator to reason about instead of three.
- Deletes the rvalue `.entries()` overloads on the owning containers, as
a
range built from a temporary table would dangle. Views don't own their
storage, so the operation remains available on them.
- In release builds, the walk over the groups is a single induction
variable: a
negative byte offset counting up to zero, anchored at the ends of the
metadata and entry arrays. Both arrays are then reached by indexed
addressing
off a base that stays put, and the entry pointer is formed only once a
group
with a present entry has been found.
- In debug builds, the range hashes the table's metadata when it is
built and
re-checks that hash when it is destroyed, catching mutation of the table
while a range is live. It also picks a random starting group and a
random odd
group stride, which varies the traversal order between ranges while
still
visiting every group exactly once. That entropy is drawn when the range
is
built rather than in `begin()`, so `begin()` stays a pure function of
the
range and the multi-pass guarantee holds.
- Removes `ForEachEntry` and all of its callers.
Measured against the iteration benchmark added in its own commit, a
traversal is at or ahead of what the callback compiled to across nearly
the
whole size range. The largest tables spend 3-5% fewer cycles, small
`Set`s as
much as 24% fewer, and instruction counts stay within about 1%. What
remains
behind is a handful of mid-sized `Map`s by up to 1%, and `Set` at 65536,
which
sits at exactly half its load factor, by 2%.
Both revisions were built with `-c opt --copt=-march=x86-64-v3` and
compared
with:
```
./scripts/bench_runner.py --exp_benchmark=... --base_benchmark=... \
--benchmark_args=--benchmark_perf_counters=INSTRUCTIONS,CYCLES \
--benchmark_args='--benchmark_filter=(Set|Map)Iterate<(Set|Map)<' \
--extra_metrics_filter='(INSTRUCTIONS|CYCLES)'
```
Trimmed below to the primary integer configurations and to the two
counters;
the pointer- and string-keyed configurations follow the same pattern.
```
Benchmark ┃ CYCLES ┃ INSTRUCTIONS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
BM_MapIterate<Map<int, int>>/1....... │ 👍 -6.032% p=1.14e-05 │ ?? p=0.752
baseline: │ 12.06 ± 1.520% │ 64 ± 3.125%
experiment: │ 11.33 ± 2.765% │ 65.5 ± 3.817%
│ │
BM_MapIterate<Map<int, int>>/2....... │ ?? p=0.155 │ ?? p=0.343
baseline: │ 7.587 ± 1.285% │ 41 ± 0.000%
experiment: │ 7.652 ± 0.865% │ 41 ± 2.439%
│ │
BM_MapIterate<Map<int, int>>/3....... │ ?? p=0.343 │ 👍 -1.020% p=0.0039
baseline: │ 6.663 ± 4.260% │ 32.67 ± 2.041%
experiment: │ 6.368 ± 12.224% │ 32.33 ± 2.062%
│ │
BM_MapIterate<Map<int, int>>/4....... │ ?? p=0.343 │ 👍 -1.786% p=0.0297
baseline: │ 6.091 ± 15.470% │ 28 ± 3.571%
experiment: │ 5.957 ± 8.932% │ 27.5 ± 3.636%
│ │
BM_MapIterate<Map<int, int>>/8....... │ ?? p=0.323 │ 👍 -1.220% p=0.000148
baseline: │ 4.845 ± 0.800% │ 20.5 ± 0.000%
experiment: │ 4.814 ± 3.585% │ 20.25 ± 0.000%
│ │
BM_MapIterate<Map<int, int>>/16...... │ 👍 -2.195% p=0.00908 │ 👍 0.769% p=6.58e-06
baseline: │ 4.312 ± 0.187% │ 16.25 ± 0.000%
experiment: │ 4.218 ± 2.368% │ 16.13 ± 0.000%
│ │
BM_MapIterate<Map<int, int>>/32...... │ ?? p=0.236 │ 👍 0.442% p=9.53e-06
baseline: │ 4.051 ± 1.084% │ 14.13 ± 0.000%
experiment: │ 4.063 ± 0.737% │ 14.06 ± 0.000%
│ │
BM_MapIterate<Map<int, int>>/64...... │ ?? p=0.693 │ 👎 0.227% p=4.52e-06
baseline: │ 4.021 ± 0.239% │ 13.75 ± 0.000%
experiment: │ 4.019 ± 0.417% │ 13.78 ± 0.000%
│ │
BM_MapIterate<Map<int, int>>/256..... │ 👍 0.360% p=0.00119 │ 👎 0.754% p=1.37e-05
baseline: │ 3.996 ± 0.173% │ 13.47 ± 0.000%
experiment: │ 3.982 ± 0.272% │ 13.57 ± 0.000%
│ │
BM_MapIterate<Map<int, int>>/4096.... │ 👍 0.581% p=1.96e-05 │ 👎 0.923% p=1.96e-05
baseline: │ 4.005 ± 0.816% │ 13.38 ± 0.000%
experiment: │ 3.981 ± 0.192% │ 13.5 ± 0.000%
│ │
BM_MapIterate<Map<int, int>>/65536... │ 👍 -4.957% p=1.14e-05 │ 👎 0.934% p=1.14e-05
baseline: │ 5.307 ± 0.501% │ 13.38 ± 0.000%
experiment: │ 5.044 ± 1.746% │ 13.5 ± 0.000%
│ │
BM_MapIterate<Map<int, int>>/1048576. │ 👍 -3.947% p=9.09e-05 │ 👎 0.935% p=3.3e-05
baseline: │ 6.074 ± 0.807% │ 13.38 ± 0.000%
experiment: │ 5.834 ± 2.159% │ 13.5 ± 0.000%
│ │
BM_MapIterate<Map<int, int>>/16777216 │ ?? p=0.155 │ 👎 0.935% p=2.11e-05
baseline: │ 5.082 ± 3.650% │ 13.38 ± 0.000%
experiment: │ 5.012 ± 1.316% │ 13.5 ± 0.000%
│ │
BM_MapIterate<Map<int, int>>/56...... │ 👎 0.825% p=0.0268 │ 👍 0.270% p=1.14e-05
baseline: │ 3.918 ± 0.501% │ 13.21 ± 0.000%
experiment: │ 3.951 ± 0.342% │ 13.18 ± 0.000%
│ │
BM_MapIterate<Map<int, int>>/224..... │ 👎 0.788% p=0.000504 │ 👎 0.346% p=1.64e-05
baseline: │ 3.895 ± 0.111% │ 12.89 ± 0.000%
experiment: │ 3.926 ± 0.285% │ 12.94 ± 0.000%
│ │
BM_MapIterate<Map<int, int>>/3584.... │ 👎 1.028% p=0.000148 │ 👎 0.545% p=1.14e-05
baseline: │ 3.913 ± 0.427% │ 12.79 ± 0.000%
experiment: │ 3.954 ± 0.325% │ 12.86 ± 0.000%
│ │
BM_MapIterate<Map<int, int>>/57344... │ ?? p=0.236 │ 👎 0.558% p=2.55e-06
baseline: │ 4.574 ± 0.721% │ 12.79 ± 0.000%
experiment: │ 4.51 ± 3.709% │ 12.86 ± 0.000%
│ │
BM_MapIterate<Map<int, int>>/917504.. │ 👍 -3.826% p=6.58e-06 │ 👎 0.559% p=2.33e-05
baseline: │ 5.221 ± 0.507% │ 12.79 ± 0.000%
experiment: │ 5.021 ± 0.556% │ 12.86 ± 0.000%
│ │
BM_MapIterate<Map<int, int>>/14680064 │ 👍 -3.839% p=1.37e-05 │ 👎 0.559% p=3.31e-05
baseline: │ 5.129 ± 1.194% │ 12.79 ± 0.000%
experiment: │ 4.932 ± 1.475% │ 12.86 ± 0.000%
│ │
Benchmark ┃ CYCLES ┃ INSTRUCTIONS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
BM_SetIterate<Set<int>>/1....... │ 👍 -3.104% p=0.000583 │ ?? p=0.206
baseline: │ 11.2 ± 6.323% │ 60 ± 3.333%
experiment: │ 10.85 ± 5.820% │ 61 ± 3.279%
│ │
BM_SetIterate<Set<int>>/2....... │ 👍 -7.037% p=0.0362 │ ?? p=0.155
baseline: │ 7.086 ± 16.857% │ 37 ± 0.000%
experiment: │ 6.587 ± 0.479% │ 36 ± 4.167%
│ │
BM_SetIterate<Set<int>>/3....... │ 👎 1.400% p=2.34e-05 │ 👍 -1.163% p=0.00136
baseline: │ 5.363 ± 0.463% │ 28.67 ± 2.326%
experiment: │ 5.438 ± 32.763% │ 28.33 ± 1.176%
│ │
BM_SetIterate<Set<int>>/4....... │ ?? p=0.968 │ ?? p=0.286
baseline: │ 4.642 ± 32.751% │ 23.5 ± 2.128%
experiment: │ 4.658 ± 38.416% │ 23.63 ± 3.704%
│ │
BM_SetIterate<Set<int>>/8....... │ 👍 -23.823% p=3.74e-06 │ 👍 -1.515% p=5.52e-05
baseline: │ 4.701 ± 6.589% │ 16.5 ± 0.000%
experiment: │ 3.581 ± 7.790% │ 16.25 ± 0.000%
│ │
BM_SetIterate<Set<int>>/16...... │ 👍 -4.502% p=1.37e-05 │ 👍 -1.020% p=3.31e-05
baseline: │ 3.124 ± 0.585% │ 12.25 ± 0.000%
experiment: │ 2.983 ± 0.625% │ 12.13 ± 0.000%
│ │
BM_SetIterate<Set<int>>/32...... │ 👍 -4.032% p=5.46e-06 │ 👍 0.617% p=1.96e-05
baseline: │ 2.957 ± 0.260% │ 10.13 ± 0.000%
experiment: │ 2.838 ± 0.434% │ 10.06 ± 0.000%
│ │
BM_SetIterate<Set<int>>/64...... │ 👍 -5.054% p=4.52e-06 │ 👎 0.321% p=1.37e-05
baseline: │ 2.937 ± 0.301% │ 9.75 ± 0.000%
experiment: │ 2.788 ± 1.143% │ 9.781 ± 0.000%
│ │
BM_SetIterate<Set<int>>/256..... │ 👍 -5.325% p=1.14e-05 │ 👎 1.073% p=6.58e-06
baseline: │ 2.916 ± 0.220% │ 9.469 ± 0.000%
experiment: │ 2.761 ± 0.142% │ 9.57 ± 0.000%
│ │
BM_SetIterate<Set<int>>/4096.... │ 👍 -4.865% p=4.52e-06 │ 👎 1.317% p=2.34e-05
baseline: │ 2.921 ± 0.194% │ 9.381 ± 0.000%
experiment: │ 2.779 ± 0.224% │ 9.504 ± 0.000%
│ │
BM_SetIterate<Set<int>>/65536... │ 👎 1.961% p=3.93e-05 │ 👎 1.332% p=1.49e-05
baseline: │ 4.015 ± 0.482% │ 9.375 ± 0.000%
experiment: │ 4.094 ± 0.613% │ 9.5 ± 0.000%
│ │
BM_SetIterate<Set<int>>/1048576. │ 👍 -4.843% p=1.14e-05 │ 👎 1.333% p=5.38e-06
baseline: │ 5.239 ± 0.144% │ 9.375 ± 0.000%
experiment: │ 4.986 ± 0.139% │ 9.5 ± 0.000%
│ │
BM_SetIterate<Set<int>>/16777216 │ 👍 0.840% p=0.0362 │ 👎 1.333% p=2.52e-06
baseline: │ 3.719 ± 1.420% │ 9.375 ± 0.000%
experiment: │ 3.688 ± 1.308% │ 9.5 ± 0.000%
│ │
BM_SetIterate<Set<int>>/56...... │ 👍 -2.857% p=9.53e-06 │ 👍 0.388% p=3.31e-05
baseline: │ 2.942 ± 0.439% │ 9.214 ± 0.000%
experiment: │ 2.858 ± 0.619% │ 9.179 ± 0.000%
│ │
BM_SetIterate<Set<int>>/224..... │ 👍 -2.161% p=2.34e-05 │ 👎 0.502% p=4.52e-06
baseline: │ 2.888 ± 0.347% │ 8.893 ± 0.000%
experiment: │ 2.826 ± 0.450% │ 8.938 ± 0.000%
│ │
BM_SetIterate<Set<int>>/3584.... │ 👍 -1.750% p=6.58e-06 │ 👎 0.793% p=2.34e-05
baseline: │ 2.89 ± 0.261% │ 8.792 ± 0.000%
experiment: │ 2.84 ± 0.411% │ 8.862 ± 0.000%
│ │
BM_SetIterate<Set<int>>/57344... │ ?? p=0.502 │ 👎 0.812% p=2.78e-05
baseline: │ 3.684 ± 4.246% │ 8.786 ± 0.000%
experiment: │ 3.644 ± 4.431% │ 8.857 ± 0.000%
│ │
BM_SetIterate<Set<int>>/917504.. │ 👍 -2.629% p=0.000148 │ 👎 0.813% p=3.08e-06
baseline: │ 4.372 ± 0.693% │ 8.786 ± 0.000%
experiment: │ 4.257 ± 0.210% │ 8.857 ± 0.000%
│ │
BM_SetIterate<Set<int>>/14680064 │ 👍 -2.927% p=0.0219 │ 👎 0.813% p=3.03e-06
baseline: │ 4.154 ± 3.286% │ 8.786 ± 0.000%
experiment: │ 4.032 ± 3.198% │ 8.857 ± 0.000%
│ │
```
Assisted-by: Antigravity with Opus
981 lines
33 KiB
C++
981 lines
33 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "common/map.h"
|
|
|
|
#include <gmock/gmock.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <concepts>
|
|
#include <initializer_list>
|
|
#include <iterator>
|
|
#include <ranges>
|
|
#include <type_traits>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#include "common/raw_hashtable_test_helpers.h"
|
|
|
|
// Workaround for std::pair comparison deficiency in libc++ 16.
|
|
#if defined(_LIBCPP_VERSION) && _LIBCPP_VERSION < 170000
|
|
namespace std {
|
|
template <typename T, typename U, typename V, typename W>
|
|
requires(convertible_to<V, T> && convertible_to<W, U>)
|
|
inline auto operator==(
|
|
pair<std::reference_wrapper<T>, std::reference_wrapper<U>> lhs,
|
|
pair<V, W> rhs) -> bool {
|
|
return lhs.first == static_cast<T>(rhs.first) &&
|
|
lhs.second == static_cast<U>(rhs.second);
|
|
}
|
|
} // namespace std
|
|
#endif
|
|
|
|
namespace Carbon::Testing {
|
|
namespace {
|
|
|
|
using RawHashtable::FixedHashKeyContext;
|
|
using RawHashtable::IndexKeyContext;
|
|
using RawHashtable::MoveOnlyTestData;
|
|
using RawHashtable::TestData;
|
|
using RawHashtable::TestKeyContext;
|
|
using ::testing::Pair;
|
|
using ::testing::UnorderedElementsAre;
|
|
using ::testing::UnorderedElementsAreArray;
|
|
|
|
template <typename MapT, typename MatcherRangeT>
|
|
auto ExpectMapElementsAre(MapT&& m, MatcherRangeT element_matchers) -> void {
|
|
// Now collect the elements into a container.
|
|
using KeyT = std::remove_reference<MapT>::type::KeyT;
|
|
using ValueT = std::remove_reference<MapT>::type::ValueT;
|
|
std::vector<
|
|
std::pair<std::reference_wrapper<KeyT>, std::reference_wrapper<ValueT>>>
|
|
map_entries;
|
|
for (auto [k, v] : m.entries()) {
|
|
map_entries.push_back({std::ref(k), std::ref(v)});
|
|
}
|
|
|
|
// Use the GoogleMock unordered container matcher to validate and show errors
|
|
// on wrong elements.
|
|
EXPECT_THAT(map_entries, UnorderedElementsAreArray(element_matchers));
|
|
}
|
|
|
|
// Allow directly using an initializer list.
|
|
template <typename MapT, typename MatcherT>
|
|
auto ExpectMapElementsAre(MapT&& m,
|
|
std::initializer_list<MatcherT> element_matchers)
|
|
-> void {
|
|
std::vector<MatcherT> element_matchers_storage = element_matchers;
|
|
ExpectMapElementsAre(m, element_matchers_storage);
|
|
}
|
|
|
|
template <typename ValueCB, typename RangeT, typename... RangeTs>
|
|
auto MakeKeyValues(ValueCB value_cb, RangeT&& range, RangeTs&&... ranges)
|
|
-> auto {
|
|
using KeyT = RangeT::value_type;
|
|
using ValueT = decltype(value_cb(std::declval<KeyT>()));
|
|
std::vector<std::pair<KeyT, ValueT>> elements;
|
|
auto add_range = [&](RangeT&& r) {
|
|
for (const auto&& e : r) {
|
|
elements.push_back({e, value_cb(e)});
|
|
}
|
|
};
|
|
add_range(std::forward<RangeT>(range));
|
|
(add_range(std::forward<RangeTs>(ranges)), ...);
|
|
|
|
return elements;
|
|
}
|
|
|
|
template <typename MapT>
|
|
class MapTest : public ::testing::Test {};
|
|
|
|
template <typename MapT>
|
|
class MoveOnlyMapTest : public ::testing::Test {};
|
|
|
|
using Types = ::testing::Types<
|
|
Map<int, int>, Map<int, int, 16>, Map<int, int, 64>,
|
|
Map<int, int, 0, TestKeyContext>, Map<int, int, 16, TestKeyContext>,
|
|
Map<int, int, 64, TestKeyContext>, Map<TestData, TestData>,
|
|
Map<TestData, TestData, 16>, Map<TestData, TestData, 0, TestKeyContext>,
|
|
Map<TestData, TestData, 16, TestKeyContext>>;
|
|
TYPED_TEST_SUITE(MapTest, Types);
|
|
|
|
using MoveOnlyTypes = ::testing::Types<
|
|
Map<MoveOnlyTestData, MoveOnlyTestData>,
|
|
Map<MoveOnlyTestData, MoveOnlyTestData, 16>,
|
|
Map<MoveOnlyTestData, MoveOnlyTestData, 64>,
|
|
Map<MoveOnlyTestData, MoveOnlyTestData, 0, TestKeyContext>,
|
|
Map<MoveOnlyTestData, MoveOnlyTestData, 16, TestKeyContext>>;
|
|
TYPED_TEST_SUITE(MoveOnlyMapTest, MoveOnlyTypes);
|
|
|
|
TYPED_TEST(MapTest, Basic) {
|
|
TypeParam m;
|
|
|
|
EXPECT_FALSE(m.Contains(42));
|
|
EXPECT_EQ(nullptr, m[42]);
|
|
EXPECT_TRUE(m.Insert(1, 100).is_inserted());
|
|
ASSERT_TRUE(m.Contains(1));
|
|
auto result = m.Lookup(1);
|
|
EXPECT_TRUE(result);
|
|
EXPECT_EQ(1, result.key());
|
|
EXPECT_EQ(100, result.value());
|
|
EXPECT_EQ(100, *m[1]);
|
|
// Reinsertion doesn't change the value.
|
|
auto i_result = m.Insert(1, 101);
|
|
EXPECT_FALSE(i_result.is_inserted());
|
|
EXPECT_EQ(100, i_result.value());
|
|
EXPECT_EQ(100, *m[1]);
|
|
// Update does change the value.
|
|
i_result = m.Update(1, 101);
|
|
EXPECT_FALSE(i_result.is_inserted());
|
|
EXPECT_EQ(101, i_result.value());
|
|
EXPECT_EQ(101, *m[1]);
|
|
|
|
// Verify all the elements.
|
|
ExpectMapElementsAre(m, {Pair(1, 101)});
|
|
|
|
// Fill up a bunch to ensure we trigger growth a few times.
|
|
for (int i : llvm::seq(2, 512)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
EXPECT_TRUE(m.Insert(i, i * 100).is_inserted());
|
|
}
|
|
ExpectMapElementsAre(
|
|
m, MakeKeyValues([](int k) { return k * 100 + static_cast<int>(k == 1); },
|
|
llvm::seq(1, 512)));
|
|
for (int i : llvm::seq(1, 512)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
EXPECT_FALSE(m.Insert(i, i * 100 + 1).is_inserted());
|
|
EXPECT_EQ(i * 100 + static_cast<int>(i == 1), *m[i]);
|
|
EXPECT_FALSE(m.Update(i, i * 100 + 1).is_inserted());
|
|
EXPECT_EQ(i * 100 + 1, *m[i]);
|
|
}
|
|
EXPECT_FALSE(m.Contains(513));
|
|
|
|
// Verify all the elements.
|
|
ExpectMapElementsAre(
|
|
m, MakeKeyValues([](int k) { return k * 100 + 1; }, llvm::seq(1, 512)));
|
|
}
|
|
|
|
TYPED_TEST(MapTest, FactoryApi) {
|
|
TypeParam m;
|
|
EXPECT_TRUE(m.Insert(1, [] { return 100; }).is_inserted());
|
|
ASSERT_TRUE(m.Contains(1));
|
|
EXPECT_EQ(100, *m[1]);
|
|
// Reinsertion doesn't invoke the callback.
|
|
EXPECT_FALSE(m.Insert(1, []() -> int {
|
|
llvm_unreachable("Should never be called!");
|
|
}).is_inserted());
|
|
// Update does invoke the callback.
|
|
auto i_result = m.Update(1, [] { return 101; });
|
|
EXPECT_FALSE(i_result.is_inserted());
|
|
EXPECT_EQ(101, i_result.value());
|
|
EXPECT_EQ(101, *m[1]);
|
|
}
|
|
|
|
TYPED_TEST(MapTest, Copy) {
|
|
using MapT = TypeParam;
|
|
|
|
MapT m;
|
|
// Make sure we exceed the small size for some of the map types, but not all
|
|
// of them, so we cover all the combinations of copying between small and
|
|
// large.
|
|
for (int i : llvm::seq(1, 24)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(m.Insert(i, i * 100).is_inserted());
|
|
}
|
|
|
|
MapT other_m1 = m;
|
|
ExpectMapElementsAre(
|
|
other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 24)));
|
|
|
|
// Add some more elements to the original.
|
|
for (int i : llvm::seq(24, 32)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(m.Insert(i, i * 100).is_inserted());
|
|
}
|
|
|
|
// The first copy doesn't change.
|
|
ExpectMapElementsAre(
|
|
other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 24)));
|
|
|
|
// A new copy does.
|
|
MapT other_m2 = m;
|
|
ExpectMapElementsAre(
|
|
other_m2, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 32)));
|
|
|
|
// Copy-assign updates.
|
|
other_m1 = m;
|
|
ExpectMapElementsAre(
|
|
other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 32)));
|
|
|
|
// Self-assign is a no-op.
|
|
other_m1 = const_cast<const MapT&>(other_m1);
|
|
ExpectMapElementsAre(
|
|
other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 32)));
|
|
|
|
// But mutating original still doesn't change copies.
|
|
for (int i : llvm::seq(32, 48)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(m.Insert(i, i * 100).is_inserted());
|
|
}
|
|
ExpectMapElementsAre(
|
|
other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 32)));
|
|
ExpectMapElementsAre(
|
|
other_m2, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 32)));
|
|
}
|
|
|
|
TYPED_TEST(MapTest, Move) {
|
|
using MapT = TypeParam;
|
|
|
|
MapT m;
|
|
// Make sure we exceed the small size for some of the map types, but not all
|
|
// of them, so we cover all the combinations of moving between small and
|
|
// large.
|
|
for (int i : llvm::seq(1, 24)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
EXPECT_TRUE(m.Insert(i, i * 100).is_inserted());
|
|
}
|
|
|
|
MapT other_m1 = std::move(m);
|
|
ExpectMapElementsAre(
|
|
other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 24)));
|
|
|
|
// Add some more elements.
|
|
for (int i : llvm::seq(24, 32)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(other_m1.Insert(i, i * 100).is_inserted());
|
|
}
|
|
ExpectMapElementsAre(
|
|
other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 32)));
|
|
|
|
// Move back over a moved-from.
|
|
m = std::move(other_m1);
|
|
ExpectMapElementsAre(
|
|
m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 32)));
|
|
|
|
// Copy over moved-from state also works.
|
|
other_m1 = m;
|
|
ExpectMapElementsAre(
|
|
other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 32)));
|
|
|
|
// Now add still more elements.
|
|
for (int i : llvm::seq(32, 48)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(other_m1.Insert(i, i * 100).is_inserted());
|
|
}
|
|
ExpectMapElementsAre(
|
|
other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 48)));
|
|
|
|
// And move-assign over the copy looks like the moved-from table not the copy.
|
|
other_m1 = std::move(m);
|
|
ExpectMapElementsAre(
|
|
other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 32)));
|
|
|
|
// Self-swap (which does a self-move) works and is a no-op.
|
|
std::swap(other_m1, other_m1);
|
|
ExpectMapElementsAre(
|
|
other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 32)));
|
|
|
|
// Test copying of a moved-from table over a valid table and
|
|
// self-move-assign. The former is required to be valid, and the latter is
|
|
// in at least the case of self-move-assign-when-moved-from, but the result
|
|
// can be in any state so just do them and ensure we don't crash.
|
|
MapT other_m2 = other_m1;
|
|
// NOLINTNEXTLINE(bugprone-use-after-move): Testing required use-after-move.
|
|
other_m2 = m;
|
|
other_m1 = std::move(other_m1);
|
|
m = std::move(m);
|
|
}
|
|
|
|
TYPED_TEST(MoveOnlyMapTest, MoveOnlyTypes) {
|
|
using MapT = TypeParam;
|
|
static_assert(!std::is_copy_assignable_v<MapT>);
|
|
static_assert(!std::is_copy_constructible_v<MapT>);
|
|
static_assert(std::is_move_assignable_v<MapT>);
|
|
static_assert(std::is_move_constructible_v<MapT>);
|
|
|
|
auto make_map = [] {
|
|
MapT m;
|
|
// Make sure we exceed the small size for some of the map types, but not all
|
|
// of them, so we cover all the combinations of moving between small and
|
|
// large.
|
|
for (int i : llvm::seq(1, 24)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
EXPECT_TRUE(m.Insert(i, i * 100).is_inserted());
|
|
}
|
|
return m;
|
|
};
|
|
|
|
MapT m = make_map();
|
|
|
|
MapT other_m1 = std::move(m);
|
|
ExpectMapElementsAre(
|
|
other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 24)));
|
|
|
|
// Add some more elements.
|
|
for (int i : llvm::seq(24, 32)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(other_m1.Insert(i, i * 100).is_inserted());
|
|
}
|
|
ExpectMapElementsAre(
|
|
other_m1, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 32)));
|
|
|
|
// Move back over a moved-from.
|
|
m = std::move(other_m1);
|
|
ExpectMapElementsAre(
|
|
m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 32)));
|
|
|
|
// Now add still more elements, crossing the small size limit for all tested
|
|
// map types.
|
|
for (int i : llvm::seq(32, 72)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(m.Insert(i, i * 100).is_inserted());
|
|
}
|
|
ExpectMapElementsAre(
|
|
m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 72)));
|
|
|
|
// Assignment replaces the contents.
|
|
m = make_map();
|
|
ExpectMapElementsAre(
|
|
m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 24)));
|
|
|
|
// Self-swap (which does a self-move) works and is a no-op.
|
|
std::swap(m, m);
|
|
ExpectMapElementsAre(
|
|
m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 24)));
|
|
}
|
|
|
|
TYPED_TEST(MapTest, Conversions) {
|
|
using MapT = TypeParam;
|
|
using KeyT = MapT::KeyT;
|
|
using ValueT = MapT::ValueT;
|
|
using KeyContextT = MapT::KeyContextT;
|
|
|
|
MapT m;
|
|
|
|
ASSERT_TRUE(m.Insert(1, 101).is_inserted());
|
|
ASSERT_TRUE(m.Insert(2, 102).is_inserted());
|
|
ASSERT_TRUE(m.Insert(3, 103).is_inserted());
|
|
ASSERT_TRUE(m.Insert(4, 104).is_inserted());
|
|
|
|
MapView<KeyT, ValueT, KeyContextT> mv = m;
|
|
MapView<const KeyT, ValueT, KeyContextT> cmv = m;
|
|
MapView<KeyT, const ValueT, KeyContextT> cmv2 = m;
|
|
MapView<const KeyT, const ValueT, KeyContextT> cmv3 = m;
|
|
EXPECT_TRUE(mv.Contains(1));
|
|
EXPECT_EQ(101, *mv[1]);
|
|
EXPECT_TRUE(cmv.Contains(2));
|
|
EXPECT_EQ(102, *cmv[2]);
|
|
EXPECT_TRUE(cmv2.Contains(3));
|
|
EXPECT_EQ(103, *cmv2[3]);
|
|
EXPECT_TRUE(cmv3.Contains(4));
|
|
EXPECT_EQ(104, *cmv3[4]);
|
|
}
|
|
|
|
TYPED_TEST(MapTest, GrowToAllocSize) {
|
|
using MapT = TypeParam;
|
|
|
|
MapT m;
|
|
// Grow when empty. May be a no-op for some small sizes.
|
|
m.GrowToAllocSize(32);
|
|
|
|
// Add some elements that will need to be propagated through subsequent
|
|
// growths. Also delete some.
|
|
ssize_t storage_bytes = m.ComputeMetrics().storage_bytes;
|
|
for (int i : llvm::seq(1, 24)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(m.Insert(i, i * 100).is_inserted());
|
|
}
|
|
for (int i : llvm::seq(1, 8)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(m.Erase(i));
|
|
}
|
|
// No further growth triggered.
|
|
EXPECT_EQ(storage_bytes, m.ComputeMetrics().storage_bytes);
|
|
|
|
// No-op.
|
|
m.GrowToAllocSize(16);
|
|
ExpectMapElementsAre(
|
|
m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(8, 24)));
|
|
// No further growth triggered.
|
|
EXPECT_EQ(storage_bytes, m.ComputeMetrics().storage_bytes);
|
|
|
|
// Get a few doubling based growths, and at least one beyond the largest small
|
|
// size.
|
|
m.GrowToAllocSize(64);
|
|
ExpectMapElementsAre(
|
|
m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(8, 24)));
|
|
m.GrowToAllocSize(128);
|
|
ExpectMapElementsAre(
|
|
m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(8, 24)));
|
|
// Update the storage bytes after growth.
|
|
EXPECT_LT(storage_bytes, m.ComputeMetrics().storage_bytes);
|
|
storage_bytes = m.ComputeMetrics().storage_bytes;
|
|
|
|
// Add some more, but not enough to trigger further growth, and then grow by
|
|
// several more multiples of two to test handling large growth.
|
|
for (int i : llvm::seq(24, 48)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(m.Insert(i, i * 100).is_inserted());
|
|
}
|
|
for (int i : llvm::seq(8, 16)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(m.Erase(i));
|
|
}
|
|
// No growth from insertions.
|
|
EXPECT_EQ(storage_bytes, m.ComputeMetrics().storage_bytes);
|
|
|
|
m.GrowToAllocSize(1024);
|
|
ExpectMapElementsAre(
|
|
m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(16, 48)));
|
|
// Storage should have grown.
|
|
EXPECT_LT(storage_bytes, m.ComputeMetrics().storage_bytes);
|
|
}
|
|
|
|
TYPED_TEST(MapTest, GrowForInsert) {
|
|
using MapT = TypeParam;
|
|
|
|
MapT m;
|
|
m.GrowForInsertCount(42);
|
|
ssize_t storage_bytes = m.ComputeMetrics().storage_bytes;
|
|
for (int i : llvm::seq(1, 42)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(m.Insert(i, i * 100).is_inserted());
|
|
}
|
|
ExpectMapElementsAre(
|
|
m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 42)));
|
|
EXPECT_EQ(storage_bytes, m.ComputeMetrics().storage_bytes);
|
|
|
|
// Erase many elements and grow again for another insert.
|
|
for (int i : llvm::seq(1, 32)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(m.Erase(i));
|
|
}
|
|
m.GrowForInsertCount(42);
|
|
storage_bytes = m.ComputeMetrics().storage_bytes;
|
|
for (int i : llvm::seq(42, 84)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(m.Insert(i, i * 100).is_inserted());
|
|
}
|
|
ExpectMapElementsAre(
|
|
m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(32, 84)));
|
|
EXPECT_EQ(storage_bytes, m.ComputeMetrics().storage_bytes);
|
|
|
|
// Erase all the elements, then grow for a much larger insertion and insert
|
|
// again.
|
|
for (int i : llvm::seq(32, 84)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(m.Erase(i));
|
|
}
|
|
m.GrowForInsertCount(321);
|
|
storage_bytes = m.ComputeMetrics().storage_bytes;
|
|
for (int i : llvm::seq(128, 321 + 128)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(m.Insert(i, i * 100).is_inserted());
|
|
}
|
|
ExpectMapElementsAre(m, MakeKeyValues([](int k) { return k * 100; },
|
|
llvm::seq(128, 321 + 128)));
|
|
EXPECT_EQ(storage_bytes, m.ComputeMetrics().storage_bytes);
|
|
}
|
|
|
|
// This test is largely exercising the underlying `RawHashtable` implementation
|
|
// with complex growth, erasure, and re-growth.
|
|
TYPED_TEST(MapTest, ComplexOpSequence) {
|
|
// Use a small size as well to cover more growth scenarios.
|
|
TypeParam m;
|
|
|
|
EXPECT_FALSE(m.Contains(42));
|
|
EXPECT_EQ(nullptr, m[42]);
|
|
EXPECT_TRUE(m.Insert(1, 100).is_inserted());
|
|
ASSERT_TRUE(m.Contains(1));
|
|
auto result = m.Lookup(1);
|
|
EXPECT_TRUE(result);
|
|
EXPECT_EQ(1, result.key());
|
|
EXPECT_EQ(100, result.value());
|
|
EXPECT_EQ(100, *m[1]);
|
|
// Reinsertion doesn't change the value.
|
|
auto i_result = m.Insert(1, 101);
|
|
EXPECT_FALSE(i_result.is_inserted());
|
|
EXPECT_EQ(100, i_result.value());
|
|
EXPECT_EQ(100, *m[1]);
|
|
// Update does change the value.
|
|
i_result = m.Update(1, 101);
|
|
EXPECT_FALSE(i_result.is_inserted());
|
|
EXPECT_EQ(101, i_result.value());
|
|
EXPECT_EQ(101, *m[1]);
|
|
|
|
// Verify all the elements.
|
|
ExpectMapElementsAre(m, {Pair(1, 101)});
|
|
|
|
// Fill up the small buffer but don't overflow it.
|
|
for (int i : llvm::seq(2, 5)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
EXPECT_TRUE(m.Insert(i, i * 100).is_inserted());
|
|
}
|
|
for (int i : llvm::seq(1, 5)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(m.Contains(i));
|
|
EXPECT_EQ(i * 100 + static_cast<int>(i == 1), *m[i]);
|
|
EXPECT_FALSE(m.Insert(i, i * 100 + 1).is_inserted());
|
|
EXPECT_EQ(i * 100 + static_cast<int>(i == 1), *m[i]);
|
|
EXPECT_FALSE(m.Update(i, i * 100 + 1).is_inserted());
|
|
EXPECT_EQ(i * 100 + 1, *m[i]);
|
|
}
|
|
EXPECT_FALSE(m.Contains(5));
|
|
|
|
// Verify all the elements.
|
|
ExpectMapElementsAre(
|
|
m, {Pair(1, 101), Pair(2, 201), Pair(3, 301), Pair(4, 401)});
|
|
|
|
// Erase some entries from the small buffer.
|
|
EXPECT_FALSE(m.Erase(42));
|
|
EXPECT_TRUE(m.Erase(2));
|
|
EXPECT_EQ(101, *m[1]);
|
|
EXPECT_EQ(nullptr, m[2]);
|
|
EXPECT_EQ(301, *m[3]);
|
|
EXPECT_EQ(401, *m[4]);
|
|
EXPECT_TRUE(m.Erase(1));
|
|
EXPECT_EQ(nullptr, m[1]);
|
|
EXPECT_EQ(nullptr, m[2]);
|
|
EXPECT_EQ(301, *m[3]);
|
|
EXPECT_EQ(401, *m[4]);
|
|
EXPECT_TRUE(m.Erase(4));
|
|
EXPECT_EQ(nullptr, m[1]);
|
|
EXPECT_EQ(nullptr, m[2]);
|
|
EXPECT_EQ(301, *m[3]);
|
|
EXPECT_EQ(nullptr, m[4]);
|
|
// Fill them back in, but with a different order and going back to the
|
|
// original value.
|
|
EXPECT_TRUE(m.Insert(1, 100).is_inserted());
|
|
EXPECT_TRUE(m.Insert(2, 200).is_inserted());
|
|
EXPECT_TRUE(m.Insert(4, 400).is_inserted());
|
|
EXPECT_EQ(100, *m[1]);
|
|
EXPECT_EQ(200, *m[2]);
|
|
EXPECT_EQ(301, *m[3]);
|
|
EXPECT_EQ(400, *m[4]);
|
|
// Then update their values to match.
|
|
EXPECT_FALSE(m.Update(1, 101).is_inserted());
|
|
EXPECT_FALSE(m.Update(2, 201).is_inserted());
|
|
EXPECT_FALSE(m.Update(4, 401).is_inserted());
|
|
|
|
// Now fill up the first metadata group.
|
|
for (int i : llvm::seq(5, 14)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
EXPECT_TRUE(m.Insert(i, i * 100).is_inserted());
|
|
}
|
|
for (int i : llvm::seq(1, 14)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(m.Contains(i));
|
|
EXPECT_EQ(i * 100 + static_cast<int>(i < 5), *m[i]);
|
|
EXPECT_FALSE(m.Insert(i, i * 100 + 2).is_inserted());
|
|
EXPECT_EQ(i * 100 + static_cast<int>(i < 5), *m[i]);
|
|
EXPECT_FALSE(m.Update(i, i * 100 + 2).is_inserted());
|
|
EXPECT_EQ(i * 100 + 2, *m[i]);
|
|
}
|
|
EXPECT_FALSE(m.Contains(42));
|
|
|
|
// Verify all the elements by walking the entire map.
|
|
ExpectMapElementsAre(
|
|
m, {Pair(1, 102), Pair(2, 202), Pair(3, 302), Pair(4, 402), Pair(5, 502),
|
|
Pair(6, 602), Pair(7, 702), Pair(8, 802), Pair(9, 902),
|
|
Pair(10, 1002), Pair(11, 1102), Pair(12, 1202), Pair(13, 1302)});
|
|
|
|
// Now fill up several more groups.
|
|
for (int i : llvm::seq(14, 100)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
EXPECT_TRUE(m.Insert(i, i * 100).is_inserted());
|
|
}
|
|
for (int i : llvm::seq(1, 100)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(m.Contains(i));
|
|
EXPECT_EQ(i * 100 + 2 * static_cast<int>(i < 14), *m[i]);
|
|
EXPECT_FALSE(m.Insert(i, i * 100 + 1).is_inserted());
|
|
EXPECT_EQ(i * 100 + 2 * static_cast<int>(i < 14), *m[i]);
|
|
EXPECT_FALSE(m.Update(i, i * 100 + 3).is_inserted());
|
|
EXPECT_EQ(i * 100 + 3, *m[i]);
|
|
}
|
|
EXPECT_FALSE(m.Contains(420));
|
|
|
|
// Check walking the entire container.
|
|
ExpectMapElementsAre(
|
|
m, MakeKeyValues([](int k) { return k * 100 + 3; }, llvm::seq(1, 100)));
|
|
|
|
// Clear back to empty.
|
|
m.Clear();
|
|
EXPECT_FALSE(m.Contains(42));
|
|
EXPECT_EQ(nullptr, m[42]);
|
|
|
|
// Refill but with both overlapping and different values.
|
|
for (int i : llvm::seq(50, 150)) {
|
|
EXPECT_TRUE(m.Insert(i, i * 100).is_inserted());
|
|
}
|
|
for (int i : llvm::seq(50, 150)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(m.Contains(i));
|
|
EXPECT_EQ(i * 100, *m[i]);
|
|
EXPECT_FALSE(m.Insert(i, i * 100 + 1).is_inserted());
|
|
EXPECT_EQ(i * 100, *m[i]);
|
|
EXPECT_FALSE(m.Update(i, i * 100 + 1).is_inserted());
|
|
EXPECT_EQ(i * 100 + 1, *m[i]);
|
|
}
|
|
EXPECT_FALSE(m.Contains(42));
|
|
EXPECT_FALSE(m.Contains(420));
|
|
|
|
ExpectMapElementsAre(
|
|
m, MakeKeyValues([](int k) { return k * 100 + 1; }, llvm::seq(50, 150)));
|
|
|
|
EXPECT_FALSE(m.Erase(42));
|
|
EXPECT_TRUE(m.Contains(73));
|
|
EXPECT_TRUE(m.Erase(73));
|
|
EXPECT_FALSE(m.Contains(73));
|
|
for (int i : llvm::seq(102, 136)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
EXPECT_TRUE(m.Contains(i));
|
|
EXPECT_TRUE(m.Erase(i));
|
|
EXPECT_FALSE(m.Contains(i));
|
|
}
|
|
for (int i : llvm::seq(50, 150)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
if (i == 73 || (i >= 102 && i < 136)) {
|
|
continue;
|
|
}
|
|
ASSERT_TRUE(m.Contains(i));
|
|
EXPECT_EQ(i * 100 + 1, *m[i]);
|
|
EXPECT_FALSE(m.Insert(i, i * 100 + 2).is_inserted());
|
|
EXPECT_EQ(i * 100 + 1, *m[i]);
|
|
EXPECT_FALSE(m.Update(i, i * 100 + 2).is_inserted());
|
|
EXPECT_EQ(i * 100 + 2, *m[i]);
|
|
}
|
|
EXPECT_TRUE(m.Insert(73, 73 * 100 + 3).is_inserted());
|
|
EXPECT_EQ(73 * 100 + 3, *m[73]);
|
|
|
|
ExpectMapElementsAre(
|
|
m, MakeKeyValues([](int k) { return k * 100 + 2 + (k == 73); },
|
|
llvm::seq(50, 102), llvm::seq(136, 150)));
|
|
|
|
// Reset back to empty and small.
|
|
m.Reset();
|
|
EXPECT_FALSE(m.Contains(42));
|
|
EXPECT_EQ(nullptr, m[42]);
|
|
|
|
// Refill but with both overlapping and different values, now triggering
|
|
// growth too. Also, use update instead of insert.
|
|
for (int i : llvm::seq(75, 175)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
EXPECT_TRUE(m.Update(i, i * 100).is_inserted());
|
|
}
|
|
for (int i : llvm::seq(75, 175)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(m.Contains(i));
|
|
EXPECT_EQ(i * 100, *m[i]);
|
|
EXPECT_FALSE(m.Insert(i, i * 100 + 1).is_inserted());
|
|
EXPECT_EQ(i * 100, *m[i]);
|
|
EXPECT_FALSE(m.Update(i, i * 100 + 1).is_inserted());
|
|
EXPECT_EQ(i * 100 + 1, *m[i]);
|
|
}
|
|
EXPECT_FALSE(m.Contains(42));
|
|
EXPECT_FALSE(m.Contains(420));
|
|
|
|
ExpectMapElementsAre(
|
|
m, MakeKeyValues([](int k) { return k * 100 + 1; }, llvm::seq(75, 175)));
|
|
|
|
EXPECT_FALSE(m.Erase(42));
|
|
EXPECT_TRUE(m.Contains(93));
|
|
EXPECT_TRUE(m.Erase(93));
|
|
EXPECT_FALSE(m.Contains(93));
|
|
for (int i : llvm::seq(102, 136)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
EXPECT_TRUE(m.Contains(i));
|
|
EXPECT_TRUE(m.Erase(i));
|
|
EXPECT_FALSE(m.Contains(i));
|
|
}
|
|
for (int i : llvm::seq(75, 175)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
if (i == 93 || (i >= 102 && i < 136)) {
|
|
continue;
|
|
}
|
|
ASSERT_TRUE(m.Contains(i));
|
|
EXPECT_EQ(i * 100 + 1, *m[i]);
|
|
EXPECT_FALSE(m.Insert(i, i * 100 + 2).is_inserted());
|
|
EXPECT_EQ(i * 100 + 1, *m[i]);
|
|
EXPECT_FALSE(m.Update(i, i * 100 + 2).is_inserted());
|
|
EXPECT_EQ(i * 100 + 2, *m[i]);
|
|
}
|
|
EXPECT_TRUE(m.Insert(93, 93 * 100 + 3).is_inserted());
|
|
EXPECT_EQ(93 * 100 + 3, *m[93]);
|
|
|
|
ExpectMapElementsAre(
|
|
m, MakeKeyValues([](int k) { return k * 100 + 2 + (k == 93); },
|
|
llvm::seq(75, 102), llvm::seq(136, 175)));
|
|
}
|
|
|
|
template <typename MapT>
|
|
class MapCollisionTest : public ::testing::Test {};
|
|
|
|
using CollisionTypes = ::testing::Types<
|
|
Map<int, int, 16,
|
|
FixedHashKeyContext<7, /*FixIndexBits*/ true, /*FixTagBits*/ false, 0>>,
|
|
Map<int, int, 16,
|
|
FixedHashKeyContext<7, /*FixIndexBits*/ false, /*FixTagBits*/ true, 0>>,
|
|
Map<int, int, 16,
|
|
FixedHashKeyContext<7, /*FixIndexBits*/ true, /*FixTagBits*/ true, 0>>,
|
|
Map<int, int, 16,
|
|
FixedHashKeyContext<7, /*FixIndexBits*/ true, /*FixTagBits*/ true,
|
|
~static_cast<uint64_t>(0)>>>;
|
|
TYPED_TEST_SUITE(MapCollisionTest, CollisionTypes);
|
|
|
|
TYPED_TEST(MapCollisionTest, Basic) {
|
|
TypeParam m;
|
|
|
|
// Fill the map through a couple of growth steps, verifying at each step. Note
|
|
// that because this is a collision test, we synthesize actively harmful
|
|
// hashes in terms of collisions and so this test is essentially quadratic. We
|
|
// need to keep it relatively small.
|
|
for (int i : llvm::seq(1, 256)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
EXPECT_TRUE(m.Insert(i, i * 100).is_inserted());
|
|
}
|
|
ExpectMapElementsAre(
|
|
m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 256)));
|
|
|
|
// Erase and re-fill from the back.
|
|
for (int i : llvm::seq(192, 256)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
EXPECT_TRUE(m.Erase(i));
|
|
}
|
|
ExpectMapElementsAre(
|
|
m, MakeKeyValues([](int k) { return k * 100; }, llvm::seq(1, 192)));
|
|
for (int i : llvm::seq(192, 256)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
EXPECT_TRUE(m.Insert(i, i * 100 + 1).is_inserted());
|
|
}
|
|
ExpectMapElementsAre(m,
|
|
MakeKeyValues([](int k) { return k * 100 + (k >= 192); },
|
|
llvm::seq(1, 256)));
|
|
|
|
// Erase and re-fill from the front.
|
|
for (int i : llvm::seq(1, 64)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
EXPECT_TRUE(m.Erase(i));
|
|
}
|
|
ExpectMapElementsAre(m,
|
|
MakeKeyValues([](int k) { return k * 100 + (k >= 192); },
|
|
llvm::seq(64, 256)));
|
|
for (int i : llvm::seq(1, 64)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
EXPECT_TRUE(m.Insert(i, i * 100 + 1).is_inserted());
|
|
}
|
|
ExpectMapElementsAre(
|
|
m, MakeKeyValues([](int k) { return k * 100 + (k < 64) + (k >= 192); },
|
|
llvm::seq(1, 256)));
|
|
|
|
// Erase and re-fill from the middle.
|
|
for (int i : llvm::seq(64, 192)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
EXPECT_TRUE(m.Erase(i));
|
|
}
|
|
ExpectMapElementsAre(m, MakeKeyValues([](int k) { return k * 100 + 1; },
|
|
llvm::seq(1, 64), llvm::seq(192, 256)));
|
|
for (int i : llvm::seq(64, 192)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
EXPECT_TRUE(m.Insert(i, i * 100 + 1).is_inserted());
|
|
}
|
|
ExpectMapElementsAre(
|
|
m, MakeKeyValues([](int k) { return k * 100 + 1; }, llvm::seq(1, 256)));
|
|
|
|
// Erase and re-fill from both the back and front.
|
|
for (auto s : {llvm::seq(192, 256), llvm::seq(1, 64)}) {
|
|
for (int i : s) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
EXPECT_TRUE(m.Erase(i));
|
|
}
|
|
}
|
|
ExpectMapElementsAre(
|
|
m, MakeKeyValues([](int k) { return k * 100 + 1; }, llvm::seq(64, 192)));
|
|
for (auto s : {llvm::seq(192, 256), llvm::seq(1, 64)}) {
|
|
for (int i : s) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
EXPECT_TRUE(m.Insert(i, i * 100 + 2).is_inserted());
|
|
}
|
|
}
|
|
ExpectMapElementsAre(
|
|
m,
|
|
MakeKeyValues([](int k) { return k * 100 + 1 + (k < 64) + (k >= 192); },
|
|
llvm::seq(1, 256)));
|
|
|
|
// And update the middle elements in place.
|
|
for (int i : llvm::seq(64, 192)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
EXPECT_FALSE(m.Update(i, i * 100 + 2).is_inserted());
|
|
}
|
|
ExpectMapElementsAre(
|
|
m, MakeKeyValues([](int k) { return k * 100 + 2; }, llvm::seq(1, 256)));
|
|
}
|
|
|
|
TEST(MapContextTest, Basic) {
|
|
llvm::SmallVector<TestData> keys;
|
|
for (int i : llvm::seq(0, 513)) {
|
|
keys.push_back(i * 100000);
|
|
}
|
|
IndexKeyContext<TestData> key_context(keys);
|
|
Map<ssize_t, int, 0, IndexKeyContext<TestData>> m;
|
|
|
|
EXPECT_FALSE(m.Contains(42, key_context));
|
|
EXPECT_TRUE(m.Insert(1, 100, key_context).is_inserted());
|
|
ASSERT_TRUE(m.Contains(1, key_context));
|
|
auto result = m.Lookup(TestData(100000), key_context);
|
|
EXPECT_TRUE(result);
|
|
EXPECT_EQ(1, result.key());
|
|
EXPECT_EQ(100, result.value());
|
|
// Reinsertion doesn't change the value. Also, double check a temporary
|
|
// context.
|
|
auto i_result = m.Insert(1, 101, IndexKeyContext<TestData>(keys));
|
|
EXPECT_FALSE(i_result.is_inserted());
|
|
EXPECT_EQ(100, i_result.value());
|
|
// Update does change the value.
|
|
i_result = m.Update(1, 101, key_context);
|
|
EXPECT_FALSE(i_result.is_inserted());
|
|
EXPECT_EQ(101, i_result.value());
|
|
|
|
// Verify all the elements.
|
|
ExpectMapElementsAre(m, {Pair(1, 101)});
|
|
|
|
// Fill up a bunch to ensure we trigger growth a few times.
|
|
for (int i : llvm::seq(2, 512)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
EXPECT_TRUE(m.Insert(i, i * 100, key_context).is_inserted());
|
|
}
|
|
// Check all the elements, including using the context.
|
|
for (int j : llvm::seq(1, 512)) {
|
|
SCOPED_TRACE(llvm::formatv("Assert key: {0}", j).str());
|
|
ASSERT_EQ(j * 100 + static_cast<int>(j == 1),
|
|
m.Lookup(j, key_context).value());
|
|
ASSERT_EQ(j * 100 + static_cast<int>(j == 1),
|
|
m.Lookup(TestData(j * 100000), key_context).value());
|
|
}
|
|
for (int i : llvm::seq(1, 512)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
EXPECT_FALSE(m.Insert(i, i * 100 + 1, key_context).is_inserted());
|
|
EXPECT_EQ(i * 100 + static_cast<int>(i == 1),
|
|
m.Lookup(i, key_context).value());
|
|
EXPECT_FALSE(m.Update(i, i * 100 + 1, key_context).is_inserted());
|
|
EXPECT_EQ(i * 100 + 1, m.Lookup(i, key_context).value());
|
|
}
|
|
EXPECT_FALSE(m.Contains(0, key_context));
|
|
EXPECT_FALSE(m.Contains(512, key_context));
|
|
|
|
// Verify all the elements.
|
|
ExpectMapElementsAre(
|
|
m, MakeKeyValues([](int k) { return k * 100 + 1; }, llvm::seq(1, 512)));
|
|
}
|
|
|
|
TYPED_TEST(MapTest, Range) {
|
|
using MapT = TypeParam;
|
|
using Range = decltype(std::declval<const MapT&>().entries());
|
|
using Iter = typename Range::Iterator;
|
|
|
|
static_assert(std::forward_iterator<Iter>);
|
|
static_assert(std::same_as<decltype(std::declval<Range>().begin()), Iter>);
|
|
static_assert(std::same_as<decltype(std::declval<Range>().end()), Iter>);
|
|
static_assert(std::ranges::forward_range<Range>);
|
|
static_assert(std::ranges::common_range<Range>);
|
|
|
|
MapT m;
|
|
EXPECT_EQ(m.entries().begin(), m.entries().end());
|
|
for (auto [k, v] : m.entries()) {
|
|
static_cast<void>(k);
|
|
static_cast<void>(v);
|
|
FAIL() << "Empty map range should have no elements";
|
|
}
|
|
|
|
for (int i = 1; i <= 5; ++i) {
|
|
m.Insert(i, i * 10);
|
|
}
|
|
|
|
int count = 0;
|
|
for (const auto& [k, v] : m.entries()) {
|
|
EXPECT_EQ(v, m.Lookup(k).value());
|
|
++count;
|
|
}
|
|
EXPECT_EQ(count, 5);
|
|
|
|
EXPECT_THAT(m.entries(),
|
|
UnorderedElementsAre(Pair(1, 10), Pair(2, 20), Pair(3, 30),
|
|
Pair(4, 40), Pair(5, 50)));
|
|
|
|
using KeyT = typename MapT::KeyT;
|
|
using ValueT = typename MapT::ValueT;
|
|
using KeyContextT = typename MapT::KeyContextT;
|
|
MapView<const KeyT, const ValueT, KeyContextT> cv = m;
|
|
int cv_count = 0;
|
|
for (auto [k, v] : cv.entries()) {
|
|
static_assert(std::is_const_v<std::remove_reference_t<decltype(k)>>);
|
|
static_assert(std::is_const_v<std::remove_reference_t<decltype(v)>>);
|
|
EXPECT_EQ(v, m.Lookup(k).value());
|
|
++cv_count;
|
|
}
|
|
EXPECT_EQ(cv_count, 5);
|
|
EXPECT_THAT(cv.entries(),
|
|
UnorderedElementsAre(Pair(1, 10), Pair(2, 20), Pair(3, 30),
|
|
Pair(4, 40), Pair(5, 50)));
|
|
|
|
for (auto [k, v] : m.entries()) {
|
|
if constexpr (requires { v.value; }) {
|
|
v.value = 99;
|
|
} else {
|
|
v = 99;
|
|
}
|
|
}
|
|
for (const auto& [k, v] : m.entries()) {
|
|
if constexpr (requires { v.value; }) {
|
|
EXPECT_EQ(v.value, 99);
|
|
} else {
|
|
EXPECT_EQ(v, 99);
|
|
}
|
|
}
|
|
EXPECT_THAT(m.entries(),
|
|
UnorderedElementsAre(Pair(1, 99), Pair(2, 99), Pair(3, 99),
|
|
Pair(4, 99), Pair(5, 99)));
|
|
|
|
auto r = m.entries();
|
|
int iter_count = 0;
|
|
for (auto it = r.begin(); it != r.end(); ++it) {
|
|
EXPECT_EQ(it->second, m.Lookup(it->first).value());
|
|
EXPECT_EQ((*it).second, m.Lookup((*it).first).value());
|
|
++iter_count;
|
|
}
|
|
EXPECT_EQ(iter_count, 5);
|
|
|
|
auto it = r.begin();
|
|
auto prev = it++;
|
|
EXPECT_NE(it, prev);
|
|
}
|
|
|
|
TYPED_TEST(MoveOnlyMapTest, Range) {
|
|
TypeParam m;
|
|
m.Insert(1, 10);
|
|
m.Insert(2, 20);
|
|
|
|
int count = 0;
|
|
for (auto [k, v] : m.entries()) {
|
|
EXPECT_EQ(v.value, k.value * 10);
|
|
++count;
|
|
}
|
|
EXPECT_EQ(count, 2);
|
|
}
|
|
|
|
#ifndef NDEBUG
|
|
TEST(MapDeathTest, MutateDuringIterationFails) {
|
|
EXPECT_DEATH(([] {
|
|
Map<int, int> m;
|
|
m.Insert(1, 10);
|
|
auto range = m.entries();
|
|
m.Insert(2, 20);
|
|
}()),
|
|
"Hashtable mutated during iteration");
|
|
}
|
|
#endif
|
|
|
|
} // namespace
|
|
} // namespace Carbon::Testing
|