Commit 7853a758 by Abseil Team Committed by vslashg

Export of internal Abseil changes

--
91ca367a7548270155721bdda74611aeea2a2153 by Abseil Team <absl-team@google.com>:

Replace the only usage of btree_node::swap with simpler logic using transfers and delete btree_node::swap.

Add a benchmark for constructing small containers.

PiperOrigin-RevId: 301169874

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

Ensure ABSL_CXX_STANDARD is set.
Fixes #640

PiperOrigin-RevId: 301160106

--
14ca0beee8c109e532134e7e9da7b072da1bf911 by Abseil Team <absl-team@google.com>:

Rollback the change to make Cord iterators a fixed size.  That change increased the iterator size, which can cause a deep recursion call to hit the stack memory limit, in turn causing a signal 11 failure.

PiperOrigin-RevId: 301084915

--
619e3cd9e56408bdb8b3b5a1e08dda1e95242264 by Matthew Brown <matthewbr@google.com>:

Internal Change

PiperOrigin-RevId: 300832828

--
64f8d62ab4c4c78077dbe85a9595a8eeb6d16608 by Gennadiy Rozental <rogeeff@google.com>:

Fix for empty braces support.

We will call proper aggregate construction in case when {} is used as default value. In other words instead of "new T", we'll call "new T{}".

PiperOrigin-RevId: 300715686

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

Emscripten supports thread-local storage nowadays.

