Skip to content

Commit

Permalink
Streamline creation of new elements in RepeatedPtrField
Browse files Browse the repository at this point in the history
The logic to create new elements in `RepeatedPtrField` was spread across an inconsistent patchwork of specializations of individual `GenericTypeHandler` static functions as well as external member and non-member functions, all living in multiple sources. Now all that logic is concentrated in three `GenericTypeHandler` full class specializations.

One change that to carefully verify in postsubmit is the now-removed old workaround for an MSVC bug #254 (MSVC compatibility is not checked in presubmit).

PiperOrigin-RevId: 670689695
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Sep 4, 2024
1 parent 5d34147 commit b90fa7f
Show file tree
Hide file tree
Showing 22 changed files with 218 additions and 449 deletions.
11 changes: 11 additions & 0 deletions src/google/protobuf/arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ template <typename T>
void arena_delete_object(void* object) {
delete reinterpret_cast<T*>(object);
}

inline bool CanUseInternalSwap(Arena* lhs, Arena* rhs) {
if (DebugHardenForceCopyInSwap()) {
// We force copy in swap when we are not using an arena.
// If we did with an arena we would grow arena usage too much.
return lhs != nullptr && lhs == rhs;
} else {
return lhs == rhs;
}
}

} // namespace internal

// ArenaOptions provides optional additional parameters to arena construction
Expand Down
28 changes: 14 additions & 14 deletions src/google/protobuf/arenastring.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,22 +503,22 @@ inline PROTOBUF_NDEBUG_INLINE void ArenaStringPtr::InternalSwap(
// Silence unused variable warnings in release buildls.
(void)arena;
std::swap(lhs->tagged_ptr_, rhs->tagged_ptr_);
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
for (auto* p : {lhs, rhs}) {
if (p->IsDefault()) continue;
std::string* old_value = p->tagged_ptr_.Get();
std::string* new_value =
p->IsFixedSizeArena()
? Arena::Create<std::string>(arena, *old_value)
: Arena::Create<std::string>(arena, std::move(*old_value));
if (arena == nullptr) {
delete old_value;
p->tagged_ptr_.SetAllocated(new_value);
} else {
p->tagged_ptr_.SetMutableArena(new_value);
if (internal::DebugHardenForceCopyInSwap()) {
for (auto* p : {lhs, rhs}) {
if (p->IsDefault()) continue;
std::string* old_value = p->tagged_ptr_.Get();
std::string* new_value =
p->IsFixedSizeArena()
? Arena::Create<std::string>(arena, *old_value)
: Arena::Create<std::string>(arena, std::move(*old_value));
if (arena == nullptr) {
delete old_value;
p->tagged_ptr_.SetAllocated(new_value);
} else {
p->tagged_ptr_.SetMutableArena(new_value);
}
}
}
#endif // PROTOBUF_FORCE_COPY_IN_SWAP
}

inline void ArenaStringPtr::ClearNonDefaultToEmpty() {
Expand Down
6 changes: 1 addition & 5 deletions src/google/protobuf/compiler/cpp/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2126,11 +2126,7 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* p) {
friend void swap($classname$& a, $classname$& b) { a.Swap(&b); }
inline void Swap($classname$* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() != nullptr && GetArena() == other->GetArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetArena() == other->GetArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
if ($pbi$::CanUseInternalSwap(GetArena(), other->GetArena())) {
InternalSwap(other);
} else {
$pbi$::GenericSwap(this, other);
Expand Down
6 changes: 1 addition & 5 deletions src/google/protobuf/compiler/java/java_features.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 4 additions & 20 deletions src/google/protobuf/compiler/plugin.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions src/google/protobuf/cpp_features.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b90fa7f

Please sign in to comment.