Skip to content

Commit e2525e6

Browse files
ezbrcopybara-github
authored andcommitted
Enable small object optimization (SOO) for RepeatedField in order to 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
1 parent 05e7398 commit e2525e6

File tree

4 files changed

+425
-221
lines changed

4 files changed

+425
-221
lines changed

src/google/protobuf/compiler/cpp/unittest.inc

+2-15
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
#include <memory>
2828
#include <vector>
2929

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

485-
// Note the address of one of the repeated fields, to verify it was swapped
486-
// rather than copied.
487-
const int32_t* addr = &message1.repeated_int32().Get(0);
488-
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
489-
const int32_t value = *addr;
490-
#endif
491-
492486
using std::swap;
493487
swap(message1, message2);
494488

495489
TestUtil::ExpectAllFieldsSet(message2);
496490
TestUtil::ExpectClear(message1);
497-
498-
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
499-
EXPECT_NE(addr, &message2.repeated_int32().Get(0));
500-
EXPECT_EQ(value, message2.repeated_int32().Get(0));
501-
#else
502-
EXPECT_EQ(addr, &message2.repeated_int32().Get(0));
503-
#endif
504491
}
505492

506493
TEST(GENERATED_MESSAGE_TEST_NAME, CopyConstructor) {

src/google/protobuf/extension_set_unittest.cc

+7-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "google/protobuf/extension_set.h"
1313

14+
#include <algorithm>
1415
#include <cstdint>
1516
#include <string>
1617

@@ -841,10 +842,12 @@ TEST(ExtensionSetTest, SpaceUsedExcludingSelf) {
841842
const size_t old_capacity = \
842843
message->GetRepeatedExtension(unittest::repeated_##type##_extension) \
843844
.Capacity(); \
844-
EXPECT_GE( \
845-
old_capacity, \
846-
(RepeatedFieldLowerClampLimit<cpptype, std::max(sizeof(cpptype), \
847-
sizeof(void*))>())); \
845+
if (sizeof(cpptype) > 1) { \
846+
EXPECT_GE( \
847+
old_capacity, \
848+
(RepeatedFieldLowerClampLimit<cpptype, std::max(sizeof(cpptype), \
849+
sizeof(void*))>())); \
850+
} \
848851
for (int i = 0; i < 16; ++i) { \
849852
message->AddExtension(unittest::repeated_##type##_extension, value); \
850853
} \

0 commit comments

Comments
 (0)