diff options
author | Abseil Team <absl-team@google.com> | 2023-06-29 09:25:56 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-06-29 09:26:57 -0700 |
commit | 53fbcb883dc8ca208dc58a8cc168d0628fe2556f (patch) | |
tree | 7075fe3a70f796194137f08957e54a6086b4cf9e /absl/strings/cord_test.cc | |
parent | bde85071e497254e954c27b1b81b442a441ad4b0 (diff) |
Introduce a kTotalMorePrecise accounting mode for Cord::EstimatedMemoryUsage(). This mode avoids double-counting blocks that a Cord references more than once; otherwise it is similar to the existing kTotal mode.
There's no change to the existing kTotal or kFairShare accounting modes.
PiperOrigin-RevId: 544378591
Change-Id: I7b4ae55cd93d631194e59a9cd0ff07f47611219e
Diffstat (limited to 'absl/strings/cord_test.cc')
-rw-r--r-- | absl/strings/cord_test.cc | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/absl/strings/cord_test.cc b/absl/strings/cord_test.cc index 55412c7f..36e397ed 100644 --- a/absl/strings/cord_test.cc +++ b/absl/strings/cord_test.cc @@ -1765,6 +1765,8 @@ TEST_P(CordTest, ExternalMemoryGet) { // of empty and inlined cords, and flat nodes. constexpr auto kFairShare = absl::CordMemoryAccounting::kFairShare; +constexpr auto kTotalMorePrecise = + absl::CordMemoryAccounting::kTotalMorePrecise; // Creates a cord of `n` `c` values, making sure no string stealing occurs. absl::Cord MakeCord(size_t n, char c) { @@ -1776,12 +1778,14 @@ TEST(CordTest, CordMemoryUsageEmpty) { absl::Cord cord; EXPECT_EQ(sizeof(absl::Cord), cord.EstimatedMemoryUsage()); EXPECT_EQ(sizeof(absl::Cord), cord.EstimatedMemoryUsage(kFairShare)); + EXPECT_EQ(sizeof(absl::Cord), cord.EstimatedMemoryUsage(kTotalMorePrecise)); } TEST(CordTest, CordMemoryUsageInlined) { absl::Cord a("hello"); EXPECT_EQ(a.EstimatedMemoryUsage(), sizeof(absl::Cord)); EXPECT_EQ(a.EstimatedMemoryUsage(kFairShare), sizeof(absl::Cord)); + EXPECT_EQ(a.EstimatedMemoryUsage(kTotalMorePrecise), sizeof(absl::Cord)); } TEST(CordTest, CordMemoryUsageExternalMemory) { @@ -1791,6 +1795,7 @@ TEST(CordTest, CordMemoryUsageExternalMemory) { sizeof(absl::Cord) + 1000 + sizeof(CordRepExternal) + sizeof(intptr_t); EXPECT_EQ(cord.EstimatedMemoryUsage(), expected); EXPECT_EQ(cord.EstimatedMemoryUsage(kFairShare), expected); + EXPECT_EQ(cord.EstimatedMemoryUsage(kTotalMorePrecise), expected); } TEST(CordTest, CordMemoryUsageFlat) { @@ -1800,6 +1805,8 @@ TEST(CordTest, CordMemoryUsageFlat) { EXPECT_EQ(cord.EstimatedMemoryUsage(), sizeof(absl::Cord) + flat_size); EXPECT_EQ(cord.EstimatedMemoryUsage(kFairShare), sizeof(absl::Cord) + flat_size); + EXPECT_EQ(cord.EstimatedMemoryUsage(kTotalMorePrecise), + sizeof(absl::Cord) + flat_size); } TEST(CordTest, CordMemoryUsageSubStringSharedFlat) { @@ -1809,6 +1816,8 @@ TEST(CordTest, CordMemoryUsageSubStringSharedFlat) { absl::Cord cord = flat.Subcord(500, 1000); EXPECT_EQ(cord.EstimatedMemoryUsage(), sizeof(absl::Cord) + sizeof(CordRepSubstring) + flat_size); + EXPECT_EQ(cord.EstimatedMemoryUsage(kTotalMorePrecise), + sizeof(absl::Cord) + sizeof(CordRepSubstring) + flat_size); EXPECT_EQ(cord.EstimatedMemoryUsage(kFairShare), sizeof(absl::Cord) + sizeof(CordRepSubstring) + flat_size / 2); } @@ -1819,6 +1828,8 @@ TEST(CordTest, CordMemoryUsageFlatShared) { const size_t flat_size = absl::CordTestPeer::Tree(cord)->flat()->AllocatedSize(); EXPECT_EQ(cord.EstimatedMemoryUsage(), sizeof(absl::Cord) + flat_size); + EXPECT_EQ(cord.EstimatedMemoryUsage(kTotalMorePrecise), + sizeof(absl::Cord) + flat_size); EXPECT_EQ(cord.EstimatedMemoryUsage(kFairShare), sizeof(absl::Cord) + flat_size / 2); } @@ -1837,6 +1848,8 @@ TEST(CordTest, CordMemoryUsageFlatHardenedAndShared) { absl::Cord cord2(cord); EXPECT_EQ(cord2.EstimatedMemoryUsage(), sizeof(absl::Cord) + sizeof(CordRepCrc) + flat_size); + EXPECT_EQ(cord2.EstimatedMemoryUsage(kTotalMorePrecise), + sizeof(absl::Cord) + sizeof(CordRepCrc) + flat_size); EXPECT_EQ(cord2.EstimatedMemoryUsage(kFairShare), sizeof(absl::Cord) + (sizeof(CordRepCrc) + flat_size / 2) / 2); } @@ -1863,6 +1876,8 @@ TEST(CordTest, CordMemoryUsageBTree) { size_t rep1_shared_size = sizeof(CordRepBtree) + flats1_size / 2; EXPECT_EQ(cord1.EstimatedMemoryUsage(), sizeof(absl::Cord) + rep1_size); + EXPECT_EQ(cord1.EstimatedMemoryUsage(kTotalMorePrecise), + sizeof(absl::Cord) + rep1_size); EXPECT_EQ(cord1.EstimatedMemoryUsage(kFairShare), sizeof(absl::Cord) + rep1_shared_size); @@ -1877,6 +1892,8 @@ TEST(CordTest, CordMemoryUsageBTree) { size_t rep2_size = sizeof(CordRepBtree) + flats2_size; EXPECT_EQ(cord2.EstimatedMemoryUsage(), sizeof(absl::Cord) + rep2_size); + EXPECT_EQ(cord2.EstimatedMemoryUsage(kTotalMorePrecise), + sizeof(absl::Cord) + rep2_size); EXPECT_EQ(cord2.EstimatedMemoryUsage(kFairShare), sizeof(absl::Cord) + rep2_size); @@ -1885,6 +1902,8 @@ TEST(CordTest, CordMemoryUsageBTree) { EXPECT_EQ(cord.EstimatedMemoryUsage(), sizeof(absl::Cord) + sizeof(CordRepBtree) + rep1_size + rep2_size); + EXPECT_EQ(cord.EstimatedMemoryUsage(kTotalMorePrecise), + 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); @@ -1903,6 +1922,66 @@ TEST_P(CordTest, CordMemoryUsageInlineRep) { EXPECT_EQ(c1.EstimatedMemoryUsage(), c2.EstimatedMemoryUsage()); } +TEST_P(CordTest, CordMemoryUsageTotalMorePreciseMode) { + constexpr size_t kChunkSize = 2000; + std::string tmp_str(kChunkSize, 'x'); + const absl::Cord flat(std::move(tmp_str)); + + // Construct `fragmented` with two references into the same + // underlying buffer shared with `flat`: + absl::Cord fragmented(flat); + fragmented.Append(flat); + + // Memory usage of `flat`, minus the top-level Cord object: + const size_t flat_internal_usage = + flat.EstimatedMemoryUsage() - sizeof(absl::Cord); + + // `fragmented` holds a Cord and a CordRepBtree. That tree points to two + // copies of flat's internals, which we expect to dedup: + EXPECT_EQ(fragmented.EstimatedMemoryUsage(kTotalMorePrecise), + sizeof(absl::Cord) + + sizeof(CordRepBtree) + + flat_internal_usage); + + // This is a case where kTotal produces an overestimate: + EXPECT_EQ(fragmented.EstimatedMemoryUsage(), + sizeof(absl::Cord) + + sizeof(CordRepBtree) + + 2 * flat_internal_usage); +} + +TEST_P(CordTest, CordMemoryUsageTotalMorePreciseModeWithSubstring) { + constexpr size_t kChunkSize = 2000; + std::string tmp_str(kChunkSize, 'x'); + const absl::Cord flat(std::move(tmp_str)); + + // Construct `fragmented` with two references into the same + // underlying buffer shared with `flat`. + // + // This time, each reference is through a Subcord(): + absl::Cord fragmented; + fragmented.Append(flat.Subcord(1, kChunkSize - 2)); + fragmented.Append(flat.Subcord(1, kChunkSize - 2)); + + // Memory usage of `flat`, minus the top-level Cord object: + const size_t flat_internal_usage = + flat.EstimatedMemoryUsage() - sizeof(absl::Cord); + + // `fragmented` holds a Cord and a CordRepBtree. That tree points to two + // CordRepSubstrings, each pointing at flat's internals. + EXPECT_EQ(fragmented.EstimatedMemoryUsage(kTotalMorePrecise), + sizeof(absl::Cord) + + sizeof(CordRepBtree) + + 2 * sizeof(CordRepSubstring) + + flat_internal_usage); + + // This is a case where kTotal produces an overestimate: + EXPECT_EQ(fragmented.EstimatedMemoryUsage(), + sizeof(absl::Cord) + + sizeof(CordRepBtree) + + 2 * sizeof(CordRepSubstring) + + 2 * flat_internal_usage); +} } // namespace // Regtest for 7510292 (fix a bug introduced by 7465150) |