Skip to content

Commit

Permalink
Automated rollback of commit 3849bc6.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 652577976
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jul 15, 2024
1 parent 9c99424 commit bcd9905
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ class SingularString : public FieldGeneratorBase {
}

void GenerateOneofCopyConstruct(io::Printer* p) const override {
ABSL_CHECK(!is_inlined());
if (EmptyDefault()) {
if (is_inlined() || EmptyDefault()) {
p->Emit("new (&$field$) decltype($field$){arena, from.$field$};\n");
} else {
p->Emit(
Expand Down Expand Up @@ -210,6 +209,12 @@ void SingularString::GenerateStaticMembers(io::Printer* p) const {
static const $pbi$::LazyString $default_variable_name$;
)cc");
}
if (is_inlined()) {
// `_init_inline_xxx` is used for initializing default instances.
p->Emit(R"cc(
static std::true_type _init_inline_$name$_;
)cc");
}
}

void SingularString::GenerateAccessorDeclarations(io::Printer* p) const {
Expand Down
23 changes: 22 additions & 1 deletion src/google/protobuf/compiler/cpp/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,27 @@ void FileGenerator::GenerateSourceDefaultInstance(int idx, io::Printer* p) {
)cc");
}

for (int i = 0; i < generator->descriptor()->field_count(); ++i) {
const FieldDescriptor* field = generator->descriptor()->field(i);
if (!IsStringInlined(field, options_)) {
continue;
}

// Force the initialization of the inlined string in the default instance.
p->Emit(
{
{"class", ClassName(generator->descriptor())},
{"field", FieldName(field)},
{"default", DefaultInstanceName(generator->descriptor(), options_)},
{"member", FieldMemberName(field, ShouldSplit(field, options_))},
},
R"cc(
PROTOBUF_ATTRIBUTE_INIT_PRIORITY2 std::true_type
$class$::Impl_::_init_inline_$field$_ =
($default$._instance.$member$.Init(), std::true_type{});
)cc");
}

if (options_.lite_implicit_weak_fields) {
p->Emit(
{
Expand Down Expand Up @@ -1579,7 +1600,7 @@ void FileGenerator::GenerateLibraryIncludes(io::Printer* p) {
IncludeFile("third_party/protobuf/io/coded_stream.h", p);
IncludeFile("third_party/protobuf/arena.h", p);
IncludeFile("third_party/protobuf/arenastring.h", p);
if (IsStringInliningEnabled(file_, options_)) {
if (IsStringInliningEnabled(options_)) {
IncludeFile("third_party/protobuf/inlined_string_field.h", p);
}
if (HasSimpleBaseClasses(file_, options_)) {
Expand Down
6 changes: 2 additions & 4 deletions src/google/protobuf/compiler/cpp/helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -935,10 +935,8 @@ float GetPresenceProbability(const FieldDescriptor* field,
return 1.f;
}

bool IsStringInliningEnabled(const FileDescriptor* file,
const Options& options) {
return GetOptimizeFor(file, options) == FileOptions::SPEED &&
(options.force_inline_string || IsProfileDriven(options));
bool IsStringInliningEnabled(const Options& options) {
return options.force_inline_string || IsProfileDriven(options);
}

bool CanStringBeInlined(const FieldDescriptor* field) {
Expand Down
3 changes: 1 addition & 2 deletions src/google/protobuf/compiler/cpp/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,7 @@ bool IsLikelyPresent(const FieldDescriptor* field, const Options& options);
float GetPresenceProbability(const FieldDescriptor* field,
const Options& options);

bool IsStringInliningEnabled(const FileDescriptor* file,
const Options& options);
bool IsStringInliningEnabled(const Options& options);

// Returns true if the provided field is a singular string and can be inlined.
bool CanStringBeInlined(const FieldDescriptor* field);
Expand Down
30 changes: 15 additions & 15 deletions src/google/protobuf/inlined_string_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,16 @@ namespace internal {
// For more details of the donating states transitions, go/pd-inlined-string.
class PROTOBUF_EXPORT InlinedStringField {
public:
InlinedStringField() : str_() {}
InlinedStringField() { Init(); }
InlinedStringField(const InlinedStringField&) = delete;
InlinedStringField& operator=(const InlinedStringField&) = delete;
inline void Init() { new (get_mutable()) std::string(); }
// Add the dummy parameter just to make InlinedStringField(nullptr)
// unambiguous.
// We have to hide this constructor from builds that do not support it. They
// won't use it in their codegen, but they might include the header.
#if defined(__cpp_lib_constexpr_string) && __cpp_lib_constexpr_string >= 201907L
constexpr InlinedStringField(
const ExplicitlyConstructed<std::string>* /*default_value*/,
bool /*dummy*/) {}
#endif
bool /*dummy*/)
: value_{} {}
explicit InlinedStringField(const std::string& default_value);
explicit InlinedStringField(Arena* arena);
InlinedStringField(Arena* arena, const InlinedStringField& rhs);
Expand Down Expand Up @@ -348,9 +346,7 @@ class PROTOBUF_EXPORT InlinedStringField {
PROTOBUF_NDEBUG_INLINE std::string* get_mutable();
PROTOBUF_NDEBUG_INLINE const std::string* get_const() const;

union {
std::string str_;
};
alignas(std::string) char value_[sizeof(std::string)];

std::string* MutableSlow(::google::protobuf::Arena* arena, bool donated,
uint32_t* donating_states, uint32_t mask,
Expand All @@ -363,14 +359,18 @@ class PROTOBUF_EXPORT InlinedStringField {
typedef void DestructorSkippable_;
};

inline std::string* InlinedStringField::get_mutable() { return &str_; }
inline std::string* InlinedStringField::get_mutable() {
return reinterpret_cast<std::string*>(&value_);
}

inline const std::string* InlinedStringField::get_const() const {
return &str_;
return reinterpret_cast<const std::string*>(&value_);
}

inline InlinedStringField::InlinedStringField(const std::string& default_value)
: str_(default_value) {}
inline InlinedStringField::InlinedStringField(
const std::string& default_value) {
new (get_mutable()) std::string(default_value);
}


#ifdef GOOGLE_PROTOBUF_INTERNAL_DONATE_STEAL_INLINE
Expand All @@ -386,12 +386,12 @@ inline void InternalRegisterArenaDtor(Arena* arena, void* object,
}
#endif // GOOGLE_PROTOBUF_INTERNAL_DONATE_STEAL_INLINE

inline InlinedStringField::InlinedStringField(Arena* /*arena*/) : str_() {}
inline InlinedStringField::InlinedStringField(Arena* /*arena*/) { Init(); }

inline InlinedStringField::InlinedStringField(Arena* arena,
const InlinedStringField& rhs) {
const std::string& src = *rhs.get_const();
::new (static_cast<void*>(&str_)) std::string(src);
new (value_) std::string(src);
}

inline const std::string& InlinedStringField::GetNoArena() const {
Expand Down

0 comments on commit bcd9905

Please sign in to comment.