From 3a3df3057bd3011384eb17fa112fb871506bed89 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Fri, 7 Jul 2023 10:15:36 -0700 Subject: [PATCH] Internal change. PiperOrigin-RevId: 546314630 --- src/google/protobuf/compiler/cpp/helpers.cc | 5 +++++ src/google/protobuf/compiler/cpp/helpers.h | 3 +++ .../compiler/cpp/parse_function_generator.cc | 1 + src/google/protobuf/generated_message_reflection.cc | 1 + src/google/protobuf/generated_message_tctable_gen.cc | 12 +++++++++--- src/google/protobuf/generated_message_tctable_gen.h | 2 ++ 6 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/google/protobuf/compiler/cpp/helpers.cc b/src/google/protobuf/compiler/cpp/helpers.cc index bf7eb2b99a..2c3cf1af93 100644 --- a/src/google/protobuf/compiler/cpp/helpers.cc +++ b/src/google/protobuf/compiler/cpp/helpers.cc @@ -896,6 +896,11 @@ bool IsRarelyPresent(const FieldDescriptor* field, const Options& options) { return false; } +float GetPresenceProbability(const FieldDescriptor* field, + const Options& options) { + return 1.f; +} + bool IsStringInlined(const FieldDescriptor* field, const Options& options) { (void)field; (void)options; diff --git a/src/google/protobuf/compiler/cpp/helpers.h b/src/google/protobuf/compiler/cpp/helpers.h index 44701d3a3f..c88c12a479 100644 --- a/src/google/protobuf/compiler/cpp/helpers.h +++ b/src/google/protobuf/compiler/cpp/helpers.h @@ -367,6 +367,9 @@ bool IsProfileDriven(const Options& options); // Returns true if `field` is unlikely to be present based on PDProto profile. bool IsRarelyPresent(const FieldDescriptor* field, const Options& options); +float GetPresenceProbability(const FieldDescriptor* field, + const Options& options); + // Returns true if `field` should be inlined based on PDProto profile. bool IsStringInlined(const FieldDescriptor* field, const Options& options); diff --git a/src/google/protobuf/compiler/cpp/parse_function_generator.cc b/src/google/protobuf/compiler/cpp/parse_function_generator.cc index 86c114a67c..a4c001a476 100644 --- a/src/google/protobuf/compiler/cpp/parse_function_generator.cc +++ b/src/google/protobuf/compiler/cpp/parse_function_generator.cc @@ -114,6 +114,7 @@ class ParseFunctionGenerator::GeneratedOptionProvider final return internal::field_layout::TransformValidation{}; }; return { + GetPresenceProbability(field, gen_->options_), verify_flag(), IsStringInlined(field, gen_->options_), IsImplicitWeakField(field, gen_->options_, gen_->scc_analyzer_), diff --git a/src/google/protobuf/generated_message_reflection.cc b/src/google/protobuf/generated_message_reflection.cc index 4af33a0a08..4705538f80 100644 --- a/src/google/protobuf/generated_message_reflection.cc +++ b/src/google/protobuf/generated_message_reflection.cc @@ -3391,6 +3391,7 @@ const internal::TcParseTableBase* Reflection::CreateTcParseTable() const { return internal::field_layout::TransformValidation{}; }; return { + 1.f, // All fields are assumed present. verify_flag(), // ref_.IsInlined(field), // diff --git a/src/google/protobuf/generated_message_tctable_gen.cc b/src/google/protobuf/generated_message_tctable_gen.cc index 72c5784737..086825e258 100644 --- a/src/google/protobuf/generated_message_tctable_gen.cc +++ b/src/google/protobuf/generated_message_tctable_gen.cc @@ -391,12 +391,18 @@ std::vector SplitFastFieldsForSize( TailCallTableInfo::FastFieldInfo& info = result[fast_idx]; if (!info.func_name.empty()) { - // This field entry is already filled. - continue; + // Null field means END_GROUP which is guaranteed to be present. + if (info.field == nullptr) continue; + + // This field entry is already filled. Skip if previous entry is more + // likely present. + const auto prev_options = option_provider.GetForField(info.field); + if (prev_options.presence_probability >= options.presence_probability) { + continue; + } } // Fill in this field's entry: - ABSL_CHECK(info.func_name.empty()) << info.func_name; PopulateFastFieldEntry(entry, options, info); info.field = field; info.coded_tag = tag; diff --git a/src/google/protobuf/generated_message_tctable_gen.h b/src/google/protobuf/generated_message_tctable_gen.h index 9fcd3c9126..d37a97d459 100644 --- a/src/google/protobuf/generated_message_tctable_gen.h +++ b/src/google/protobuf/generated_message_tctable_gen.h @@ -57,6 +57,8 @@ enum TransformValidation : uint16_t; // Helper class for generating tailcall parsing functions. struct PROTOBUF_EXPORT TailCallTableInfo { struct PerFieldOptions { + // For presence awareness (e.g. PDProto). + float presence_probability; // kTvEager, kTvLazy, or 0 field_layout::TransformValidation lazy_opt; bool is_string_inlined;