Skip to content

Change how we decide which empty string implementation to use. #20708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions src/google/protobuf/port.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,9 @@ void RealDebugCounter::Register(absl::string_view name) {
}
}

#if defined(__cpp_lib_constexpr_string) && __cpp_lib_constexpr_string >= 201907L
PROTOBUF_ATTRIBUTE_NO_DESTROY PROTOBUF_CONSTINIT const GlobalEmptyString
fixed_address_empty_string{};
#else
PROTOBUF_ATTRIBUTE_NO_DESTROY PROTOBUF_CONSTINIT
PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 GlobalEmptyString
fixed_address_empty_string{};
#endif

} // namespace internal
} // namespace protobuf
Expand Down
23 changes: 17 additions & 6 deletions src/google/protobuf/port.h
Original file line number Diff line number Diff line change
Expand Up @@ -494,20 +494,27 @@ class NoopDebugCounter {
// Default empty string object. Don't use this directly. Instead, call
// GetEmptyString() to get the reference. This empty string is aligned with a
// minimum alignment of 8 bytes to match the requirement of ArenaStringPtr.
#if defined(__cpp_lib_constexpr_string) && __cpp_lib_constexpr_string >= 201907L

// Take advantage of C++20 constexpr support in std::string.
class alignas(8) GlobalEmptyString {
class alignas(8) GlobalEmptyStringConstexpr {
public:
const std::string& get() const { return value_; }
// Nothing to init, or destroy.
std::string* Init() const { return nullptr; }

template <typename T = std::string, bool = (T(), true)>
static constexpr std::true_type HasConstexprDefaultConstructor(int) {
return {};
}
static constexpr std::false_type HasConstexprDefaultConstructor(char) {
return {};
}

private:
std::string value_;
};
PROTOBUF_EXPORT extern const GlobalEmptyString fixed_address_empty_string;
#else
class alignas(8) GlobalEmptyString {

class alignas(8) GlobalEmptyStringDynamicInit {
public:
const std::string& get() const {
return *reinterpret_cast<const std::string*>(internal::Launder(buffer_));
Expand All @@ -519,8 +526,12 @@ class alignas(8) GlobalEmptyString {
private:
alignas(std::string) char buffer_[sizeof(std::string)];
};

using GlobalEmptyString = std::conditional_t<
GlobalEmptyStringConstexpr::HasConstexprDefaultConstructor(0),
const GlobalEmptyStringConstexpr, GlobalEmptyStringDynamicInit>;

PROTOBUF_EXPORT extern GlobalEmptyString fixed_address_empty_string;
#endif

} // namespace internal
} // namespace protobuf
Expand Down
Loading