Skip to content

Commit

Permalink
Enable small object optimization (SOO) for RepeatedField in order to …
Browse files Browse the repository at this point in the history
…reduce data indirections.

The arena and the heap representation are both aligned to 8 bytes so we can use the lowest 3 bits of the heap pointer to encode whether the RepeatedField is in SOO mode or not, and if it is in SOO mode, to record the size. Therefore, we can have SOO sizes from 0 to 3 and one bit for whether we're in SOO mode. Note that we also tried using 3 bits for SOO size with a sentinel value for not-SOO-mode, but that was slower.

PiperOrigin-RevId: 659578775
  • Loading branch information
ezbr authored and copybara-github committed Aug 5, 2024
1 parent 05e7398 commit e2525e6
Show file tree
Hide file tree
Showing 4 changed files with 425 additions and 221 deletions.
17 changes: 2 additions & 15 deletions src/google/protobuf/compiler/cpp/unittest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
#include <memory>
#include <vector>

#include "google/protobuf/compiler/cpp/unittest.h"
#include "absl/base/attributes.h"
#include "absl/strings/cord.h"
#include "google/protobuf/compiler/cpp/unittest.h"
#include "absl/strings/string_view.h"
#ifndef _MSC_VER
// We exclude this large proto because it's too large for
Expand Down Expand Up @@ -482,25 +483,11 @@ TEST(GENERATED_MESSAGE_TEST_NAME, ADLSwap) {
UNITTEST::TestAllTypes message1, message2;
TestUtil::SetAllFields(&message1);

// Note the address of one of the repeated fields, to verify it was swapped
// rather than copied.
const int32_t* addr = &message1.repeated_int32().Get(0);
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
const int32_t value = *addr;
#endif

using std::swap;
swap(message1, message2);

TestUtil::ExpectAllFieldsSet(message2);
TestUtil::ExpectClear(message1);

#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
EXPECT_NE(addr, &message2.repeated_int32().Get(0));
EXPECT_EQ(value, message2.repeated_int32().Get(0));
#else
EXPECT_EQ(addr, &message2.repeated_int32().Get(0));
#endif
}

TEST(GENERATED_MESSAGE_TEST_NAME, CopyConstructor) {
Expand Down
11 changes: 7 additions & 4 deletions src/google/protobuf/extension_set_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "google/protobuf/extension_set.h"

#include <algorithm>
#include <cstdint>
#include <string>

Expand Down Expand Up @@ -841,10 +842,12 @@ TEST(ExtensionSetTest, SpaceUsedExcludingSelf) {
const size_t old_capacity = \
message->GetRepeatedExtension(unittest::repeated_##type##_extension) \
.Capacity(); \
EXPECT_GE( \
old_capacity, \
(RepeatedFieldLowerClampLimit<cpptype, std::max(sizeof(cpptype), \
sizeof(void*))>())); \
if (sizeof(cpptype) > 1) { \
EXPECT_GE( \
old_capacity, \
(RepeatedFieldLowerClampLimit<cpptype, std::max(sizeof(cpptype), \
sizeof(void*))>())); \
} \
for (int i = 0; i < 16; ++i) { \
message->AddExtension(unittest::repeated_##type##_extension, value); \
} \
Expand Down
Loading

0 comments on commit e2525e6

Please sign in to comment.