Move ByteSizeLong calculation of packed varint fields out of line.

This reduces code size.

PiperOrigin-RevId: 653700178
pull/17495/head
Protobuf Team Bot 7 months ago committed by Copybara-Service
parent bf8a9fc73f
commit 4ea1f2024a
  1. 41
      src/google/protobuf/compiler/cpp/field_generators/enum_field.cc
  2. 35
      src/google/protobuf/compiler/cpp/field_generators/primitive_field.cc
  3. 67
      src/google/protobuf/descriptor.pb.cc
  4. 86
      src/google/protobuf/wire_format_lite.cc
  5. 22
      src/google/protobuf/wire_format_lite.h

@ -518,41 +518,38 @@ void RepeatedEnum::GenerateSerializeWithCachedSizesToArray(
} }
void RepeatedEnum::GenerateByteSize(io::Printer* p) const { void RepeatedEnum::GenerateByteSize(io::Printer* p) const {
p->Emit( if (has_cached_size_) {
{ ABSL_CHECK(field_->is_packed());
{"add_to_size",
[&] {
if (!field_->is_packed()) {
p->Emit(R"cc( p->Emit(R"cc(
total_size += std::size_t{$kTagBytes$} * count; total_size += ::_pbi::WireFormatLite::EnumSizeWithPackedTagSize(
this_._internal_$name$(), $kTagBytes$, this_.$cached_size_$);
)cc"); )cc");
return; return;
} }
p->Emit(
{
{"tag_size",
[&] {
if (field_->is_packed()) {
p->Emit(R"cc( p->Emit(R"cc(
if (data_size > 0) { data_size == 0
total_size += $kTagBytes$; ? 0
total_size += ::_pbi::WireFormatLite::Int32Size( : $kTagBytes$ + ::_pbi::WireFormatLite::Int32Size(
static_cast<int32_t>(data_size)); static_cast<int32_t>(data_size));
}
)cc"); )cc");
if (has_cached_size_) { } else {
p->Emit(R"cc( p->Emit(R"cc(
this_.$cached_size_$.Set(::_pbi::ToCachedSize(data_size)); std::size_t{$kTagBytes$} *
::_pbi::FromIntSize(this_._internal_$name$_size());
)cc"); )cc");
} }
}}, }},
}, },
R"cc( R"cc(
std::size_t data_size = 0; std::size_t data_size =
auto count = static_cast<std::size_t>(this_._internal_$name$_size()); ::_pbi::WireFormatLite::EnumSize(this_._internal_$name$());
std::size_t tag_size = $tag_size$;
for (std::size_t i = 0; i < count; ++i) { total_size += data_size + tag_size;
data_size += ::_pbi::WireFormatLite::EnumSize(
this_._internal_$name$().Get(static_cast<int>(i)));
}
total_size += data_size;
$add_to_size$;
)cc"); )cc");
} }
} // namespace } // namespace

