Export of internal Abseil changes

--
2c5c118f0615ba90e48ee2f18eccc9f511740f6d by Samuel Benzaquen <sbenza@google.com>:

Rename internal macros to follow the convention in absl.

PiperOrigin-RevId: 299906738

--
92d84a707c7ebc4ec19bdd92d5765d1b6d218c1e by Derek Mauro <dmauro@google.com>:

Import GitHub #629: Skip the .exe suffix in the helpshort filter on Windows

PiperOrigin-RevId: 299892396

--
2a6910d4be6c67a8376628764121b528ff53504d by Abseil Team <absl-team@google.com>:

Use unsigned int128 intrinsic when available. It generates better branchless code.

PiperOrigin-RevId: 299848585

--
110c16cf0a739e1df5028fb6fbd03ef5dde1d278 by Derek Mauro <dmauro@google.com>:

Import GitHub #594: Avoid reading the registry for Windows UWP apps

PiperOrigin-RevId: 299821671

--
d8397d367e88163e5e8a47f379c716352dc91d03 by Greg Falcon <gfalcon@google.com>:

Add absl::Hash support for Cord.  The hash function is heterogeneous with other string types: a Cord and a string with the same byte sequence will hash to the same value.

SwissTable types know about Cord, and will allow heterogeneous lookup (e.g., you can pass a Cord to flat_hash_map<string, T>::find(), and vice versa.)

Add a missing dependency to the cmake Cord target.

PiperOrigin-RevId: 299443713
GitOrigin-RevId: 2c5c118f0615ba90e48ee2f18eccc9f511740f6d
Change-Id: I7b087c7984b0cb52c4b337d49266c467b98ebdf9
This commit is contained in:
Abseil Team 2020-03-09 12:34:31 -07:00 committed by Derek Mauro
parent 238b9a59c8
commit d936052d32
21 changed files with 274 additions and 51 deletions

View file

@ -72,10 +72,10 @@ static int GetNumCPUs() {
#if defined(_WIN32) #if defined(_WIN32)
static double GetNominalCPUFrequency() { static double GetNominalCPUFrequency() {
// UWP apps don't have access to the registry and currently don't provide an
// API informing about CPU nominal frequency.
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && \ #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && \
!WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
// UWP apps don't have access to the registry and currently don't provide an
// API informing about CPU nominal frequency.
return 1.0; return 1.0;
#else #else
#pragma comment(lib, "advapi32.lib") // For Reg* functions. #pragma comment(lib, "advapi32.lib") // For Reg* functions.
@ -97,7 +97,7 @@ static double GetNominalCPUFrequency() {
} }
} }
return 1.0; return 1.0;
#endif // WINAPI_PARTITION_APP && !WINAPI_PARTITION_DESKTOP #endif // WINAPI_PARTITION_APP && !WINAPI_PARTITION_DESKTOP
} }
#elif defined(CTL_HW) && defined(HW_CPU_FREQ) #elif defined(CTL_HW) && defined(HW_CPU_FREQ)

View file

@ -390,6 +390,7 @@ cc_library(
"//absl/base:config", "//absl/base:config",
"//absl/hash", "//absl/hash",
"//absl/strings", "//absl/strings",
"//absl/strings:cord",
], ],
) )
@ -402,7 +403,10 @@ cc_test(
deps = [ deps = [
":hash_function_defaults", ":hash_function_defaults",
"//absl/hash", "//absl/hash",
"//absl/random",
"//absl/strings", "//absl/strings",
"//absl/strings:cord",
"//absl/strings:cord_test_helpers",
"@com_google_googletest//:gtest_main", "@com_google_googletest//:gtest_main",
], ],
) )
@ -828,6 +832,7 @@ cc_library(
"//absl/memory", "//absl/memory",
"//absl/meta:type_traits", "//absl/meta:type_traits",
"//absl/strings", "//absl/strings",
"//absl/strings:cord",
"//absl/types:compare", "//absl/types:compare",
"//absl/utility", "//absl/utility",
], ],
@ -844,6 +849,7 @@ cc_library(
":btree", ":btree",
":flat_hash_set", ":flat_hash_set",
"//absl/strings", "//absl/strings",
"//absl/strings:cord",
"//absl/time", "//absl/time",
], ],
) )
@ -895,6 +901,7 @@ cc_binary(
"//absl/flags:flag", "//absl/flags:flag",
"//absl/hash", "//absl/hash",
"//absl/memory", "//absl/memory",
"//absl/strings:cord",
"//absl/strings:str_format", "//absl/strings:str_format",
"//absl/time", "//absl/time",
"@com_github_google_benchmark//:benchmark_main", "@com_github_google_benchmark//:benchmark_main",

View file

@ -40,6 +40,7 @@ absl_cc_library(
absl::compare absl::compare
absl::compressed_tuple absl::compressed_tuple
absl::container_memory absl::container_memory
absl::cord
absl::core_headers absl::core_headers
absl::layout absl::layout
absl::memory absl::memory
@ -60,6 +61,7 @@ absl_cc_library(
${ABSL_DEFAULT_LINKOPTS} ${ABSL_DEFAULT_LINKOPTS}
DEPS DEPS
absl::btree absl::btree
absl::cord
absl::flat_hash_set absl::flat_hash_set
absl::strings absl::strings
absl::time absl::time
@ -443,6 +445,7 @@ absl_cc_library(
${ABSL_DEFAULT_COPTS} ${ABSL_DEFAULT_COPTS}
DEPS DEPS
absl::config absl::config
absl::cord
absl::hash absl::hash
absl::strings absl::strings
PUBLIC PUBLIC
@ -456,8 +459,11 @@ absl_cc_test(
COPTS COPTS
${ABSL_TEST_COPTS} ${ABSL_TEST_COPTS}
DEPS DEPS
absl::cord
absl::cord_test_helpers
absl::hash_function_defaults absl::hash_function_defaults
absl::hash absl::hash
absl::random_random
absl::strings absl::strings
gmock_main gmock_main
) )

View file

@ -36,6 +36,7 @@
#include "absl/flags/flag.h" #include "absl/flags/flag.h"
#include "absl/hash/hash.h" #include "absl/hash/hash.h"
#include "absl/memory/memory.h" #include "absl/memory/memory.h"
#include "absl/strings/cord.h"
#include "absl/strings/str_format.h" #include "absl/strings/str_format.h"
#include "absl/time/time.h" #include "absl/time/time.h"
#include "benchmark/benchmark.h" #include "benchmark/benchmark.h"
@ -438,6 +439,7 @@ using StdString = std::string;
STL_ORDERED_TYPES(int32_t); STL_ORDERED_TYPES(int32_t);
STL_ORDERED_TYPES(int64_t); STL_ORDERED_TYPES(int64_t);
STL_ORDERED_TYPES(StdString); STL_ORDERED_TYPES(StdString);
STL_ORDERED_TYPES(Cord);
STL_ORDERED_TYPES(Time); STL_ORDERED_TYPES(Time);
#define STL_UNORDERED_TYPES(value) \ #define STL_UNORDERED_TYPES(value) \
@ -458,6 +460,8 @@ STL_ORDERED_TYPES(Time);
using stl_unordered_multimap_##value = \ using stl_unordered_multimap_##value = \
std::unordered_multimap<value, intptr_t, hash> std::unordered_multimap<value, intptr_t, hash>
STL_UNORDERED_TYPES_CUSTOM_HASH(Cord, absl::Hash<absl::Cord>);
STL_UNORDERED_TYPES(int32_t); STL_UNORDERED_TYPES(int32_t);
STL_UNORDERED_TYPES(int64_t); STL_UNORDERED_TYPES(int64_t);
STL_UNORDERED_TYPES(StdString); STL_UNORDERED_TYPES(StdString);
@ -478,6 +482,7 @@ STL_UNORDERED_TYPES_CUSTOM_HASH(Time, absl::Hash<absl::Time>);
BTREE_TYPES(int32_t); BTREE_TYPES(int32_t);
BTREE_TYPES(int64_t); BTREE_TYPES(int64_t);
BTREE_TYPES(StdString); BTREE_TYPES(StdString);
BTREE_TYPES(Cord);
BTREE_TYPES(Time); BTREE_TYPES(Time);
#define MY_BENCHMARK4(type, func) \ #define MY_BENCHMARK4(type, func) \
@ -526,6 +531,7 @@ BTREE_TYPES(Time);
MY_BENCHMARK(int32_t); MY_BENCHMARK(int32_t);
MY_BENCHMARK(int64_t); MY_BENCHMARK(int64_t);
MY_BENCHMARK(StdString); MY_BENCHMARK(StdString);
MY_BENCHMARK(Cord);
MY_BENCHMARK(Time); MY_BENCHMARK(Time);
// Define a type whose size and cost of moving are independently customizable. // Define a type whose size and cost of moving are independently customizable.

View file

@ -812,10 +812,12 @@ void MapTest() {
TEST(Btree, set_int32) { SetTest<int32_t>(); } TEST(Btree, set_int32) { SetTest<int32_t>(); }
TEST(Btree, set_int64) { SetTest<int64_t>(); } TEST(Btree, set_int64) { SetTest<int64_t>(); }
TEST(Btree, set_string) { SetTest<std::string>(); } TEST(Btree, set_string) { SetTest<std::string>(); }
TEST(Btree, set_cord) { SetTest<absl::Cord>(); }
TEST(Btree, set_pair) { SetTest<std::pair<int, int>>(); } TEST(Btree, set_pair) { SetTest<std::pair<int, int>>(); }
TEST(Btree, map_int32) { MapTest<int32_t>(); } TEST(Btree, map_int32) { MapTest<int32_t>(); }
TEST(Btree, map_int64) { MapTest<int64_t>(); } TEST(Btree, map_int64) { MapTest<int64_t>(); }
TEST(Btree, map_string) { MapTest<std::string>(); } TEST(Btree, map_string) { MapTest<std::string>(); }
TEST(Btree, map_cord) { MapTest<absl::Cord>(); }
TEST(Btree, map_pair) { MapTest<std::pair<int, int>>(); } TEST(Btree, map_pair) { MapTest<std::pair<int, int>>(); }
template <typename K, int N = 256> template <typename K, int N = 256>
@ -847,10 +849,12 @@ void MultiMapTest() {
TEST(Btree, multiset_int32) { MultiSetTest<int32_t>(); } TEST(Btree, multiset_int32) { MultiSetTest<int32_t>(); }
TEST(Btree, multiset_int64) { MultiSetTest<int64_t>(); } TEST(Btree, multiset_int64) { MultiSetTest<int64_t>(); }
TEST(Btree, multiset_string) { MultiSetTest<std::string>(); } TEST(Btree, multiset_string) { MultiSetTest<std::string>(); }
TEST(Btree, multiset_cord) { MultiSetTest<absl::Cord>(); }
TEST(Btree, multiset_pair) { MultiSetTest<std::pair<int, int>>(); } TEST(Btree, multiset_pair) { MultiSetTest<std::pair<int, int>>(); }
TEST(Btree, multimap_int32) { MultiMapTest<int32_t>(); } TEST(Btree, multimap_int32) { MultiMapTest<int32_t>(); }
TEST(Btree, multimap_int64) { MultiMapTest<int64_t>(); } TEST(Btree, multimap_int64) { MultiMapTest<int64_t>(); }
TEST(Btree, multimap_string) { MultiMapTest<std::string>(); } TEST(Btree, multimap_string) { MultiMapTest<std::string>(); }
TEST(Btree, multimap_cord) { MultiMapTest<absl::Cord>(); }
TEST(Btree, multimap_pair) { MultiMapTest<std::pair<int, int>>(); } TEST(Btree, multimap_pair) { MultiMapTest<std::pair<int, int>>(); }
struct CompareIntToString { struct CompareIntToString {
@ -1268,6 +1272,8 @@ TEST(Btree, KeyCompareToAdapter) {
AssertKeyCompareToAdapted<std::less<absl::string_view>, absl::string_view>(); AssertKeyCompareToAdapted<std::less<absl::string_view>, absl::string_view>();
AssertKeyCompareToAdapted<std::greater<absl::string_view>, AssertKeyCompareToAdapted<std::greater<absl::string_view>,
absl::string_view>(); absl::string_view>();
AssertKeyCompareToAdapted<std::less<absl::Cord>, absl::Cord>();
AssertKeyCompareToAdapted<std::greater<absl::Cord>, absl::Cord>();
AssertKeyCompareToNotAdapted<std::less<int>, int>(); AssertKeyCompareToNotAdapted<std::less<int>, int>();
AssertKeyCompareToNotAdapted<std::greater<int>, int>(); AssertKeyCompareToNotAdapted<std::greater<int>, int>();
} }

View file

@ -25,6 +25,7 @@
#include "absl/container/btree_map.h" #include "absl/container/btree_map.h"
#include "absl/container/btree_set.h" #include "absl/container/btree_set.h"
#include "absl/container/flat_hash_set.h" #include "absl/container/flat_hash_set.h"
#include "absl/strings/cord.h"
#include "absl/time/time.h" #include "absl/time/time.h"
namespace absl { namespace absl {
@ -100,6 +101,16 @@ struct Generator<std::string> {
} }
}; };
template <>
struct Generator<Cord> {
int maxval;
explicit Generator(int m) : maxval(m) {}
Cord operator()(int i) const {
char buf[16];
return Cord(GenerateDigits(buf, i, maxval));
}
};
template <typename T, typename U> template <typename T, typename U>
struct Generator<std::pair<T, U> > { struct Generator<std::pair<T, U> > {
Generator<typename remove_pair_const<T>::type> tgen; Generator<typename remove_pair_const<T>::type> tgen;

View file

@ -65,6 +65,7 @@
#include "absl/container/internal/layout.h" #include "absl/container/internal/layout.h"
#include "absl/memory/memory.h" #include "absl/memory/memory.h"
#include "absl/meta/type_traits.h" #include "absl/meta/type_traits.h"
#include "absl/strings/cord.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
#include "absl/types/compare.h" #include "absl/types/compare.h"
#include "absl/utility/utility.h" #include "absl/utility/utility.h"
@ -93,6 +94,19 @@ struct StringBtreeDefaultLess {
absl::string_view rhs) const { absl::string_view rhs) const {
return compare_internal::compare_result_as_ordering(lhs.compare(rhs)); return compare_internal::compare_result_as_ordering(lhs.compare(rhs));
} }
StringBtreeDefaultLess(std::less<absl::Cord>) {} // NOLINT
absl::weak_ordering operator()(const absl::Cord &lhs,
const absl::Cord &rhs) const {
return compare_internal::compare_result_as_ordering(lhs.Compare(rhs));
}
absl::weak_ordering operator()(const absl::Cord &lhs,
absl::string_view rhs) const {
return compare_internal::compare_result_as_ordering(lhs.Compare(rhs));
}
absl::weak_ordering operator()(absl::string_view lhs,
const absl::Cord &rhs) const {
return compare_internal::compare_result_as_ordering(-rhs.Compare(lhs));
}
}; };
struct StringBtreeDefaultGreater { struct StringBtreeDefaultGreater {
@ -107,13 +121,27 @@ struct StringBtreeDefaultGreater {
absl::string_view rhs) const { absl::string_view rhs) const {
return compare_internal::compare_result_as_ordering(rhs.compare(lhs)); return compare_internal::compare_result_as_ordering(rhs.compare(lhs));
} }
StringBtreeDefaultGreater(std::greater<absl::Cord>) {} // NOLINT
absl::weak_ordering operator()(const absl::Cord &lhs,
const absl::Cord &rhs) const {
return compare_internal::compare_result_as_ordering(rhs.Compare(lhs));
}
absl::weak_ordering operator()(const absl::Cord &lhs,
absl::string_view rhs) const {
return compare_internal::compare_result_as_ordering(-lhs.Compare(rhs));
}
absl::weak_ordering operator()(absl::string_view lhs,
const absl::Cord &rhs) const {
return compare_internal::compare_result_as_ordering(rhs.Compare(lhs));
}
}; };
// A helper class to convert a boolean comparison into a three-way "compare-to" // A helper class to convert a boolean comparison into a three-way "compare-to"
// comparison that returns a negative value to indicate less-than, zero to // comparison that returns a negative value to indicate less-than, zero to
// indicate equality and a positive value to indicate greater-than. This helper // indicate equality and a positive value to indicate greater-than. This helper
// class is specialized for less<std::string>, greater<std::string>, // class is specialized for less<std::string>, greater<std::string>,
// less<string_view>, and greater<string_view>. // less<string_view>, greater<string_view>, less<absl::Cord>, and
// greater<absl::Cord>.
// //
// key_compare_to_adapter is provided so that btree users // key_compare_to_adapter is provided so that btree users
// automatically get the more efficient compare-to code when using common // automatically get the more efficient compare-to code when using common
@ -145,6 +173,16 @@ struct key_compare_to_adapter<std::greater<absl::string_view>> {
using type = StringBtreeDefaultGreater; using type = StringBtreeDefaultGreater;
}; };
template <>
struct key_compare_to_adapter<std::less<absl::Cord>> {
using type = StringBtreeDefaultLess;
};
template <>
struct key_compare_to_adapter<std::greater<absl::Cord>> {
using type = StringBtreeDefaultGreater;
};
template <typename Key, typename Compare, typename Alloc, int TargetNodeSize, template <typename Key, typename Compare, typename Alloc, int TargetNodeSize,
bool Multi, typename SlotPolicy> bool Multi, typename SlotPolicy>
struct common_params { struct common_params {

View file

@ -53,6 +53,7 @@
#include "absl/base/config.h" #include "absl/base/config.h"
#include "absl/hash/hash.h" #include "absl/hash/hash.h"
#include "absl/strings/cord.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
namespace absl { namespace absl {
@ -72,6 +73,9 @@ struct StringHash {
size_t operator()(absl::string_view v) const { size_t operator()(absl::string_view v) const {
return absl::Hash<absl::string_view>{}(v); return absl::Hash<absl::string_view>{}(v);
} }
size_t operator()(const absl::Cord& v) const {
return absl::Hash<absl::Cord>{}(v);
}
}; };
// Supports heterogeneous lookup for string-like elements. // Supports heterogeneous lookup for string-like elements.
@ -82,6 +86,15 @@ struct StringHashEq {
bool operator()(absl::string_view lhs, absl::string_view rhs) const { bool operator()(absl::string_view lhs, absl::string_view rhs) const {
return lhs == rhs; return lhs == rhs;
} }
bool operator()(const absl::Cord& lhs, const absl::Cord& rhs) const {
return lhs == rhs;
}
bool operator()(const absl::Cord& lhs, absl::string_view rhs) const {
return lhs == rhs;
}
bool operator()(absl::string_view lhs, const absl::Cord& rhs) const {
return lhs == rhs;
}
}; };
}; };
@ -89,6 +102,8 @@ template <>
struct HashEq<std::string> : StringHashEq {}; struct HashEq<std::string> : StringHashEq {};
template <> template <>
struct HashEq<absl::string_view> : StringHashEq {}; struct HashEq<absl::string_view> : StringHashEq {};
template <>
struct HashEq<absl::Cord> : StringHashEq {};
// Supports heterogeneous lookup for pointers and smart pointers. // Supports heterogeneous lookup for pointers and smart pointers.
template <class T> template <class T>

View file

@ -19,6 +19,9 @@
#include <utility> #include <utility>
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "absl/random/random.h"
#include "absl/strings/cord.h"
#include "absl/strings/cord_test_helpers.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
namespace absl { namespace absl {
@ -203,10 +206,91 @@ TYPED_TEST(HashPointer, Works) {
EXPECT_NE(hash(&dummy), hash(cuptr)); EXPECT_NE(hash(&dummy), hash(cuptr));
} }
TEST(EqCord, Works) {
hash_default_eq<absl::Cord> eq;
const absl::string_view a_string_view = "a";
const absl::Cord a_cord(a_string_view);
const absl::string_view b_string_view = "b";
const absl::Cord b_cord(b_string_view);
EXPECT_TRUE(eq(a_cord, a_cord));
EXPECT_TRUE(eq(a_cord, a_string_view));
EXPECT_TRUE(eq(a_string_view, a_cord));
EXPECT_FALSE(eq(a_cord, b_cord));
EXPECT_FALSE(eq(a_cord, b_string_view));
EXPECT_FALSE(eq(b_string_view, a_cord));
}
TEST(HashCord, Works) {
hash_default_hash<absl::Cord> hash;
const absl::string_view a_string_view = "a";
const absl::Cord a_cord(a_string_view);
const absl::string_view b_string_view = "b";
const absl::Cord b_cord(b_string_view);
EXPECT_EQ(hash(a_cord), hash(a_cord));
EXPECT_EQ(hash(b_cord), hash(b_cord));
EXPECT_EQ(hash(a_string_view), hash(a_cord));
EXPECT_EQ(hash(b_string_view), hash(b_cord));
EXPECT_EQ(hash(absl::Cord("")), hash(""));
EXPECT_EQ(hash(absl::Cord()), hash(absl::string_view()));
EXPECT_NE(hash(a_cord), hash(b_cord));
EXPECT_NE(hash(a_cord), hash(b_string_view));
EXPECT_NE(hash(a_string_view), hash(b_cord));
EXPECT_NE(hash(a_string_view), hash(b_string_view));
}
void NoOpReleaser(absl::string_view data, void* arg) {}
TEST(HashCord, FragmentedCordWorks) {
hash_default_hash<absl::Cord> hash;
absl::Cord c = absl::MakeFragmentedCord({"a", "b", "c"});
EXPECT_FALSE(c.TryFlat().has_value());
EXPECT_EQ(hash(c), hash("abc"));
}
TEST(HashCord, FragmentedLongCordWorks) {
hash_default_hash<absl::Cord> hash;
// Crete some large strings which do not fit on the stack.
std::string a(65536, 'a');
std::string b(65536, 'b');
absl::Cord c = absl::MakeFragmentedCord({a, b});
EXPECT_FALSE(c.TryFlat().has_value());
EXPECT_EQ(hash(c), hash(a + b));
}
TEST(HashCord, RandomCord) {
hash_default_hash<absl::Cord> hash;
auto bitgen = absl::BitGen();
for (int i = 0; i < 1000; ++i) {
const int number_of_segments = absl::Uniform(bitgen, 0, 10);
std::vector<std::string> pieces;
for (size_t s = 0; s < number_of_segments; ++s) {
std::string str;
str.resize(absl::Uniform(bitgen, 0, 4096));
// MSVC needed the explicit return type in the lambda.
std::generate(str.begin(), str.end(), [&]() -> char {
return static_cast<char>(absl::Uniform<unsigned char>(bitgen));
});
pieces.push_back(str);
}
absl::Cord c = absl::MakeFragmentedCord(pieces);
EXPECT_EQ(hash(c), hash(std::string(c)));
}
}
// Cartesian product of (std::string, absl::string_view) // Cartesian product of (std::string, absl::string_view)
// with (std::string, absl::string_view, const char*). // with (std::string, absl::string_view, const char*, absl::Cord).
using StringTypesCartesianProduct = Types< using StringTypesCartesianProduct = Types<
// clang-format off // clang-format off
std::pair<absl::Cord, std::string>,
std::pair<absl::Cord, absl::string_view>,
std::pair<absl::Cord, absl::Cord>,
std::pair<absl::Cord, const char*>,
std::pair<std::string, absl::Cord>,
std::pair<absl::string_view, absl::Cord>,
std::pair<absl::string_view, std::string>, std::pair<absl::string_view, std::string>,
std::pair<absl::string_view, absl::string_view>, std::pair<absl::string_view, absl::string_view>,

View file

@ -226,7 +226,7 @@ void RecordInsertSlow(HashtablezInfo* info, size_t hash,
// SwissTables probe in groups of 16, so scale this to count items probes and // SwissTables probe in groups of 16, so scale this to count items probes and
// not offset from desired. // not offset from desired.
size_t probe_length = distance_from_desired; size_t probe_length = distance_from_desired;
#if SWISSTABLE_HAVE_SSE2 #if ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSE2
probe_length /= 16; probe_length /= 16;
#else #else
probe_length /= 8; probe_length /= 8;

View file

@ -98,7 +98,7 @@ struct HashtablezInfo {
}; };
inline void RecordRehashSlow(HashtablezInfo* info, size_t total_probe_length) { inline void RecordRehashSlow(HashtablezInfo* info, size_t total_probe_length) {
#if SWISSTABLE_HAVE_SSE2 #if ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSE2
total_probe_length /= 16; total_probe_length /= 16;
#else #else
total_probe_length /= 8; total_probe_length /= 8;

View file

@ -29,7 +29,7 @@
#include "absl/time/clock.h" #include "absl/time/clock.h"
#include "absl/time/time.h" #include "absl/time/time.h"
#if SWISSTABLE_HAVE_SSE2 #if ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSE2
constexpr int kProbeLength = 16; constexpr int kProbeLength = 16;
#else #else
constexpr int kProbeLength = 8; constexpr int kProbeLength = 8;

View file

@ -16,33 +16,34 @@
#ifndef ABSL_CONTAINER_INTERNAL_HAVE_SSE_H_ #ifndef ABSL_CONTAINER_INTERNAL_HAVE_SSE_H_
#define ABSL_CONTAINER_INTERNAL_HAVE_SSE_H_ #define ABSL_CONTAINER_INTERNAL_HAVE_SSE_H_
#ifndef SWISSTABLE_HAVE_SSE2 #ifndef ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSE2
#if defined(__SSE2__) || \ #if defined(__SSE2__) || \
(defined(_MSC_VER) && \ (defined(_MSC_VER) && \
(defined(_M_X64) || (defined(_M_IX86) && _M_IX86_FP >= 2))) (defined(_M_X64) || (defined(_M_IX86) && _M_IX86_FP >= 2)))
#define SWISSTABLE_HAVE_SSE2 1 #define ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSE2 1
#else #else
#define SWISSTABLE_HAVE_SSE2 0 #define ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSE2 0
#endif #endif
#endif #endif
#ifndef SWISSTABLE_HAVE_SSSE3 #ifndef ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSSE3
#ifdef __SSSE3__ #ifdef __SSSE3__
#define SWISSTABLE_HAVE_SSSE3 1 #define ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSSE3 1
#else #else
#define SWISSTABLE_HAVE_SSSE3 0 #define ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSSE3 0
#endif #endif
#endif #endif
#if SWISSTABLE_HAVE_SSSE3 && !SWISSTABLE_HAVE_SSE2 #if ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSSE3 && \
!ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSE2
#error "Bad configuration!" #error "Bad configuration!"
#endif #endif
#if SWISSTABLE_HAVE_SSE2 #if ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSE2
#include <emmintrin.h> #include <emmintrin.h>
#endif #endif
#if SWISSTABLE_HAVE_SSSE3 #if ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSSE3
#include <tmmintrin.h> #include <tmmintrin.h>
#endif #endif

View file

@ -312,7 +312,7 @@ inline bool IsFull(ctrl_t c) { return c >= 0; }
inline bool IsDeleted(ctrl_t c) { return c == kDeleted; } inline bool IsDeleted(ctrl_t c) { return c == kDeleted; }
inline bool IsEmptyOrDeleted(ctrl_t c) { return c < kSentinel; } inline bool IsEmptyOrDeleted(ctrl_t c) { return c < kSentinel; }
#if SWISSTABLE_HAVE_SSE2 #if ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSE2
// https://github.com/abseil/abseil-cpp/issues/209 // https://github.com/abseil/abseil-cpp/issues/209
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87853 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87853
@ -346,7 +346,7 @@ struct GroupSse2Impl {
// Returns a bitmask representing the positions of empty slots. // Returns a bitmask representing the positions of empty slots.
BitMask<uint32_t, kWidth> MatchEmpty() const { BitMask<uint32_t, kWidth> MatchEmpty() const {
#if SWISSTABLE_HAVE_SSSE3 #if ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSSE3
// This only works because kEmpty is -128. // This only works because kEmpty is -128.
return BitMask<uint32_t, kWidth>( return BitMask<uint32_t, kWidth>(
_mm_movemask_epi8(_mm_sign_epi8(ctrl, ctrl))); _mm_movemask_epi8(_mm_sign_epi8(ctrl, ctrl)));
@ -372,7 +372,7 @@ struct GroupSse2Impl {
void ConvertSpecialToEmptyAndFullToDeleted(ctrl_t* dst) const { void ConvertSpecialToEmptyAndFullToDeleted(ctrl_t* dst) const {
auto msbs = _mm_set1_epi8(static_cast<char>(-128)); auto msbs = _mm_set1_epi8(static_cast<char>(-128));
auto x126 = _mm_set1_epi8(126); auto x126 = _mm_set1_epi8(126);
#if SWISSTABLE_HAVE_SSSE3 #if ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSSE3
auto res = _mm_or_si128(_mm_shuffle_epi8(x126, ctrl), msbs); auto res = _mm_or_si128(_mm_shuffle_epi8(x126, ctrl), msbs);
#else #else
auto zero = _mm_setzero_si128(); auto zero = _mm_setzero_si128();
@ -384,7 +384,7 @@ struct GroupSse2Impl {
__m128i ctrl; __m128i ctrl;
}; };
#endif // SWISSTABLE_HAVE_SSE2 #endif // ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSE2
struct GroupPortableImpl { struct GroupPortableImpl {
static constexpr size_t kWidth = 8; static constexpr size_t kWidth = 8;
@ -438,7 +438,7 @@ struct GroupPortableImpl {
uint64_t ctrl; uint64_t ctrl;
}; };
#if SWISSTABLE_HAVE_SSE2 #if ABSL_INTERNAL_RAW_HASH_SET_HAVE_SSE2
using Group = GroupSse2Impl; using Group = GroupSse2Impl;
#else #else
using Group = GroupPortableImpl; using Group = GroupPortableImpl;

View file

@ -43,6 +43,7 @@ cc_library(
"//absl/meta:type_traits", "//absl/meta:type_traits",
"//absl/numeric:int128", "//absl/numeric:int128",
"//absl/strings", "//absl/strings",
"//absl/strings:cord",
"//absl/types:optional", "//absl/types:optional",
"//absl/types:variant", "//absl/types:variant",
"//absl/utility", "//absl/utility",
@ -76,6 +77,7 @@ cc_test(
"//absl/container:flat_hash_set", "//absl/container:flat_hash_set",
"//absl/meta:type_traits", "//absl/meta:type_traits",
"//absl/numeric:int128", "//absl/numeric:int128",
"//absl/strings:cord_test_helpers",
"@com_google_googletest//:gtest_main", "@com_google_googletest//:gtest_main",
], ],
) )

View file

@ -25,6 +25,7 @@ absl_cc_library(
COPTS COPTS
${ABSL_DEFAULT_COPTS} ${ABSL_DEFAULT_COPTS}
DEPS DEPS
absl::cord
absl::core_headers absl::core_headers
absl::endian absl::endian
absl::fixed_array absl::fixed_array
@ -62,6 +63,7 @@ absl_cc_test(
COPTS COPTS
${ABSL_TEST_COPTS} ${ABSL_TEST_COPTS}
DEPS DEPS
absl::cord_test_helpers
absl::hash absl::hash
absl::hash_testing absl::hash_testing
absl::core_headers absl::core_headers

View file

@ -98,6 +98,7 @@ ABSL_NAMESPACE_BEGIN
// * std::tuple<Ts...>, if all the Ts... are hashable // * std::tuple<Ts...>, if all the Ts... are hashable
// * std::unique_ptr and std::shared_ptr // * std::unique_ptr and std::shared_ptr
// * All string-like types including: // * All string-like types including:
// * absl::Cord
// * std::string // * std::string
// * std::string_view (as well as any instance of std::basic_string that // * std::string_view (as well as any instance of std::basic_string that
// uses char and std::char_traits) // uses char and std::char_traits)

View file

@ -42,6 +42,7 @@
#include "absl/hash/internal/spy_hash_state.h" #include "absl/hash/internal/spy_hash_state.h"
#include "absl/meta/type_traits.h" #include "absl/meta/type_traits.h"
#include "absl/numeric/int128.h" #include "absl/numeric/int128.h"
#include "absl/strings/cord_test_helpers.h"
namespace { namespace {
@ -269,6 +270,22 @@ struct WrapInTuple {
} }
}; };
absl::Cord FlatCord(absl::string_view sv) {
absl::Cord c(sv);
c.Flatten();
return c;
}
absl::Cord FragmentedCord(absl::string_view sv) {
if (sv.size() < 2) {
return absl::Cord(sv);
}
size_t halfway = sv.size() / 2;
std::vector<absl::string_view> parts = {sv.substr(0, halfway),
sv.substr(halfway)};
return absl::MakeFragmentedCord(parts);
}
TEST(HashValueTest, Strings) { TEST(HashValueTest, Strings) {
EXPECT_TRUE((is_hashable<std::string>::value)); EXPECT_TRUE((is_hashable<std::string>::value));
@ -277,23 +294,27 @@ TEST(HashValueTest, Strings) {
const std::string large = std::string(2048, 'x'); // multiple of chunk size const std::string large = std::string(2048, 'x'); // multiple of chunk size
const std::string huge = std::string(5000, 'a'); // not a multiple const std::string huge = std::string(5000, 'a'); // not a multiple
EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple( EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple( //
std::string(), absl::string_view(), std::string(), absl::string_view(), absl::Cord(), //
std::string(""), absl::string_view(""), std::string(""), absl::string_view(""), absl::Cord(""), //
std::string(small), absl::string_view(small), std::string(small), absl::string_view(small), absl::Cord(small), //
std::string(dup), absl::string_view(dup), std::string(dup), absl::string_view(dup), absl::Cord(dup), //
std::string(large), absl::string_view(large), std::string(large), absl::string_view(large), absl::Cord(large), //
std::string(huge), absl::string_view(huge)))); std::string(huge), absl::string_view(huge), FlatCord(huge), //
FragmentedCord(huge))));
// Also check that nested types maintain the same hash. // Also check that nested types maintain the same hash.
const WrapInTuple t{}; const WrapInTuple t{};
EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple( EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple( //
t(std::string()), t(absl::string_view()), t(std::string()), t(absl::string_view()), t(absl::Cord()), //
t(std::string("")), t(absl::string_view("")), t(std::string("")), t(absl::string_view("")), t(absl::Cord("")), //
t(std::string(small)), t(absl::string_view(small)), t(std::string(small)), t(absl::string_view(small)), //
t(std::string(dup)), t(absl::string_view(dup)), t(absl::Cord(small)), //
t(std::string(large)), t(absl::string_view(large)), t(std::string(dup)), t(absl::string_view(dup)), t(absl::Cord(dup)), //
t(std::string(huge)), t(absl::string_view(huge))))); t(std::string(large)), t(absl::string_view(large)), //
t(absl::Cord(large)), //
t(std::string(huge)), t(absl::string_view(huge)), //
t(FlatCord(huge)), t(FragmentedCord(huge)))));
// Make sure that hashing a `const char*` does not use its std::string-value. // Make sure that hashing a `const char*` does not use its std::string-value.
EXPECT_NE(SpyHash(static_cast<const char*>("ABC")), EXPECT_NE(SpyHash(static_cast<const char*>("ABC")),

View file

@ -43,6 +43,7 @@
#include "absl/container/fixed_array.h" #include "absl/container/fixed_array.h"
#include "absl/meta/type_traits.h" #include "absl/meta/type_traits.h"
#include "absl/numeric/int128.h" #include "absl/numeric/int128.h"
#include "absl/strings/cord.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
#include "absl/types/optional.h" #include "absl/types/optional.h"
#include "absl/types/variant.h" #include "absl/types/variant.h"
@ -413,6 +414,7 @@ H AbslHashValue(H hash_state, const std::shared_ptr<T>& ptr) {
// All the string-like types supported here provide the same hash expansion for // All the string-like types supported here provide the same hash expansion for
// the same character sequence. These types are: // the same character sequence. These types are:
// //
// - `absl::Cord`
// - `std::string` (and std::basic_string<char, std::char_traits<char>, A> for // - `std::string` (and std::basic_string<char, std::char_traits<char>, A> for
// any allocator A) // any allocator A)
// - `absl::string_view` and `std::string_view` // - `absl::string_view` and `std::string_view`
@ -441,6 +443,25 @@ H AbslHashValue(
str.size()); str.size());
} }
template <typename H>
H HashFragmentedCord(H hash_state, const absl::Cord& c) {
PiecewiseCombiner combiner;
c.ForEachChunk([&combiner, &hash_state](absl::string_view chunk) {
hash_state =
combiner.add_buffer(std::move(hash_state), chunk.data(), chunk.size());
});
return H::combine(combiner.finalize(std::move(hash_state)), c.size());
}
template <typename H>
H AbslHashValue(H hash_state, const absl::Cord& c) {
absl::optional<absl::string_view> maybe_flat = c.TryFlat();
if (maybe_flat.has_value()) {
return H::combine(std::move(hash_state), *maybe_flat);
}
return hash_internal::HashFragmentedCord(std::move(hash_state), c);
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// AbslHashValue for Sequence Containers // AbslHashValue for Sequence Containers
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------

View file

@ -792,28 +792,21 @@ inline bool operator!=(uint128 lhs, uint128 rhs) {
} }
inline bool operator<(uint128 lhs, uint128 rhs) { inline bool operator<(uint128 lhs, uint128 rhs) {
#ifdef ABSL_HAVE_INTRINSIC_INT128
return static_cast<unsigned __int128>(lhs) <
static_cast<unsigned __int128>(rhs);
#else
return (Uint128High64(lhs) == Uint128High64(rhs)) return (Uint128High64(lhs) == Uint128High64(rhs))
? (Uint128Low64(lhs) < Uint128Low64(rhs)) ? (Uint128Low64(lhs) < Uint128Low64(rhs))
: (Uint128High64(lhs) < Uint128High64(rhs)); : (Uint128High64(lhs) < Uint128High64(rhs));
#endif
} }
inline bool operator>(uint128 lhs, uint128 rhs) { inline bool operator>(uint128 lhs, uint128 rhs) { return rhs < lhs; }
return (Uint128High64(lhs) == Uint128High64(rhs))
? (Uint128Low64(lhs) > Uint128Low64(rhs))
: (Uint128High64(lhs) > Uint128High64(rhs));
}
inline bool operator<=(uint128 lhs, uint128 rhs) { inline bool operator<=(uint128 lhs, uint128 rhs) { return !(rhs < lhs); }
return (Uint128High64(lhs) == Uint128High64(rhs))
? (Uint128Low64(lhs) <= Uint128Low64(rhs))
: (Uint128High64(lhs) <= Uint128High64(rhs));
}
inline bool operator>=(uint128 lhs, uint128 rhs) { inline bool operator>=(uint128 lhs, uint128 rhs) { return !(lhs < rhs); }
return (Uint128High64(lhs) == Uint128High64(rhs))
? (Uint128Low64(lhs) >= Uint128Low64(rhs))
: (Uint128High64(lhs) >= Uint128High64(rhs));
}
// Unary operators. // Unary operators.
@ -870,6 +863,9 @@ inline uint128& uint128::operator^=(uint128 other) {
// Arithmetic operators. // Arithmetic operators.
inline uint128 operator<<(uint128 lhs, int amount) { inline uint128 operator<<(uint128 lhs, int amount) {
#ifdef ABSL_HAVE_INTRINSIC_INT128
return static_cast<unsigned __int128>(lhs) << amount;
#else
// uint64_t shifts of >= 64 are undefined, so we will need some // uint64_t shifts of >= 64 are undefined, so we will need some
// special-casing. // special-casing.
if (amount < 64) { if (amount < 64) {
@ -881,9 +877,13 @@ inline uint128 operator<<(uint128 lhs, int amount) {
return lhs; return lhs;
} }
return MakeUint128(Uint128Low64(lhs) << (amount - 64), 0); return MakeUint128(Uint128Low64(lhs) << (amount - 64), 0);
#endif
} }
inline uint128 operator>>(uint128 lhs, int amount) { inline uint128 operator>>(uint128 lhs, int amount) {
#ifdef ABSL_HAVE_INTRINSIC_INT128
return static_cast<unsigned __int128>(lhs) >> amount;
#else
// uint64_t shifts of >= 64 are undefined, so we will need some // uint64_t shifts of >= 64 are undefined, so we will need some
// special-casing. // special-casing.
if (amount < 64) { if (amount < 64) {
@ -895,6 +895,7 @@ inline uint128 operator>>(uint128 lhs, int amount) {
return lhs; return lhs;
} }
return MakeUint128(0, Uint128High64(lhs) >> (amount - 64)); return MakeUint128(0, Uint128High64(lhs) >> (amount - 64));
#endif
} }
inline uint128 operator+(uint128 lhs, uint128 rhs) { inline uint128 operator+(uint128 lhs, uint128 rhs) {

View file

@ -548,6 +548,7 @@ absl_cc_library(
absl::inlined_vector absl::inlined_vector
absl::optional absl::optional
absl::raw_logging_internal absl::raw_logging_internal
absl::strings
absl::type_traits absl::type_traits
PUBLIC PUBLIC
) )