Skip to content

Commit

Permalink
Breaking Change: Remove deprecated RepeatedPtrField::ClearedCount().
Browse files Browse the repository at this point in the history
These have been marked ABSL_DEPRECATED as of v4.22.x and was intended for removal in v5.26.x: https://protobuf.dev/news/2023-12-13/#remove-deprecated-clear-apis-on-repeated-fields. Users of this API should consider migrating to arenas for better memory reuse.

See https://protobuf.dev/news/2024-10-02/#repeatedptrfieldclearedcount and https://protobuf.dev/support/migration/#cleared-elements

PiperOrigin-RevId: 690652745
  • Loading branch information
zhangskz authored and copybara-github committed Oct 28, 2024
1 parent 24ef3d6 commit e8e3253
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 46 deletions.
8 changes: 0 additions & 8 deletions src/google/protobuf/repeated_ptr_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -1146,10 +1146,6 @@ class RepeatedPtrField final : private internal::RepeatedPtrFieldBase {
// Hardcore programs may choose to manipulate these cleared objects
// to better optimize memory management using the following routines.

// Gets the number of cleared objects that are currently being kept
// around for reuse.
ABSL_DEPRECATED("This will be removed in a future release")
int ClearedCount() const;

// Removes the element referenced by position.
//
Expand Down Expand Up @@ -1526,10 +1522,6 @@ inline Element* RepeatedPtrField<Element>::UnsafeArenaReleaseLast() {
return RepeatedPtrFieldBase::UnsafeArenaReleaseLast<TypeHandler>();
}

template <typename Element>
inline int RepeatedPtrField<Element>::ClearedCount() const {
return RepeatedPtrFieldBase::ClearedCount();
}

template <typename Element>
inline void RepeatedPtrField<Element>::Reserve(int new_size) {
Expand Down
38 changes: 0 additions & 38 deletions src/google/protobuf/repeated_ptr_field_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -458,37 +458,6 @@ TEST(RepeatedPtrField, ReserveDoesntLoseAllocated) {
EXPECT_EQ(first, field.Add());
}

// Clearing elements is tricky with RepeatedPtrFields since the memory for
// the elements is retained and reused.
TEST(RepeatedPtrField, ClearedElements) {
PROTOBUF_IGNORE_DEPRECATION_START
RepeatedPtrField<std::string> field;

std::string* original = field.Add();
*original = "foo";

EXPECT_EQ(field.ClearedCount(), 0);

field.RemoveLast();
EXPECT_TRUE(original->empty());
EXPECT_EQ(field.ClearedCount(), 1);

EXPECT_EQ(field.Add(),
original); // Should return same string for reuse.
EXPECT_EQ(field.UnsafeArenaReleaseLast(), original); // We take ownership.
EXPECT_EQ(field.ClearedCount(), 0);

EXPECT_NE(field.Add(), original); // Should NOT return the same string.
EXPECT_EQ(field.ClearedCount(), 0);

field.UnsafeArenaAddAllocated(original); // Give ownership back.
EXPECT_EQ(field.ClearedCount(), 0);
EXPECT_EQ(field.Mutable(1), original);

field.Clear();
EXPECT_EQ(field.ClearedCount(), 2);
PROTOBUF_IGNORE_DEPRECATION_STOP
}

// Test all code paths in AddAllocated().
TEST(RepeatedPtrField, AddAllocated) {
Expand All @@ -512,7 +481,6 @@ TEST(RepeatedPtrField, AddAllocated) {
std::string* foo = new std::string("foo");
field.AddAllocated(foo);
EXPECT_EQ(index + 1, field.size());
EXPECT_EQ(0, field.ClearedCount());
EXPECT_EQ(foo, &field.Get(index));

// Last branch: Field is not at capacity and there are no cleared objects.
Expand All @@ -521,7 +489,6 @@ TEST(RepeatedPtrField, AddAllocated) {
field.AddAllocated(bar);
++index;
EXPECT_EQ(index + 1, field.size());
EXPECT_EQ(0, field.ClearedCount());
EXPECT_EQ(bar, &field.Get(index));

// Third branch: Field is not at capacity and there are no cleared objects.
Expand All @@ -530,7 +497,6 @@ TEST(RepeatedPtrField, AddAllocated) {
std::string* baz = new std::string("baz");
field.AddAllocated(baz);
EXPECT_EQ(index + 1, field.size());
EXPECT_EQ(1, field.ClearedCount());
EXPECT_EQ(baz, &field.Get(index));

// Second branch: Field is at capacity but has some cleared objects.
Expand All @@ -541,7 +507,6 @@ TEST(RepeatedPtrField, AddAllocated) {
field.AddAllocated(moo);
EXPECT_EQ(index + 1, field.size());
// We should have discarded the cleared object.
EXPECT_EQ(0, field.ClearedCount());
EXPECT_EQ(moo, &field.Get(index));
}

Expand Down Expand Up @@ -937,7 +902,6 @@ TEST(RepeatedPtrField, ExtractSubrange) {
EXPECT_EQ(field.size(), sz + extra);
for (int i = 0; i < extra; ++i) field.RemoveLast();
EXPECT_EQ(field.size(), sz);
EXPECT_EQ(field.ClearedCount(), extra);

// Create a catcher array and call ExtractSubrange.
std::string* catcher[10];
Expand All @@ -959,9 +923,7 @@ TEST(RepeatedPtrField, ExtractSubrange) {
EXPECT_EQ(field.Mutable(i), subject[i + num]);

// Reinstate the cleared elements.
EXPECT_EQ(field.ClearedCount(), extra);
for (int i = 0; i < extra; ++i) field.Add();
EXPECT_EQ(field.ClearedCount(), 0);
EXPECT_EQ(field.size(), sz - num + extra);

// Make sure the extra elements are all there (in some order).
Expand Down

0 comments on commit e8e3253

Please sign in to comment.