@ -590,41 +590,42 @@ void RepeatedPrimitive::GenerateSerializeWithCachedSizesToArray(
} }
void RepeatedPrimitive::GenerateByteSize(io::Printer* p) const { void RepeatedPrimitive::GenerateByteSize(io::Printer* p) const {
if (HasCachedSize()) {
ABSL_CHECK(field_->is_packed());
p->Emit(
R"cc(
total_size +=
::_pbi::WireFormatLite::$DeclaredType$SizeWithPackedTagSize(
this_._internal_$name$(), $kTagBytes$,
this_.$_field_cached_byte_size_$);
)cc");
return;
}
p->Emit( p->Emit(
{ {
Sub{"data_size", {"data_size",
[&] { [&] {
auto fixed_size = FixedSize(field_->type()); auto fixed_size = FixedSize(field_->type());
if (fixed_size.has_value()) { if (fixed_size.has_value()) {
p->Emit({{"kFixed", *fixed_size}}, R"cc( p->Emit({{"kFixed", *fixed_size}}, R"cc(
std::size_t{$kFixed$} * std::size_t{$kFixed$} *
::_pbi::FromIntSize(this_._internal_$name$_size()) ::_pbi::FromIntSize(this_._internal_$name$_size());
)cc"); )cc");
} else { } else {
p->Emit(R"cc( p->Emit(R"cc(
::_pbi::WireFormatLite::$DeclaredType$Size( ::_pbi::WireFormatLite::$DeclaredType$Size(
this_._internal_$name$()) this_._internal_$name$());
)cc"); )cc");
} }
}} // Here and below, we need to disable the default ;-chomping
// that closure substitutions do.
.WithSuffix(""),
{"maybe_cache_data_size",
[&] {
if (!HasCachedSize()) return;
p->Emit(R"cc(
this_.$_field_cached_byte_size_$.Set(
::_pbi::ToCachedSize(data_size));
)cc");
}}, }},
Sub{"tag_size", {"tag_size",
[&] { [&] {
if (field_->is_packed()) { if (field_->is_packed()) {
p->Emit(R"cc( p->Emit(R"cc(
data_size == 0 data_size == 0
? 0 ? 0
: $kTagBytes$ + ::_pbi::WireFormatLite::Int32Size( : $kTagBytes$ + ::_pbi::WireFormatLite::Int32Size(
static_cast<int32_t>(data_size)) static_cast<int32_t>(data_size));
)cc"); )cc");
} else { } else {
p->Emit(R"cc( p->Emit(R"cc(
@ -632,12 +633,10 @@ void RepeatedPrimitive::GenerateByteSize(io::Printer* p) const {
::_pbi::FromIntSize(this_._internal_$name$_size()); ::_pbi::FromIntSize(this_._internal_$name$_size());
)cc"); )cc");
} }
}} }},
.WithSuffix(""),
}, },
R"cc( R"cc(
std::size_t data_size = $data_size$; std::size_t data_size = $data_size$;
$maybe_cache_data_size$;
std::size_t tag_size = $tag_size$; std::size_t tag_size = $tag_size$;
total_size += tag_size + data_size; total_size += tag_size + data_size;
)cc"); )cc");

@ -3304,21 +3304,17 @@ PROTOBUF_NOINLINE void FileDescriptorProto::Clear() {
// repeated int32 public_dependency = 10; // repeated int32 public_dependency = 10;
{ {
std::size_t data_size = ::_pbi::WireFormatLite::Int32Size( std::size_t data_size = ::_pbi::WireFormatLite::Int32Size(
this_._internal_public_dependency()) this_._internal_public_dependency());
;
std::size_t tag_size = std::size_t{1} * std::size_t tag_size = std::size_t{1} *
::_pbi::FromIntSize(this_._internal_public_dependency_size()); ::_pbi::FromIntSize(this_._internal_public_dependency_size());
;
total_size += tag_size + data_size; total_size += tag_size + data_size;
} }
// repeated int32 weak_dependency = 11; // repeated int32 weak_dependency = 11;
{ {
std::size_t data_size = ::_pbi::WireFormatLite::Int32Size( std::size_t data_size = ::_pbi::WireFormatLite::Int32Size(
this_._internal_weak_dependency()) this_._internal_weak_dependency());
;
std::size_t tag_size = std::size_t{1} * std::size_t tag_size = std::size_t{1} *
::_pbi::FromIntSize(this_._internal_weak_dependency_size()); ::_pbi::FromIntSize(this_._internal_weak_dependency_size());
;
total_size += tag_size + data_size; total_size += tag_size + data_size;
} }
} }
@ -10112,15 +10108,11 @@ PROTOBUF_NOINLINE void FieldOptions::Clear() {
{ {
// repeated .google.protobuf.FieldOptions.OptionTargetType targets = 19; // repeated .google.protobuf.FieldOptions.OptionTargetType targets = 19;
{ {
std::size_t data_size = 0; std::size_t data_size =
auto count = static_cast<std::size_t>(this_._internal_targets_size()); ::_pbi::WireFormatLite::EnumSize(this_._internal_targets());
std::size_t tag_size = std::size_t{2} *
for (std::size_t i = 0; i < count; ++i) { ::_pbi::FromIntSize(this_._internal_targets_size());
data_size += ::_pbi::WireFormatLite::EnumSize( total_size += data_size + tag_size;
this_._internal_targets().Get(static_cast<int>(i)));
}
total_size += data_size;
total_size += std::size_t{2} * count;
} }
// repeated .google.protobuf.FieldOptions.EditionDefault edition_defaults = 20; // repeated .google.protobuf.FieldOptions.EditionDefault edition_defaults = 20;
{ {
@ -14098,31 +14090,17 @@ PROTOBUF_NOINLINE void SourceCodeInfo_Location::Clear() {
{ {
// repeated int32 path = 1 [packed = true]; // repeated int32 path = 1 [packed = true];
{ {
std::size_t data_size = ::_pbi::WireFormatLite::Int32Size( total_size +=
this_._internal_path()) ::_pbi::WireFormatLite::Int32SizeWithPackedTagSize(
; this_._internal_path(), 1,
this_._impl_._path_cached_byte_size_.Set( this_._impl_._path_cached_byte_size_);
::_pbi::ToCachedSize(data_size));
std::size_t tag_size = data_size == 0
? 0
: 1 + ::_pbi::WireFormatLite::Int32Size(
static_cast<int32_t>(data_size))
;
total_size += tag_size + data_size;
} }
// repeated int32 span = 2 [packed = true]; // repeated int32 span = 2 [packed = true];
{ {
std::size_t data_size = ::_pbi::WireFormatLite::Int32Size( total_size +=
this_._internal_span()) ::_pbi::WireFormatLite::Int32SizeWithPackedTagSize(
; this_._internal_span(), 1,
this_._impl_._span_cached_byte_size_.Set( this_._impl_._span_cached_byte_size_);
::_pbi::ToCachedSize(data_size));
std::size_t tag_size = data_size == 0
? 0
: 1 + ::_pbi::WireFormatLite::Int32Size(
static_cast<int32_t>(data_size))
;
total_size += tag_size + data_size;
} }
// repeated string leading_detached_comments = 6; // repeated string leading_detached_comments = 6;
{ {
@ -14692,17 +14670,10 @@ PROTOBUF_NOINLINE void GeneratedCodeInfo_Annotation::Clear() {
{ {
// repeated int32 path = 1 [packed = true]; // repeated int32 path = 1 [packed = true];
{ {
std::size_t data_size = ::_pbi::WireFormatLite::Int32Size( total_size +=
this_._internal_path()) ::_pbi::WireFormatLite::Int32SizeWithPackedTagSize(
; this_._internal_path(), 1,
this_._impl_._path_cached_byte_size_.Set( this_._impl_._path_cached_byte_size_);
::_pbi::ToCachedSize(data_size));
std::size_t tag_size = data_size == 0
? 0
: 1 + ::_pbi::WireFormatLite::Int32Size(
static_cast<int32_t>(data_size))
;
total_size += tag_size + data_size;
} }
} }
cached_has_bits = this_._impl_._has_bits_[0]; cached_has_bits = this_._impl_._has_bits_[0];

@ -21,6 +21,7 @@
#include "absl/strings/cord.h" #include "absl/strings/cord.h"
#include "absl/strings/str_cat.h" #include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h" #include "absl/strings/str_format.h"
#include "google/protobuf/message_lite.h"
#include "utf8_validity.h" #include "utf8_validity.h"
@ -830,6 +831,91 @@ size_t WireFormatLite::SInt64Size(const RepeatedField<int64_t>& value) {
#endif #endif
size_t WireFormatLite::Int32SizeWithPackedTagSize(
const RepeatedField<int32_t>& value, size_t tag_size,
const internal::CachedSize& cached_size) {
if (value.empty()) {
cached_size.Set(0);
return 0;
}
size_t res;
PROTOBUF_ALWAYS_INLINE_CALL res = Int32Size(value);
cached_size.Set(ToCachedSize(res));
return tag_size + res + Int32Size(static_cast<int32_t>(res));
}
size_t WireFormatLite::Int64SizeWithPackedTagSize(
const RepeatedField<int64_t>& value, size_t tag_size,
const internal::CachedSize& cached_size) {
if (value.empty()) {
cached_size.Set(0);
return 0;
}
size_t res;
PROTOBUF_ALWAYS_INLINE_CALL res = Int64Size(value);
cached_size.Set(ToCachedSize(res));
return tag_size + res + Int32Size(static_cast<int32_t>(res));
}
size_t WireFormatLite::UInt32SizeWithPackedTagSize(
const RepeatedField<uint32_t>& value, size_t tag_size,
const internal::CachedSize& cached_size) {
if (value.empty()) {
cached_size.Set(0);
return 0;
}
size_t res;
PROTOBUF_ALWAYS_INLINE_CALL res = UInt32Size(value);
cached_size.Set(ToCachedSize(res));
return tag_size + res + Int32Size(static_cast<int32_t>(res));
}
size_t WireFormatLite::UInt64SizeWithPackedTagSize(
const RepeatedField<uint64_t>& value, size_t tag_size,
const internal::CachedSize& cached_size) {
if (value.empty()) {
cached_size.Set(0);
return 0;
}
size_t res;
PROTOBUF_ALWAYS_INLINE_CALL res = UInt64Size(value);
cached_size.Set(ToCachedSize(res));
return tag_size + res + Int32Size(static_cast<int32_t>(res));
}
size_t WireFormatLite::SInt32SizeWithPackedTagSize(
const RepeatedField<int32_t>& value, size_t tag_size,
const internal::CachedSize& cached_size) {
if (value.empty()) {
cached_size.Set(0);
return 0;
}
size_t res;
PROTOBUF_ALWAYS_INLINE_CALL res = SInt32Size(value);
cached_size.Set(ToCachedSize(res));
return tag_size + res + Int32Size(static_cast<int32_t>(res));
}
size_t WireFormatLite::SInt64SizeWithPackedTagSize(
const RepeatedField<int64_t>& value, size_t tag_size,
const internal::CachedSize& cached_size) {
if (value.empty()) {
cached_size.Set(0);
return 0;
}
size_t res;
PROTOBUF_ALWAYS_INLINE_CALL res = SInt64Size(value);
cached_size.Set(ToCachedSize(res));
return tag_size + res + Int32Size(static_cast<int32_t>(res));
}
size_t WireFormatLite::EnumSizeWithPackedTagSize(
const RepeatedField<int>& value, size_t tag_size,
const internal::CachedSize& cached_size) {
if (value.empty()) {
cached_size.Set(0);
return 0;
}
size_t res;
PROTOBUF_ALWAYS_INLINE_CALL res = EnumSize(value);
cached_size.Set(ToCachedSize(res));
return tag_size + res + Int32Size(static_cast<int32_t>(res));
}
} // namespace internal } // namespace internal
} // namespace protobuf } // namespace protobuf
} // namespace google } // namespace google

@ -700,6 +700,28 @@ class PROTOBUF_EXPORT WireFormatLite {
static size_t SInt64Size(const RepeatedField<int64_t>& value); static size_t SInt64Size(const RepeatedField<int64_t>& value);
static size_t EnumSize(const RepeatedField<int>& value); static size_t EnumSize(const RepeatedField<int>& value);
static size_t Int32SizeWithPackedTagSize(
const RepeatedField<int32_t>& value, size_t tag_size,
const internal::CachedSize& cached_size);
static size_t Int64SizeWithPackedTagSize(
const RepeatedField<int64_t>& value, size_t tag_size,
const internal::CachedSize& cached_size);
static size_t UInt32SizeWithPackedTagSize(
const RepeatedField<uint32_t>& value, size_t tag_size,
const internal::CachedSize& cached_size);
static size_t UInt64SizeWithPackedTagSize(
const RepeatedField<uint64_t>& value, size_t tag_size,
const internal::CachedSize& cached_size);
static size_t SInt32SizeWithPackedTagSize(
const RepeatedField<int32_t>& value, size_t tag_size,
const internal::CachedSize& cached_size);
static size_t SInt64SizeWithPackedTagSize(
const RepeatedField<int64_t>& value, size_t tag_size,
const internal::CachedSize& cached_size);
static size_t EnumSizeWithPackedTagSize(
const RepeatedField<int>& value, size_t tag_size,
const internal::CachedSize& cached_size);
// These types always have the same size. // These types always have the same size.
static constexpr size_t kFixed32Size = 4; static constexpr size_t kFixed32Size = 4;
static constexpr size_t kFixed64Size = 8; static constexpr size_t kFixed64Size = 8;

Loading…
Cancel
Save