Skip to content

Commit 1d1ad22

Browse files
Abseil Teamderekmauro
Abseil Team
authored andcommitted
Export of internal Abseil changes
-- f9476c95cf7625d7b0fc4661f253b0aac4341044 by Abseil Team <absl-team@google.com>: Add a test to verify that the new checksum field in Hashtablez is calculated PiperOrigin-RevId: 356744293 -- ff8a3612463000e8c3d451e50367a3c65cb6cf21 by Abseil Team <absl-team@google.com>: Remove the implied support comment for port.h, attributes.h, and integral_types.h's C compatibility from the header documentations. Abseil-cpp is a C++ library; this brings port.h, attributes.h, and integral_types.h, into our stance for the rest of Abseil (aka, no assurance of C compatibility) There is no guarantee that future changes to port.h, attributes.h, and integral_types.h, and their dependencies, will remain compatible with C, even for macros and definitions that currently are. PiperOrigin-RevId: 356727505 -- be62292016381deee628dbb3f36cb6009bcc0282 by Abseil Team <absl-team@google.com>: internal change PiperOrigin-RevId: 356608125 -- 13b35f17171df3d6853ea7088797b3be611505fc by Evan Brown <ezb@google.com>: Clarify the comments for CapacityToGrowth/GrowthToLowerboundCapacity methods to specify the intent that capacity should equal growth when `capacity+1 < kWidth`. Also add testing for this behavior. PiperOrigin-RevId: 356579041 GitOrigin-RevId: f9476c95cf7625d7b0fc4661f253b0aac4341044 Change-Id: Iadd094d109b4869998f2427319ef66d1cf1e8eff
1 parent 2aa00ab commit 1d1ad22

File tree

5 files changed

+40
-12
lines changed

5 files changed

+40
-12
lines changed

absl/base/attributes.h

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
// These macros are used within Abseil and allow the compiler to optimize, where
1919
// applicable, certain function calls.
2020
//
21-
// This file is used for both C and C++!
22-
//
2321
// Most macros here are exposing GCC or Clang features, and are stubbed out for
2422
// other compilers.
2523
//

absl/base/port.h

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
//
1515
// This files is a forwarding header for other headers containing various
1616
// portability macros and functions.
17-
// This file is used for both C and C++!
1817

1918
#ifndef ABSL_BASE_PORT_H_
2019
#define ABSL_BASE_PORT_H_

absl/container/internal/raw_hash_set.h

+11-3
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,16 @@ inline size_t NormalizeCapacity(size_t n) {
468468
return n ? ~size_t{} >> countl_zero(n) : 1;
469469
}
470470

