mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 11:20:12 +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
617 lines
20 KiB
C++
617 lines
20 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/set.h"
|
|
|
|
#include <gmock/gmock.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <concepts>
|
|
#include <initializer_list>
|
|
#include <iterator>
|
|
#include <ranges>
|
|
#include <set>
|
|
#include <string>
|
|
#include <type_traits>
|
|
#include <vector>
|
|
|
|
#include "common/raw_hashtable_test_helpers.h"
|
|
|
|
namespace Carbon {
|
|
namespace {
|
|
|
|
using RawHashtable::IndexKeyContext;
|
|
using RawHashtable::MoveOnlyTestData;
|
|
using RawHashtable::TestData;
|
|
using ::testing::UnorderedElementsAre;
|
|
using ::testing::UnorderedElementsAreArray;
|
|
|
|
template <typename SetT, typename MatcherRangeT>
|
|
auto ExpectSetElementsAre(SetT&& s, MatcherRangeT element_matchers) -> void {
|
|
// Collect the elements into a container.
|
|
using KeyT = std::remove_reference<SetT>::type::KeyT;
|
|
std::vector<std::reference_wrapper<KeyT>> entries;
|
|
for (auto& k : s.entries()) {
|
|
entries.push_back(std::ref(k));
|
|
}
|
|
|
|
// Use the GoogleMock unordered container matcher to validate and show errors
|
|
// on wrong elements.
|
|
EXPECT_THAT(entries, UnorderedElementsAreArray(element_matchers));
|
|
}
|
|
|
|
// Allow directly using an initializer list.
|
|
template <typename SetT, typename MatcherT>
|
|
auto ExpectSetElementsAre(SetT&& s,
|
|
std::initializer_list<MatcherT> element_matchers)
|
|
-> void {
|
|
std::vector<MatcherT> element_matchers_storage = element_matchers;
|
|
ExpectSetElementsAre(s, element_matchers_storage);
|
|
}
|
|
|
|
template <typename RangeT, typename... RangeTs>
|
|
auto MakeElements(RangeT&& range, RangeTs&&... ranges) {
|
|
std::vector<typename RangeT::value_type> elements;
|
|
auto add_range = [&elements](RangeT&& r) {
|
|
for (const auto&& e : r) {
|
|
elements.push_back(e);
|
|
}
|
|
};
|
|
add_range(std::forward<RangeT>(range));
|
|
(add_range(std::forward<RangeTs>(ranges)), ...);
|
|
|
|
return elements;
|
|
}
|
|
|
|
template <typename SetT>
|
|
class SetTest : public ::testing::Test {};
|
|
|
|
template <typename SetT>
|
|
class MoveOnlySetTest : public ::testing::Test {};
|
|
|
|
using Types = ::testing::Types<Set<int>, Set<int, 16>, Set<int, 128>,
|
|
Set<TestData>, Set<TestData, 16>>;
|
|
TYPED_TEST_SUITE(SetTest, Types);
|
|
|
|
using MoveOnlyTypes =
|
|
::testing::Types<Set<MoveOnlyTestData>, Set<MoveOnlyTestData, 16>,
|
|
Set<MoveOnlyTestData, 64>>;
|
|
TYPED_TEST_SUITE(MoveOnlySetTest, MoveOnlyTypes);
|
|
|
|
TYPED_TEST(SetTest, Basic) {
|
|
using SetT = TypeParam;
|
|
SetT s;
|
|
|
|
EXPECT_FALSE(s.Contains(42));
|
|
EXPECT_TRUE(s.Insert(1).is_inserted());
|
|
EXPECT_TRUE(s.Contains(1));
|
|
auto result = s.Lookup(1);
|
|
EXPECT_TRUE(result);
|
|
EXPECT_EQ(1, result.key());
|
|
auto i_result = s.Insert(1);
|
|
EXPECT_FALSE(i_result.is_inserted());
|
|
EXPECT_TRUE(s.Contains(1));
|
|
|
|
// Verify all the elements.
|
|
ExpectSetElementsAre(s, {1});
|
|
|
|
// 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(s.Insert(i).is_inserted());
|
|
}
|
|
for (int i : llvm::seq(1, 512)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
EXPECT_TRUE(s.Contains(i));
|
|
EXPECT_FALSE(s.Insert(i).is_inserted());
|
|
}
|
|
EXPECT_FALSE(s.Contains(513));
|
|
|
|
// Verify all the elements.
|
|
ExpectSetElementsAre(s, MakeElements(llvm::seq(1, 512)));
|
|
}
|
|
|
|
TYPED_TEST(SetTest, FactoryApi) {
|
|
using SetT = TypeParam;
|
|
SetT s;
|
|
EXPECT_TRUE(s.Insert(1, [](int k, void* key_storage) {
|
|
return new (key_storage) int(k);
|
|
}).is_inserted());
|
|
ASSERT_TRUE(s.Contains(1));
|
|
// Reinsertion doesn't invoke the callback.
|
|
EXPECT_FALSE(s.Insert(1, [](int, void*) -> int* {
|
|
llvm_unreachable("Should never be called!");
|
|
}).is_inserted());
|
|
}
|
|
|
|
TYPED_TEST(SetTest, Copy) {
|
|
using SetT = TypeParam;
|
|
|
|
SetT s;
|
|
// Make sure we exceed the small size for some of the set 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(s.Insert(i).is_inserted());
|
|
}
|
|
|
|
SetT other_s1 = s;
|
|
ExpectSetElementsAre(other_s1, MakeElements(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(s.Insert(i).is_inserted());
|
|
}
|
|
|
|
// The first copy doesn't change.
|
|
ExpectSetElementsAre(other_s1, MakeElements(llvm::seq(1, 24)));
|
|
|
|
// A new copy does.
|
|
SetT other_s2 = s;
|
|
ExpectSetElementsAre(other_s2, MakeElements(llvm::seq(1, 32)));
|
|
|
|
// Copy-assign updates.
|
|
other_s1 = s;
|
|
ExpectSetElementsAre(other_s1, MakeElements(llvm::seq(1, 32)));
|
|
|
|
// Self-assign is a no-op.
|
|
other_s1 = const_cast<const SetT&>(other_s1);
|
|
ExpectSetElementsAre(other_s1, MakeElements(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(s.Insert(i).is_inserted());
|
|
}
|
|
ExpectSetElementsAre(other_s1, MakeElements(llvm::seq(1, 32)));
|
|
ExpectSetElementsAre(other_s2, MakeElements(llvm::seq(1, 32)));
|
|
}
|
|
|
|
TYPED_TEST(SetTest, Move) {
|
|
using SetT = TypeParam;
|
|
|
|
SetT s;
|
|
// Make sure we exceed the small size for some of the set 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());
|
|
EXPECT_TRUE(s.Insert(i).is_inserted());
|
|
}
|
|
|
|
SetT other_s1 = std::move(s);
|
|
ExpectSetElementsAre(other_s1, MakeElements(llvm::seq(1, 24)));
|
|
// A moved-from set has a size but no storage, and must iterate as empty.
|
|
EXPECT_EQ(s.entries().begin(), s.entries().end());
|
|
|
|
// Add some more elements.
|
|
for (int i : llvm::seq(24, 32)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(other_s1.Insert(i).is_inserted());
|
|
}
|
|
ExpectSetElementsAre(other_s1, MakeElements(llvm::seq(1, 32)));
|
|
|
|
// Move back over a moved-from.
|
|
s = std::move(other_s1);
|
|
ExpectSetElementsAre(s, MakeElements(llvm::seq(1, 32)));
|
|
|
|
// Copy over moved-from state also works.
|
|
other_s1 = s;
|
|
ExpectSetElementsAre(other_s1, MakeElements(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_s1.Insert(i).is_inserted());
|
|
}
|
|
ExpectSetElementsAre(other_s1, MakeElements(llvm::seq(1, 48)));
|
|
|
|
// Move-assign over the copy looks like the moved-from table not the copy.
|
|
other_s1 = std::move(s);
|
|
ExpectSetElementsAre(other_s1, MakeElements(llvm::seq(1, 32)));
|
|
|
|
// Self-swap (which does a self-move) works and is a no-op.
|
|
std::swap(other_s1, other_s1);
|
|
ExpectSetElementsAre(other_s1, MakeElements(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.
|
|
SetT other_s2 = other_s1;
|
|
// NOLINTNEXTLINE(bugprone-use-after-move): Testing required use-after-move.
|
|
other_s2 = s;
|
|
other_s1 = std::move(other_s1);
|
|
s = std::move(s);
|
|
}
|
|
|
|
TYPED_TEST(MoveOnlySetTest, Move) {
|
|
using SetT = TypeParam;
|
|
static_assert(!std::is_copy_assignable_v<SetT>);
|
|
static_assert(!std::is_copy_constructible_v<SetT>);
|
|
static_assert(std::is_move_assignable_v<SetT>);
|
|
static_assert(std::is_move_constructible_v<SetT>);
|
|
|
|
auto make_set = [] {
|
|
SetT s;
|
|
// Make sure we exceed the small size for some of the set 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());
|
|
EXPECT_TRUE(s.Insert(i).is_inserted());
|
|
}
|
|
return s;
|
|
};
|
|
|
|
SetT s = make_set();
|
|
|
|
SetT other_s1 = std::move(s);
|
|
ExpectSetElementsAre(other_s1, MakeElements(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_s1.Insert(i).is_inserted());
|
|
}
|
|
ExpectSetElementsAre(other_s1, MakeElements(llvm::seq(1, 32)));
|
|
|
|
// Move back over a moved-from.
|
|
s = std::move(other_s1);
|
|
ExpectSetElementsAre(s, MakeElements(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(s.Insert(i).is_inserted());
|
|
}
|
|
ExpectSetElementsAre(s, MakeElements(llvm::seq(1, 72)));
|
|
|
|
// Assignment replaces the contents.
|
|
s = make_set();
|
|
ExpectSetElementsAre(s, MakeElements(llvm::seq(1, 24)));
|
|
|
|
// Self-swap (which does a self-move) works and is a no-op.
|
|
std::swap(s, s);
|
|
ExpectSetElementsAre(s, MakeElements(llvm::seq(1, 24)));
|
|
}
|
|
|
|
TYPED_TEST(SetTest, Conversions) {
|
|
using SetT = TypeParam;
|
|
using KeyT = SetT::KeyT;
|
|
SetT s;
|
|
ASSERT_TRUE(s.Insert(1).is_inserted());
|
|
ASSERT_TRUE(s.Insert(2).is_inserted());
|
|
ASSERT_TRUE(s.Insert(3).is_inserted());
|
|
ASSERT_TRUE(s.Insert(4).is_inserted());
|
|
|
|
SetView<KeyT> sv = s;
|
|
SetView<const KeyT> csv = sv;
|
|
SetView<const KeyT> csv2 = s;
|
|
EXPECT_TRUE(sv.Contains(1));
|
|
EXPECT_TRUE(csv.Contains(2));
|
|
EXPECT_TRUE(csv2.Contains(3));
|
|
}
|
|
|
|
TYPED_TEST(SetTest, GrowToAllocSize) {
|
|
using SetT = TypeParam;
|
|
|
|
SetT s;
|
|
// Grow when empty. May be a no-op for some small sizes.
|
|
s.GrowToAllocSize(32);
|
|
|
|
// Add some elements that will need to be propagated through subsequent
|
|
// growths. Also delete some.
|
|
ssize_t storage_bytes = s.ComputeMetrics().storage_bytes;
|
|
for (int i : llvm::seq(1, 24)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(s.Insert(i).is_inserted());
|
|
}
|
|
for (int i : llvm::seq(1, 8)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(s.Erase(i));
|
|
}
|
|
// No further growth triggered.
|
|
EXPECT_EQ(storage_bytes, s.ComputeMetrics().storage_bytes);
|
|
|
|
// No-op.
|
|
s.GrowToAllocSize(16);
|
|
ExpectSetElementsAre(s, MakeElements(llvm::seq(8, 24)));
|
|
// No further growth triggered.
|
|
EXPECT_EQ(storage_bytes, s.ComputeMetrics().storage_bytes);
|
|
|
|
// Get a few doubling based growths, and at least one beyond the largest small
|
|
// size.
|
|
s.GrowToAllocSize(64);
|
|
ExpectSetElementsAre(s, MakeElements(llvm::seq(8, 24)));
|
|
s.GrowToAllocSize(128);
|
|
ExpectSetElementsAre(s, MakeElements(llvm::seq(8, 24)));
|
|
s.GrowToAllocSize(256);
|
|
ExpectSetElementsAre(s, MakeElements(llvm::seq(8, 24)));
|
|
// Update the storage bytes after growth.
|
|
EXPECT_LT(storage_bytes, s.ComputeMetrics().storage_bytes);
|
|
storage_bytes = s.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(s.Insert(i).is_inserted());
|
|
}
|
|
for (int i : llvm::seq(8, 16)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(s.Erase(i));
|
|
}
|
|
// No growth from insertions.
|
|
EXPECT_EQ(storage_bytes, s.ComputeMetrics().storage_bytes);
|
|
|
|
s.GrowToAllocSize(1024);
|
|
ExpectSetElementsAre(s, MakeElements(llvm::seq(16, 48)));
|
|
// Storage should have grown.
|
|
EXPECT_LT(storage_bytes, s.ComputeMetrics().storage_bytes);
|
|
}
|
|
|
|
TYPED_TEST(SetTest, GrowForInsert) {
|
|
using SetT = TypeParam;
|
|
|
|
SetT s;
|
|
s.GrowForInsertCount(42);
|
|
ssize_t storage_bytes = s.ComputeMetrics().storage_bytes;
|
|
for (int i : llvm::seq(1, 42)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(s.Insert(i).is_inserted());
|
|
}
|
|
ExpectSetElementsAre(s, MakeElements(llvm::seq(1, 42)));
|
|
EXPECT_EQ(storage_bytes, s.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(s.Erase(i));
|
|
}
|
|
s.GrowForInsertCount(42);
|
|
storage_bytes = s.ComputeMetrics().storage_bytes;
|
|
for (int i : llvm::seq(42, 84)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(s.Insert(i).is_inserted());
|
|
}
|
|
ExpectSetElementsAre(s, MakeElements(llvm::seq(32, 84)));
|
|
EXPECT_EQ(storage_bytes, s.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(s.Erase(i));
|
|
}
|
|
s.GrowForInsertCount(321);
|
|
storage_bytes = s.ComputeMetrics().storage_bytes;
|
|
for (int i : llvm::seq(128, 321 + 128)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
ASSERT_TRUE(s.Insert(i).is_inserted());
|
|
}
|
|
ExpectSetElementsAre(s, MakeElements(llvm::seq(128, 321 + 128)));
|
|
EXPECT_EQ(storage_bytes, s.ComputeMetrics().storage_bytes);
|
|
}
|
|
|
|
TEST(SetContextTest, Basic) {
|
|
llvm::SmallVector<TestData> keys;
|
|
for (int i : llvm::seq(0, 513)) {
|
|
keys.push_back(i * 100);
|
|
}
|
|
IndexKeyContext<TestData> key_context(keys);
|
|
Set<ssize_t, 0, IndexKeyContext<TestData>> s;
|
|
|
|
EXPECT_FALSE(s.Contains(42, key_context));
|
|
EXPECT_TRUE(s.Insert(1, key_context).is_inserted());
|
|
EXPECT_TRUE(s.Contains(1, key_context));
|
|
auto result = s.Lookup(TestData(100), key_context);
|
|
EXPECT_TRUE(result);
|
|
EXPECT_EQ(1, result.key());
|
|
auto i_result = s.Insert(1, IndexKeyContext<TestData>(keys));
|
|
EXPECT_FALSE(i_result.is_inserted());
|
|
EXPECT_TRUE(s.Contains(1, key_context));
|
|
EXPECT_TRUE(
|
|
s.Insert(TestData(200), [] { return 2; }, key_context).is_inserted());
|
|
EXPECT_TRUE(s.Contains(2, key_context));
|
|
EXPECT_TRUE(s.Contains(TestData(200), key_context));
|
|
|
|
// Verify all the elements.
|
|
ExpectSetElementsAre(s, {1, 2});
|
|
|
|
// Fill up a bunch to ensure we trigger growth a few times.
|
|
for (int i : llvm::seq(3, 512)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
EXPECT_TRUE(s.Insert(i, key_context).is_inserted());
|
|
}
|
|
for (int i : llvm::seq(1, 512)) {
|
|
SCOPED_TRACE(llvm::formatv("Key: {0}", i).str());
|
|
EXPECT_TRUE(s.Contains(i, key_context));
|
|
EXPECT_FALSE(s.Insert(i, key_context).is_inserted());
|
|
}
|
|
EXPECT_FALSE(s.Contains(0, key_context));
|
|
EXPECT_FALSE(s.Contains(512, key_context));
|
|
EXPECT_FALSE(s.Contains(TestData(0), key_context));
|
|
EXPECT_FALSE(s.Contains(TestData(51200), key_context));
|
|
|
|
// Verify all the elements.
|
|
ExpectSetElementsAre(s, MakeElements(llvm::seq(1, 512)));
|
|
}
|
|
|
|
TYPED_TEST(SetTest, Range) {
|
|
using SetT = TypeParam;
|
|
using Range = decltype(std::declval<const SetT&>().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>);
|
|
|
|
SetT s;
|
|
EXPECT_EQ(s.entries().begin(), s.entries().end());
|
|
for (const auto& k : s.entries()) {
|
|
static_cast<void>(k);
|
|
FAIL() << "Empty set range should have no elements";
|
|
}
|
|
|
|
for (int i = 1; i <= 5; ++i) {
|
|
s.Insert(i);
|
|
}
|
|
|
|
// Range-for traversal by const ref.
|
|
int count = 0;
|
|
for (const auto& k : s.entries()) {
|
|
EXPECT_GE(k, 1);
|
|
EXPECT_LE(k, 5);
|
|
++count;
|
|
}
|
|
EXPECT_EQ(count, 5);
|
|
|
|
// Direct GMock container matching.
|
|
EXPECT_THAT(s.entries(), UnorderedElementsAre(1, 2, 3, 4, 5));
|
|
|
|
// Const view range iteration.
|
|
using KeyT = typename SetT::KeyT;
|
|
using KeyContextT = typename SetT::KeyContextT;
|
|
SetView<const KeyT, KeyContextT> cv = s;
|
|
int cv_count = 0;
|
|
for (const auto& k : cv.entries()) {
|
|
static_assert(std::is_const_v<std::remove_reference_t<decltype(k)>>);
|
|
EXPECT_GE(k, 1);
|
|
EXPECT_LE(k, 5);
|
|
++cv_count;
|
|
}
|
|
EXPECT_EQ(cv_count, 5);
|
|
EXPECT_THAT(cv.entries(), UnorderedElementsAre(1, 2, 3, 4, 5));
|
|
|
|
// Explicit iterator traversal, dereference, and post-increment.
|
|
auto r = s.entries();
|
|
int iter_count = 0;
|
|
for (auto it = r.begin(); it != r.end(); ++it) {
|
|
EXPECT_NE(*it, 0);
|
|
++iter_count;
|
|
}
|
|
EXPECT_EQ(iter_count, 5);
|
|
|
|
auto it = r.begin();
|
|
auto prev = it++;
|
|
EXPECT_NE(it, prev);
|
|
}
|
|
|
|
TYPED_TEST(MoveOnlySetTest, Range) {
|
|
TypeParam s;
|
|
s.Insert(1);
|
|
s.Insert(2);
|
|
|
|
int count = 0;
|
|
for (const auto& k : s.entries()) {
|
|
EXPECT_GT(k.value, 0);
|
|
++count;
|
|
}
|
|
EXPECT_EQ(count, 2);
|
|
}
|
|
|
|
#ifndef NDEBUG
|
|
TEST(SetDeathTest, MutateDuringIterationFails) {
|
|
EXPECT_DEATH(([] {
|
|
Set<int> s;
|
|
s.Insert(1);
|
|
auto range = s.entries();
|
|
s.Insert(2);
|
|
}()),
|
|
"Hashtable mutated during iteration");
|
|
}
|
|
#endif
|
|
|
|
// A range outlives the *view* it was built from: views don't own storage, and
|
|
// the range copies the view rather than pointing at it.
|
|
TEST(SetTest, RangeOutlivesTemporaryView) {
|
|
Set<int> s;
|
|
s.Insert(1);
|
|
|
|
auto make_view = [&s]() -> SetView<int> { return s; };
|
|
auto range = make_view().entries();
|
|
EXPECT_THAT(range, UnorderedElementsAre(1));
|
|
}
|
|
|
|
#ifdef NDEBUG
|
|
// Release iteration state is two end pointers, a group offset, and the
|
|
// present-bit mask; it needs to stay small enough to live in registers across
|
|
// the loop. Debug builds add the randomized walk and mutation-check state.
|
|
static_assert(sizeof(Set<int>::Range::Iterator) <= 4 * sizeof(void*));
|
|
#endif
|
|
|
|
// Forward ranges guarantee multi-pass: `begin()` must be a pure function of the
|
|
// range. Debug builds draw their traversal entropy when the range is
|
|
// constructed rather than in `begin()` precisely so that repeated calls start
|
|
// from the same group.
|
|
TEST(SetTest, RangeIsMultiPass) {
|
|
Set<int, 16> s;
|
|
for (int i = 1; i <= 64; ++i) {
|
|
s.Insert(i);
|
|
}
|
|
|
|
auto range = s.entries();
|
|
EXPECT_EQ(range.begin(), range.begin());
|
|
|
|
// Two passes over the same range must agree on both the keys visited and the
|
|
// order they're visited in.
|
|
std::vector<int> first;
|
|
for (int k : range) {
|
|
first.push_back(k);
|
|
}
|
|
std::vector<int> second;
|
|
for (int k : range) {
|
|
second.push_back(k);
|
|
}
|
|
EXPECT_EQ(first, second);
|
|
EXPECT_EQ(static_cast<ssize_t>(first.size()), 64);
|
|
}
|
|
|
|
// Whatever order a range picks, it has to be a genuine permutation of the
|
|
// table. Debug builds additionally vary that order between ranges over the same
|
|
// table so that callers can't come to depend on it.
|
|
TEST(SetTest, TraversalOrderIsAVaryingPermutation) {
|
|
Set<int, 64> s;
|
|
std::vector<int> inserted;
|
|
// Enough keys to populate every group of the small storage.
|
|
for (int i = 0; i < 36; ++i) {
|
|
int key = i * 17 + 7;
|
|
EXPECT_TRUE(s.Insert(key).is_inserted());
|
|
inserted.push_back(key);
|
|
}
|
|
|
|
std::set<std::vector<int>> distinct_orders;
|
|
for (int i = 0; i < 64; ++i) {
|
|
std::vector<int> visited;
|
|
for (int k : s.entries()) {
|
|
visited.push_back(k);
|
|
}
|
|
// A walk that skipped a group would drop keys and one that revisited a
|
|
// group would duplicate them, so comparing as a multiset covers both. This
|
|
// is what makes an odd group stride a valid traversal.
|
|
EXPECT_THAT(visited, UnorderedElementsAreArray(inserted));
|
|
distinct_orders.insert(visited);
|
|
}
|
|
|
|
#ifndef NDEBUG
|
|
// Debug builds randomize both the starting group and the stride, so across
|
|
// this many ranges we should see more than the two orders (pure forward and
|
|
// pure reverse) that a simple direction flip would produce.
|
|
EXPECT_GT(distinct_orders.size(), 2)
|
|
<< "Debug traversal order does not appear to be randomized.";
|
|
#else
|
|
// Release builds always scan the groups in order.
|
|
EXPECT_EQ(distinct_orders.size(), 1);
|
|
#endif
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace Carbon
|