summaryrefslogtreecommitdiff
path: root/absl/strings/cord_test.cc
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2021-12-14 12:42:15 -0800
committerGravatar rogeeff <rogeeff@google.com>2021-12-15 09:56:15 -0500
commit52d41a9ec23e39db7e2cbce5c9449506cf2d3a5c (patch)
tree8dd98e467b4808228e1da309abf11f85b2062c0e /absl/strings/cord_test.cc
parent1065514ef332d036f3437e950e78d35ce6b7c740 (diff)
Export of internal Abseil changes
-- 81f95fcf85b75b84f9892c73123501472b9cff33 by Martijn Vels <mvels@google.com>: Introduce GetEstimatedMemoryUsage(CordMemoryAccounting::kFairShare) Memory usage analysis is moved into a separate cord_analysis.cc source. PiperOrigin-RevId: 416370158 -- 6bc7b1348fd27fe53f100c9eabd47f4f2cb9c19c by Abseil Team <absl-team@google.com>: Support scoped enum in absl::Substitute. PiperOrigin-RevId: 416345422 -- 6399f4f6ae05ebcd67664ebd844902f699ab8ec7 by Abseil Team <absl-team@google.com>: Correct the computation of contention cycles Currently, we record contention cycles from the first time a thread started waiting on a mutex. Consider a situation in which two threads, T1 and T2, run a loop at the top of which they acquire a common mutex and release it at the end of the loop body. Further assume that T2 is never able to acquire the mutex as T1 repeatedly acquires and then releases the mutex. In this case, we would expect that the reported contention cycles would be increase linearly over time. But currently we observe a quadratic behavior in the reported waiting time as mentioned in b/14684244#comment10. To fix the issue, this CL records the contention cycles experienced by all the threads woken up when the mutex is released. Further, contention_start_cycles is set to the current time since the contention cycles for the time already passed has been taken into account. With this CL, we get a linear increase in the waiting time, the expected behavior. PiperOrigin-RevId: 416322593 -- 149c1637c8a0f1a38e5a8f9f27e5803a2015a554 by Jorg Brown <jorg@google.com>: Make Status::EmptyString more efficient by constructing it in global space, rather than on the heap. See https://godbolt.org/z/8M9n7YqcY for reduced code size. PiperOrigin-RevId: 416307833 -- 3b4562a8be5a3c80077cb67b0a32c97419058380 by Abseil Team <absl-team@google.com>: Clarify the usage of RegisterMutexProfiler PiperOrigin-RevId: 416146130 GitOrigin-RevId: 81f95fcf85b75b84f9892c73123501472b9cff33 Change-Id: Iccb72d7ee617e6ebe226a38170d62e0849b43480
Diffstat (limited to 'absl/strings/cord_test.cc')
-rw-r--r--absl/strings/cord_test.cc236
1 files changed, 176 insertions, 60 deletions
diff --git a/absl/strings/cord_test.cc b/absl/strings/cord_test.cc
index a22db0bb..e499b55f 100644
--- a/absl/strings/cord_test.cc
+++ b/absl/strings/cord_test.cc
@@ -50,7 +50,12 @@ static constexpr auto MAX_FLAT_TAG = absl::cord_internal::MAX_FLAT_TAG;
typedef std::mt19937_64 RandomEngine;
using absl::cord_internal::CordRep;
+using absl::cord_internal::CordRepBtree;
+using absl::cord_internal::CordRepConcat;
+using absl::cord_internal::CordRepCrc;
+using absl::cord_internal::CordRepExternal;
using absl::cord_internal::CordRepFlat;
+using absl::cord_internal::CordRepSubstring;
using absl::cord_internal::kFlatOverhead;
using absl::cord_internal::kMaxFlatLength;
@@ -196,6 +201,7 @@ class CordTestPeer {
}
static bool IsTree(const Cord& c) { return c.contents_.is_tree(); }
+ static CordRep* Tree(const Cord& c) { return c.contents_.tree(); }
static cord_internal::CordzInfo* GetCordzInfo(const Cord& c) {
return c.contents_.cordz_info();
@@ -1474,76 +1480,186 @@ TEST_P(CordTest, ExternalMemoryGet) {
}
// CordMemoryUsage tests verify the correctness of the EstimatedMemoryUsage()
-// These tests take into account that the reported memory usage is approximate
-// and non-deterministic. For all tests, We verify that the reported memory
-// usage is larger than `size()`, and less than `size() * 1.5` as a cord should
-// never reserve more 'extra' capacity than half of its size as it grows.
-// Additionally we have some whiteboxed expectations based on our knowledge of
-// the layout and size of empty and inlined cords, and flat nodes.
+// We use whiteboxed expectations based on our knowledge of the layout and size
+// of empty and inlined cords, and flat nodes.
-TEST_P(CordTest, CordMemoryUsageEmpty) {
- EXPECT_EQ(sizeof(absl::Cord), absl::Cord().EstimatedMemoryUsage());
-}
-
-TEST_P(CordTest, CordMemoryUsageEmbedded) {
- absl::Cord a("hello");
- EXPECT_EQ(a.EstimatedMemoryUsage(), sizeof(absl::Cord));
-}
+constexpr auto kFairShare = absl::CordMemoryAccounting::kFairShare;
-TEST_P(CordTest, CordMemoryUsageEmbeddedAppend) {
- absl::Cord a("a");
- absl::Cord b("bcd");
- EXPECT_EQ(b.EstimatedMemoryUsage(), sizeof(absl::Cord));
- a.Append(b);
- EXPECT_EQ(a.EstimatedMemoryUsage(), sizeof(absl::Cord));
+// Creates a cord of `n` `c` values, making sure no string stealing occurs.
+absl::Cord MakeCord(size_t n, char c) {
+ const std::string s(n, c);
+ return absl::Cord(s);
}
-TEST_P(CordTest, CordMemoryUsageExternalMemory) {
- static const int kLength = 1000;
+TEST(CordTest, CordMemoryUsageEmpty) {
absl::Cord cord;
- AddExternalMemory(std::string(kLength, 'x'), &cord);
- EXPECT_GT(cord.EstimatedMemoryUsage(), kLength);
- EXPECT_LE(cord.EstimatedMemoryUsage(), kLength * 1.5);
-}
-
-TEST_P(CordTest, CordMemoryUsageFlat) {
- static const int kLength = 125;
- absl::Cord a(std::string(kLength, 'a'));
- EXPECT_GT(a.EstimatedMemoryUsage(), kLength);
- EXPECT_LE(a.EstimatedMemoryUsage(), kLength * 1.5);
+ EXPECT_EQ(sizeof(absl::Cord), cord.EstimatedMemoryUsage());
+ EXPECT_EQ(sizeof(absl::Cord), cord.EstimatedMemoryUsage(kFairShare));
}
-TEST_P(CordTest, CordMemoryUsageAppendFlat) {
- using absl::strings_internal::CordTestAccess;
- absl::Cord a(std::string(CordTestAccess::MaxFlatLength(), 'a'));
- size_t length = a.EstimatedMemoryUsage();
- a.Append(std::string(CordTestAccess::MaxFlatLength(), 'b'));
- size_t delta = a.EstimatedMemoryUsage() - length;
- EXPECT_GT(delta, CordTestAccess::MaxFlatLength());
- EXPECT_LE(delta, CordTestAccess::MaxFlatLength() * 1.5);
+TEST(CordTest, CordMemoryUsageInlined) {
+ absl::Cord a("hello");
+ EXPECT_EQ(a.EstimatedMemoryUsage(), sizeof(absl::Cord));
+ EXPECT_EQ(a.EstimatedMemoryUsage(kFairShare), sizeof(absl::Cord));
}
-TEST_P(CordTest, CordMemoryUsageAppendExternal) {
- static const int kLength = 1000;
- using absl::strings_internal::CordTestAccess;
- absl::Cord a(std::string(CordTestAccess::MaxFlatLength(), 'a'));
- size_t length = a.EstimatedMemoryUsage();
- AddExternalMemory(std::string(kLength, 'b'), &a);
- size_t delta = a.EstimatedMemoryUsage() - length;
- EXPECT_GT(delta, kLength);
- EXPECT_LE(delta, kLength * 1.5);
-}
+TEST(CordTest, CordMemoryUsageExternalMemory) {
+ absl::Cord cord;
+ AddExternalMemory(std::string(1000, 'x'), &cord);
+ const size_t expected =
+ sizeof(absl::Cord) + 1000 + sizeof(CordRepExternal) + sizeof(intptr_t);
+ EXPECT_EQ(cord.EstimatedMemoryUsage(), expected);
+ EXPECT_EQ(cord.EstimatedMemoryUsage(kFairShare), expected);
+}
+
+TEST(CordTest, CordMemoryUsageFlat) {
+ absl::Cord cord = MakeCord(1000, 'a');
+ const size_t flat_size =
+ absl::CordTestPeer::Tree(cord)->flat()->AllocatedSize();
+ EXPECT_EQ(cord.EstimatedMemoryUsage(), sizeof(absl::Cord) + flat_size);
+ EXPECT_EQ(cord.EstimatedMemoryUsage(kFairShare),
+ sizeof(absl::Cord) + flat_size);
+}
+
+TEST(CordTest, CordMemoryUsageSubStringSharedFlat) {
+ absl::Cord flat = MakeCord(2000, 'a');
+ const size_t flat_size =
+ absl::CordTestPeer::Tree(flat)->flat()->AllocatedSize();
+ absl::Cord cord = flat.Subcord(500, 1000);
+ EXPECT_EQ(cord.EstimatedMemoryUsage(),
+ sizeof(absl::Cord) + sizeof(CordRepSubstring) + flat_size);
+ EXPECT_EQ(cord.EstimatedMemoryUsage(kFairShare),
+ sizeof(absl::Cord) + sizeof(CordRepSubstring) + flat_size / 2);
+}
+
+TEST(CordTest, CordMemoryUsageFlatShared) {
+ absl::Cord shared = MakeCord(1000, 'a');
+ absl::Cord cord(shared);
+ const size_t flat_size =
+ absl::CordTestPeer::Tree(cord)->flat()->AllocatedSize();
+ EXPECT_EQ(cord.EstimatedMemoryUsage(), sizeof(absl::Cord) + flat_size);
+ EXPECT_EQ(cord.EstimatedMemoryUsage(kFairShare),
+ sizeof(absl::Cord) + flat_size / 2);
+}
+
+TEST(CordTest, CordMemoryUsageFlatHardenedAndShared) {
+ absl::Cord shared = MakeCord(1000, 'a');
+ absl::Cord cord(shared);
+ const size_t flat_size =
+ absl::CordTestPeer::Tree(cord)->flat()->AllocatedSize();
+ cord.SetExpectedChecksum(1);
+ EXPECT_EQ(cord.EstimatedMemoryUsage(),
+ sizeof(absl::Cord) + sizeof(CordRepCrc) + flat_size);
+ EXPECT_EQ(cord.EstimatedMemoryUsage(kFairShare),
+ sizeof(absl::Cord) + sizeof(CordRepCrc) + flat_size / 2);
+
+ absl::Cord cord2(cord);
+ EXPECT_EQ(cord2.EstimatedMemoryUsage(),
+ sizeof(absl::Cord) + sizeof(CordRepCrc) + flat_size);
+ EXPECT_EQ(cord2.EstimatedMemoryUsage(kFairShare),
+ sizeof(absl::Cord) + (sizeof(CordRepCrc) + flat_size / 2) / 2);
+}
+
+TEST(CordTest, CordMemoryUsageBTree) {
+ absl::cord_internal::enable_cord_btree(true);
+
+ absl::Cord cord1;
+ size_t flats1_size = 0;
+ absl::Cord flats1[4] = {MakeCord(1000, 'a'), MakeCord(1100, 'a'),
+ MakeCord(1200, 'a'), MakeCord(1300, 'a')};
+ for (absl::Cord flat : flats1) {
+ flats1_size += absl::CordTestPeer::Tree(flat)->flat()->AllocatedSize();
+ cord1.Append(std::move(flat));
+ }
+
+ // Make sure the created cord is a BTREE tree. Under some builds such as
+ // windows DLL, we may have ODR like effects on the flag, meaning the DLL
+ // code will run with the picked up default.
+ if (!absl::CordTestPeer::Tree(cord1)->IsBtree()) {
+ ABSL_RAW_LOG(WARNING, "Cord library code not respecting btree flag");
+ return;
+ }
+
+ size_t rep1_size = sizeof(CordRepBtree) + flats1_size;
+ size_t rep1_shared_size = sizeof(CordRepBtree) + flats1_size / 2;
+
+ EXPECT_EQ(cord1.EstimatedMemoryUsage(), sizeof(absl::Cord) + rep1_size);
+ EXPECT_EQ(cord1.EstimatedMemoryUsage(kFairShare),
+ sizeof(absl::Cord) + rep1_shared_size);
+
+ absl::Cord cord2;
+ size_t flats2_size = 0;
+ absl::Cord flats2[4] = {MakeCord(600, 'a'), MakeCord(700, 'a'),
+ MakeCord(800, 'a'), MakeCord(900, 'a')};
+ for (absl::Cord& flat : flats2) {
+ flats2_size += absl::CordTestPeer::Tree(flat)->flat()->AllocatedSize();
+ cord2.Append(std::move(flat));
+ }
+ size_t rep2_size = sizeof(CordRepBtree) + flats2_size;
+
+ EXPECT_EQ(cord2.EstimatedMemoryUsage(), sizeof(absl::Cord) + rep2_size);
+ EXPECT_EQ(cord2.EstimatedMemoryUsage(kFairShare),
+ sizeof(absl::Cord) + rep2_size);
+
+ absl::Cord cord(cord1);
+ cord.Append(std::move(cord2));
+
+ EXPECT_EQ(cord.EstimatedMemoryUsage(),
+ sizeof(absl::Cord) + sizeof(CordRepBtree) + rep1_size + rep2_size);
+ EXPECT_EQ(cord.EstimatedMemoryUsage(kFairShare),
+ sizeof(absl::Cord) + sizeof(CordRepBtree) + rep1_shared_size / 2 +
+ rep2_size);
+}
+
+TEST(CordTest, CordMemoryUsageConcat) {
+ absl::cord_internal::enable_cord_btree(false);
+
+ absl::Cord cord1;
+ size_t flats1_size = 0;
+ absl::Cord flats1[4] = {MakeCord(1000, 'a'), MakeCord(1100, 'a'),
+ MakeCord(1200, 'a'), MakeCord(1300, 'a')};
+ for (absl::Cord flat : flats1) {
+ flats1_size += absl::CordTestPeer::Tree(flat)->flat()->AllocatedSize();
+ cord1.Append(std::move(flat));
+ }
+
+ // Make sure the created cord is a CONCAT tree. Under some builds such as
+ // windows DLL, we may have ODR like effects on the flag, meaning the DLL
+ // code will run with the picked up default.
+ if (!absl::CordTestPeer::Tree(cord1)->IsConcat()) {
+ ABSL_RAW_LOG(WARNING, "Cord library code not respecting btree flag");
+ return;
+ }
+
+ size_t rep1_size = sizeof(CordRepConcat) * 3 + flats1_size;
+ size_t rep1_shared_size = sizeof(CordRepConcat) * 3 + flats1_size / 2;
+
+ EXPECT_EQ(cord1.EstimatedMemoryUsage(), sizeof(absl::Cord) + rep1_size);
+ EXPECT_EQ(cord1.EstimatedMemoryUsage(kFairShare),
+ sizeof(absl::Cord) + rep1_shared_size);
+
+ absl::Cord cord2;
+ size_t flats2_size = 0;
+ absl::Cord flats2[4] = {MakeCord(600, 'a'), MakeCord(700, 'a'),
+ MakeCord(800, 'a'), MakeCord(900, 'a')};
+ for (absl::Cord& flat : flats2) {
+ flats2_size += absl::CordTestPeer::Tree(flat)->flat()->AllocatedSize();
+ cord2.Append(std::move(flat));
+ }
+
+ size_t rep2_size = sizeof(CordRepConcat) * 3 + flats2_size;
+
+ EXPECT_EQ(cord2.EstimatedMemoryUsage(), sizeof(absl::Cord) + rep2_size);
+ EXPECT_EQ(cord2.EstimatedMemoryUsage(kFairShare),
+ sizeof(absl::Cord) + rep2_size);
-TEST_P(CordTest, CordMemoryUsageSubString) {
- static const int kLength = 2000;
- using absl::strings_internal::CordTestAccess;
- absl::Cord a(std::string(kLength, 'a'));
- size_t length = a.EstimatedMemoryUsage();
- AddExternalMemory(std::string(kLength, 'b'), &a);
- absl::Cord b = a.Subcord(0, kLength + kLength / 2);
- size_t delta = b.EstimatedMemoryUsage() - length;
- EXPECT_GT(delta, kLength);
- EXPECT_LE(delta, kLength * 1.5);
+ absl::Cord cord(cord1);
+ cord.Append(std::move(cord2));
+ EXPECT_EQ(cord.EstimatedMemoryUsage(),
+ sizeof(absl::Cord) + sizeof(CordRepConcat) + rep1_size + rep2_size);
+ EXPECT_EQ(cord.EstimatedMemoryUsage(kFairShare),
+ sizeof(absl::Cord) + sizeof(CordRepConcat) + rep1_shared_size / 2 +
+ rep2_size);
}
// Regtest for a change that had to be rolled back because it expanded out