471-
// We use 7/8th as maximum load factor.
472-
// For 16-wide groups, that gives an average of two empty slots per group.
471+
// General notes on capacity/growth methods below:
472+
// - We use 7/8th as maximum load factor. For 16-wide groups, that gives an
473+
// average of two empty slots per group.
474+
// - For (capacity+1) >= Group::kWidth, growth is 7/8*capacity.
475+
// - For (capacity+1) < Group::kWidth, growth == capacity. In this case, we
476+
// never need to probe (the whole table fits in one group) so we don't need a
477+
// load factor less than 1.
478+
479+
// Given `capacity` of the table, returns the size (i.e. number of full slots)
480+
// at which we should grow the capacity.
473481
inline size_t CapacityToGrowth(size_t capacity) {
474482
assert(IsValidCapacity(capacity));
475483
// `capacity*7/8`
@@ -480,7 +488,7 @@ inline size_t CapacityToGrowth(size_t capacity) {
480488
return capacity - capacity / 8;
481489
}
482490
// From desired "growth" to a lowerbound of the necessary capacity.
483-
// Might not be a valid one and required NormalizeCapacity().
491+
// Might not be a valid one and requires NormalizeCapacity().
484492
inline size_t GrowthToLowerboundCapacity(size_t growth) {
485493
// `growth*8/7`
486494
if (Group::kWidth == 8 && growth == 7) {

absl/container/internal/raw_hash_set_test.cc

+29-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "absl/container/internal/raw_hash_set.h"
1616

17+
#include <atomic>
1718
#include <cmath>
1819
#include <cstdint>
1920
#include <deque>
@@ -22,6 +23,8 @@
2223
#include <numeric>
2324
#include <random>
2425
#include <string>
26+
#include <unordered_map>
27+
#include <unordered_set>
2528

2629
#include "gmock/gmock.h"
2730
#include "gtest/gtest.h"
@@ -48,11 +51,10 @@ struct RawHashSetTestOnlyAccess {
4851

4952
namespace {
5053

51-
using ::testing::DoubleNear;
5254
using ::testing::ElementsAre;
55+
using ::testing::Eq;
5356
using ::testing::Ge;
5457
using ::testing::Lt;
55-
using ::testing::Optional;
5658
using ::testing::Pair;
5759
using ::testing::UnorderedElementsAre;
5860

@@ -75,8 +77,14 @@ TEST(Util, GrowthAndCapacity) {
7577
for (size_t growth = 0; growth < 10000; ++growth) {
7678
SCOPED_TRACE(growth);
7779
size_t capacity = NormalizeCapacity(GrowthToLowerboundCapacity(growth));
78-
// The capacity is large enough for `growth`
80+
// The capacity is large enough for `growth`.
7981
EXPECT_THAT(CapacityToGrowth(capacity), Ge(growth));
82+
// For (capacity+1) < kWidth, growth should equal capacity.
83+
if (capacity + 1 < Group::kWidth) {
84+
EXPECT_THAT(CapacityToGrowth(capacity), Eq(capacity));
85+
} else {
86+
EXPECT_THAT(CapacityToGrowth(capacity), Lt(capacity));
87+
}
8088
if (growth != 0 && capacity > 1) {
8189
// There is no smaller capacity that works.
8290
EXPECT_THAT(CapacityToGrowth(capacity / 2), Lt(growth));
@@ -1875,18 +1883,34 @@ TEST(RawHashSamplerTest, Sample) {
18751883

18761884
auto& sampler = HashtablezSampler::Global();
18771885
size_t start_size = 0;
1878-
start_size += sampler.Iterate([&](const HashtablezInfo&) { ++start_size; });
1886+
std::unordered_set<const HashtablezInfo*> preexisting_info;
1887+
start_size += sampler.Iterate([&](const HashtablezInfo& info) {
1888+
preexisting_info.insert(&info);
1889+
++start_size;
1890+
});
18791891

18801892
std::vector<IntTable> tables;
18811893
for (int i = 0; i < 1000000; ++i) {
18821894
tables.emplace_back();
18831895
tables.back().insert(1);
1896+
tables.back().insert(i % 5);
18841897
}
18851898
size_t end_size = 0;
1886-
end_size += sampler.Iterate([&](const HashtablezInfo&) { ++end_size; });
1899+
std::unordered_map<size_t, int> observed_checksums;
1900+
end_size += sampler.Iterate([&](const HashtablezInfo& info) {
1901+
if (preexisting_info.count(&info) == 0) {
1902+
observed_checksums[info.hashes_bitwise_xor.load(
1903+
std::memory_order_relaxed)]++;
1904+
}
1905+
++end_size;
1906+
});
18871907

18881908
EXPECT_NEAR((end_size - start_size) / static_cast<double>(tables.size()),
18891909
0.01, 0.005);
1910+
EXPECT_EQ(observed_checksums.size(), 5);
1911+
for (const auto& [_, count] : observed_checksums) {
1912+
EXPECT_NEAR((100 * count) / static_cast<double>(tables.size()), 0.2, 0.05);
1913+
}
18901914
}
18911915
#endif // ABSL_INTERNAL_HASHTABLEZ_SAMPLE
18921916

absl/status/status_test.cc

-1
Original file line numberDiff line numberDiff line change
@@ -460,5 +460,4 @@ TEST(Status, Swap) {
460460
test_swap(no_payload, with_payload);
461461
test_swap(with_payload, no_payload);
462462
}
463-
464463
} // namespace

0 commit comments

Comments
 (0)