PiperOrigin-RevId: 300675185
GitOrigin-RevId: 91ca367a7548270155721bdda74611aeea2a2153
Change-Id: I3344f745f9c3fc78775532b1808442fabd98e34a
parent c6954897
......@@ -262,13 +262,6 @@ static_assert(ABSL_INTERNAL_INLINE_NAMESPACE_STR[0] != 'h' ||
#endif
#endif // defined(__ANDROID__) && defined(__clang__)
// Emscripten doesn't yet support `thread_local` or `__thread`.
// https://github.com/emscripten-core/emscripten/issues/3502
#if defined(__EMSCRIPTEN__)
#undef ABSL_HAVE_TLS
#undef ABSL_HAVE_THREAD_LOCAL
#endif // defined(__EMSCRIPTEN__)
// ABSL_HAVE_INTRINSIC_INT128
//
// Checks whether the __int128 compiler extension for a 128-bit integral type is
......
......@@ -134,6 +134,27 @@ void BM_InsertEnd(benchmark::State& state) {
}
}
// Benchmark inserting the first few elements in a container. In b-tree, this is
// when the root node grows.
template <typename T>
void BM_InsertSmall(benchmark::State& state) {
using V = typename remove_pair_const<typename T::value_type>::type;
const int kSize = 8;
std::vector<V> values = GenerateValues<V>(kSize);
T container;
while (state.KeepRunningBatch(kSize)) {
for (int i = 0; i < kSize; ++i) {
benchmark::DoNotOptimize(container.insert(values[i]));
}
state.PauseTiming();
// Do not measure the time it takes to clear the container.
container.clear();
state.ResumeTiming();
}
}
template <typename T>
void BM_LookupImpl(benchmark::State& state, bool sorted) {
using V = typename remove_pair_const<typename T::value_type>::type;
......@@ -493,6 +514,7 @@ BTREE_TYPES(Time);
MY_BENCHMARK4(type, Insert); \
MY_BENCHMARK4(type, InsertSorted); \
MY_BENCHMARK4(type, InsertEnd); \
MY_BENCHMARK4(type, InsertSmall); \
MY_BENCHMARK4(type, Lookup); \
MY_BENCHMARK4(type, FullLookup); \
MY_BENCHMARK4(type, Delete); \
......
......@@ -776,9 +776,6 @@ class btree_node {
// delimiting key in the parent node onto itself.
void merge(btree_node *src, allocator_type *alloc);
// Swaps the contents of `this` and `other`.
void swap(btree_node *other, allocator_type *alloc);
// Node allocation/deletion routines.
void init_leaf(btree_node *parent, int max_count) {
set_parent(parent);
......@@ -820,6 +817,14 @@ class btree_node {
absl::container_internal::SanitizerPoisonObject(slot(i));
}
// Transfers value from slot `src_i` in `src` to slot `dest_i` in `this`.
void transfer(const size_type dest_i, const size_type src_i, btree_node *src,
allocator_type *alloc) {
absl::container_internal::SanitizerUnpoisonObject(slot(dest_i));
params_type::transfer(alloc, slot(dest_i), src->slot(src_i));
absl::container_internal::SanitizerPoisonObject(src->slot(src_i));
}
// Move n values starting at value i in this node into the values starting at
// value j in dest_node.
void uninitialized_move_n(const size_type n, const size_type i,
......@@ -1752,54 +1757,6 @@ void btree_node<P>::merge(btree_node *src, allocator_type *alloc) {
parent()->remove_value(position(), alloc);
}
template <typename P>
void btree_node<P>::swap(btree_node *other, allocator_type *alloc) {
using std::swap;
assert(leaf() == other->leaf());
// Determine which is the smaller/larger node.
btree_node *smaller = this, *larger = other;
if (smaller->count() > larger->count()) {
swap(smaller, larger);
}
// Swap the values.
for (slot_type *a = smaller->start_slot(), *b = larger->start_slot(),
*end = smaller->finish_slot();
a != end; ++a, ++b) {
params_type::swap(alloc, a, b);
}
// Move values that can't be swapped.
const size_type to_move = larger->count() - smaller->count();
larger->uninitialized_move_n(to_move, smaller->finish(), smaller->finish(),
smaller, alloc);
larger->value_destroy_n(smaller->finish(), to_move, alloc);
if (!leaf()) {
// Swap the child pointers.
std::swap_ranges(&smaller->mutable_child(smaller->start()),
&smaller->mutable_child(smaller->finish() + 1),
&larger->mutable_child(larger->start()));
// Update swapped children's parent pointers.
int i = smaller->start();
int j = larger->start();
for (; i <= smaller->finish(); ++i, ++j) {
smaller->child(i)->set_parent(smaller);
larger->child(j)->set_parent(larger);
}
// Move the child pointers that couldn't be swapped.
for (; j <= larger->finish(); ++i, ++j) {
smaller->init_child(i, larger->child(j));
larger->clear_child(j);
}
}
// Swap the `finish`s.
// TODO(ezb): with floating storage, will also need to swap starts.
swap(mutable_finish(), other->mutable_finish());
}
////
// btree_iterator methods
template <typename N, typename R, typename P>
......@@ -2492,6 +2449,7 @@ inline auto btree<P>::internal_emplace(iterator iter, Args &&... args)
++iter.position;
}
const int max_count = iter.node->max_count();
allocator_type *alloc = mutable_allocator();
if (iter.node->count() == max_count) {
// Make room in the leaf for the new item.
if (max_count < kNodeValues) {
......@@ -2500,15 +2458,21 @@ inline auto btree<P>::internal_emplace(iterator iter, Args &&... args)
assert(iter.node == root());
iter.node =
new_leaf_root_node((std::min<int>)(kNodeValues, 2 * max_count));
iter.node->swap(root(), mutable_allocator());
delete_leaf_node(root());
mutable_root() = rightmost_ = iter.node;
// Transfer the values from the old root to the new root.
node_type *old_root = root();
node_type *new_root = iter.node;
for (int i = old_root->start(), f = old_root->finish(); i < f; ++i) {
new_root->transfer(i, i, old_root, alloc);
}
new_root->set_finish(old_root->finish());
old_root->set_finish(old_root->start());
delete_leaf_node(old_root);
mutable_root() = rightmost_ = new_root;
} else {
rebalance_or_split(&iter);
}
}
iter.node->emplace_value(iter.position, mutable_allocator(),
std::forward<Args>(args)...);
iter.node->emplace_value(iter.position, alloc, std::forward<Args>(args)...);
++size_;
return iter;
}
......
......@@ -63,3 +63,5 @@ else()
set(ABSL_DEFAULT_COPTS "")
set(ABSL_TEST_COPTS "")
endif()
set(ABSL_CXX_STANDARD "${CMAKE_CXX_STANDARD}")
......@@ -293,6 +293,18 @@ TEST_F(FlagTest, TestFlagDefinition) {
// --------------------------------------------------------------------
TEST_F(FlagTest, TestDefault) {
EXPECT_EQ(FLAGS_test_flag_01.DefaultValue(), "true");
EXPECT_EQ(FLAGS_test_flag_02.DefaultValue(), "1234");
EXPECT_EQ(FLAGS_test_flag_03.DefaultValue(), "-34");
EXPECT_EQ(FLAGS_test_flag_04.DefaultValue(), "189");
EXPECT_EQ(FLAGS_test_flag_05.DefaultValue(), "10765");
EXPECT_EQ(FLAGS_test_flag_06.DefaultValue(), "40000");
EXPECT_EQ(FLAGS_test_flag_07.DefaultValue(), "-1234567");
EXPECT_EQ(FLAGS_test_flag_08.DefaultValue(), "9876543");
EXPECT_EQ(FLAGS_test_flag_09.DefaultValue(), "-9.876e-50");
EXPECT_EQ(FLAGS_test_flag_10.DefaultValue(), "1.234e+12");
EXPECT_EQ(FLAGS_test_flag_11.DefaultValue(), "");
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_01), true);
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_02), 1234);
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_03), -34);
......@@ -308,6 +320,61 @@ TEST_F(FlagTest, TestDefault) {
// --------------------------------------------------------------------
struct NonTriviallyCopyableAggregate {
NonTriviallyCopyableAggregate() = default;
NonTriviallyCopyableAggregate(const NonTriviallyCopyableAggregate& rhs)
: value(rhs.value) {}
NonTriviallyCopyableAggregate& operator=(
const NonTriviallyCopyableAggregate& rhs) {
value = rhs.value;
return *this;
}
int value;
};
bool AbslParseFlag(absl::string_view src, NonTriviallyCopyableAggregate* f,
std::string* e) {
return absl::ParseFlag(src, &f->value, e);
}
std::string AbslUnparseFlag(const NonTriviallyCopyableAggregate& ntc) {
return absl::StrCat(ntc.value);
}
bool operator==(const NonTriviallyCopyableAggregate& ntc1,
const NonTriviallyCopyableAggregate& ntc2) {
return ntc1.value == ntc2.value;
}
} // namespace
ABSL_FLAG(bool, test_flag_eb_01, {}, "");
ABSL_FLAG(int32_t, test_flag_eb_02, {}, "");
ABSL_FLAG(int64_t, test_flag_eb_03, {}, "");
ABSL_FLAG(double, test_flag_eb_04, {}, "");
ABSL_FLAG(std::string, test_flag_eb_05, {}, "");
ABSL_FLAG(NonTriviallyCopyableAggregate, test_flag_eb_06, {}, "");
namespace {
TEST_F(FlagTest, TestEmptyBracesDefault) {
EXPECT_EQ(FLAGS_test_flag_eb_01.DefaultValue(), "false");
EXPECT_EQ(FLAGS_test_flag_eb_02.DefaultValue(), "0");
EXPECT_EQ(FLAGS_test_flag_eb_03.DefaultValue(), "0");
EXPECT_EQ(FLAGS_test_flag_eb_04.DefaultValue(), "0");
EXPECT_EQ(FLAGS_test_flag_eb_05.DefaultValue(), "");
EXPECT_EQ(FLAGS_test_flag_eb_06.DefaultValue(), "0");
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_eb_01), false);
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_eb_02), 0);
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_eb_03), 0);
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_eb_04), 0.0);
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_eb_05), "");
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_eb_06),
NonTriviallyCopyableAggregate{});
}
// --------------------------------------------------------------------
TEST_F(FlagTest, TestGetSet) {
absl::SetFlag(&FLAGS_test_flag_01, false);
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_01), false);
......
......@@ -647,7 +647,7 @@ T* MakeFromDefaultValue(T t) {
template <typename T>
T* MakeFromDefaultValue(EmptyBraces) {
return new T;
return new T{};
}
} // namespace flags_internal
......
......@@ -48,6 +48,7 @@
#include "absl/base/internal/per_thread_tls.h"
#include "absl/base/macros.h"
#include "absl/base/port.h"
#include "absl/container/inlined_vector.h"
#include "absl/functional/function_ref.h"
#include "absl/meta/type_traits.h"
#include "absl/strings/internal/cord_internal.h"
......@@ -67,55 +68,6 @@ template <typename H>
H HashFragmentedCord(H, const Cord&);
}
namespace cord_internal {
// It's expensive to keep a tree perfectly balanced, so instead we keep trees
// approximately balanced. A tree node N of depth D(N) that contains a string
// of L(N) characters is considered balanced if L >= Fibonacci(D + 2).
// The "+ 2" is used to ensure that every balanced leaf node contains at least
// one character. Here we presume that
// Fibonacci(0) = 0
// Fibonacci(1) = 1
// Fibonacci(2) = 1
// Fibonacci(3) = 2
// ...
// The algorithm is based on paper by Hans Boehm et al:
// https://www.cs.rit.edu/usr/local/pub/jeh/courses/QUARTERS/FP/Labs/CedarRope/rope-paper.pdf
// In this paper authors shows that rebalancing based on cord forest of already
// balanced subtrees can be proven to never produce tree of depth larger than
// largest Fibonacci number representable in the same integral type as cord size
// For 64 bit integers this is the 93rd Fibonacci number. For 32 bit integrals
// this is 47th Fibonacci number.
constexpr size_t MaxCordDepth() { return sizeof(size_t) == 8 ? 93 : 47; }
// This class models fixed max size stack of CordRep pointers.
// The elements are being pushed back and popped from the back.
template <typename CordRepPtr, size_t N>
class CordTreePath {
public:
CordTreePath() {}
explicit CordTreePath(CordRepPtr root) { push_back(root); }
bool empty() const { return size_ == 0; }
size_t size() const { return size_; }
void clear() { size_ = 0; }
CordRepPtr back() { return data_[size_ - 1]; }
void pop_back() {
--size_;
assert(size_ < N);
}
void push_back(CordRepPtr elem) { data_[size_++] = elem; }
private:
CordRepPtr data_[N];
size_t size_ = 0;
};
using CordTreeMutablePath = CordTreePath<CordRep*, MaxCordDepth()>;
} // namespace cord_internal
// A Cord is a sequence of characters.
class Cord {
private:
......@@ -333,7 +285,8 @@ class Cord {
absl::cord_internal::CordRep* current_leaf_ = nullptr;
// The number of bytes left in the `Cord` over which we are iterating.
size_t bytes_remaining_ = 0;
absl::cord_internal::CordTreeMutablePath stack_of_right_children_;
absl::InlinedVector<absl::cord_internal::CordRep*, 4>
stack_of_right_children_;
};
// Returns an iterator to the first chunk of the `Cord`.
......
......@@ -1402,53 +1402,6 @@ TEST(CordChunkIterator, Operations) {
VerifyChunkIterator(subcords, 128);
}
TEST(CordChunkIterator, MaxLengthFullTree) {
// Start with a 1-byte cord, and then double its length in a loop. We should
// be able to do this until the point where we would overflow size_t.
absl::Cord cord;
size_t size = 1;
AddExternalMemory("x", &cord);
EXPECT_EQ(cord.size(), size);
const int kCordLengthDoublingLimit = std::numeric_limits<size_t>::digits - 1;
for (int i = 0; i < kCordLengthDoublingLimit; ++i) {
cord.Prepend(absl::Cord(cord));
size <<= 1;
EXPECT_EQ(cord.size(), size);
auto chunk_it = cord.chunk_begin();
EXPECT_EQ(*chunk_it, "x");
}
EXPECT_DEATH_IF_SUPPORTED(
(cord.Prepend(absl::Cord(cord)), *cord.chunk_begin()),
"Cord is too long");
}
TEST(CordChunkIterator, MaxDepth) {
// By reusing nodes, it's possible in pathological cases to build a Cord that
// exceeds both the maximum permissible length and depth. In this case, the
// violation of the maximum depth is reported.
absl::Cord left_child;
AddExternalMemory("x", &left_child);
absl::Cord root = left_child;
for (int i = 0; i < absl::cord_internal::MaxCordDepth() - 2; ++i) {
size_t new_size = left_child.size() + root.size();
root.Prepend(left_child);
EXPECT_EQ(root.size(), new_size);
auto chunk_it = root.chunk_begin();
EXPECT_EQ(*chunk_it, "x");
std::swap(left_child, root);
}
EXPECT_DEATH_IF_SUPPORTED(root.Prepend(left_child), "Cord is too long");
}
TEST(CordCharIterator, Traits) {
static_assert(std::is_copy_constructible<absl::Cord::CharIterator>::value,
"");
......
......@@ -24,6 +24,7 @@
#include "absl/base/config.h"
#include "absl/base/port.h"
#include "absl/meta/type_traits.h"
#include "absl/strings/internal/str_format/output.h"
#include "absl/strings/string_view.h"
......@@ -365,11 +366,22 @@ constexpr FormatConversionCharSet operator|(FormatConversionCharSet a,
static_cast<uint64_t>(b));
}
// Overloaded conversion functions to support absl::ParsedFormat.
// Get a conversion with a single character in it.
constexpr FormatConversionCharSet ConversionCharToConv(char c) {
return FormatConversionCharSet(FormatConversionCharToConvValue(c));
constexpr FormatConversionCharSet ToFormatConversionCharSet(char c) {
return static_cast<FormatConversionCharSet>(
FormatConversionCharToConvValue(c));
}
// Get a conversion with a single character in it.
constexpr FormatConversionCharSet ToFormatConversionCharSet(
FormatConversionCharSet c) {
return c;
}
template <typename T>
void ToFormatConversionCharSet(T) = delete;
// Checks whether `c` exists in `set`.
constexpr bool Contains(FormatConversionCharSet set, char c) {
return (static_cast<uint64_t>(set) & FormatConversionCharToConvValue(c)) != 0;
......
......@@ -285,7 +285,7 @@ using FormatSpec =
// }
template <char... Conv>
using ParsedFormat = str_format_internal::ExtendedParsedFormat<
str_format_internal::ConversionCharToConv(Conv)...>;
absl::str_format_internal::ToFormatConversionCharSet(Conv)...>;
// StrFormat()
//
......
......@@ -36,7 +36,7 @@ for compilation_mode in ${ABSL_CMAKE_BUILD_TYPES}; do
time cmake ${ABSEIL_ROOT} \
-GXcode \
-DCMAKE_BUILD_TYPE=${compilation_mode} \
-DCMAKE_CXX_FLAGS=-std=c++14 \
-DCMAKE_CXX_STANDARD=11 \
-DABSL_USE_GOOGLETEST_HEAD=ON \
-DABSL_RUN_TESTS=ON
time cmake --build .
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment