diff --git a/common/BUILD b/common/BUILD index f5ca6251f..77c8acbf7 100644 --- a/common/BUILD +++ b/common/BUILD @@ -789,6 +789,7 @@ cc_library( "@com_google_absl//absl/base:no_destructor", "@com_google_absl//absl/base:nullability", "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/functional:any_invocable", "@com_google_absl//absl/functional:function_ref", "@com_google_absl//absl/functional:overload", "@com_google_absl//absl/hash", diff --git a/common/type_reflector.h b/common/type_reflector.h index 8378ed36c..b7bad4016 100644 --- a/common/type_reflector.h +++ b/common/type_reflector.h @@ -15,7 +15,11 @@ #ifndef THIRD_PARTY_CEL_CPP_COMMON_TYPE_REFLECTOR_H_ #define THIRD_PARTY_CEL_CPP_COMMON_TYPE_REFLECTOR_H_ +#include + +#include "absl/base/attributes.h" #include "absl/base/nullability.h" +#include "absl/functional/any_invocable.h" #include "absl/status/statusor.h" #include "absl/strings/string_view.h" #include "common/type_introspector.h" @@ -36,6 +40,37 @@ class TypeReflector : public virtual TypeIntrospector { absl::string_view name, google::protobuf::MessageFactory* absl_nonnull message_factory, google::protobuf::Arena* absl_nonnull arena) const = 0; + + // Creates new `ValueBuilder` instances for a type that was resolved ahead of + // time (see `NewValueBuilderFactory`). + using ValueBuilderFactory = + absl::AnyInvocable( + google::protobuf::MessageFactory* absl_nonnull, google::protobuf::Arena* absl_nonnull) + const>; + + // `NewValueBuilderFactory` returns a factory for creating new `ValueBuilder` + // instances for the corresponding type `name`. + // + // This is used to eagerly resolve the dependencies for creating value + // builders (e.g. descriptor and prototype lookups) at plan time, instead of + // repeating them every time a struct is created. `message_factory` is the + // message factory that is expected to be used at evaluation time; + // implementations must fall back to the regular lookup if the factory + // passed at evaluation time is different. + // + // The returned factory must not outlive this `TypeReflector`. + virtual ValueBuilderFactory NewValueBuilderFactory( + absl::string_view name, + google::protobuf::MessageFactory* absl_nonnull message_factory) const + ABSL_ATTRIBUTE_LIFETIME_BOUND { + static_cast(message_factory); + return [this, name = std::string(name)]( + google::protobuf::MessageFactory* absl_nonnull message_factory, + google::protobuf::Arena* absl_nonnull arena) + -> absl::StatusOr { + return NewValueBuilder(name, message_factory, arena); + }; + } }; } // namespace cel diff --git a/common/values/struct_value_builder.cc b/common/values/struct_value_builder.cc index 1cb117f46..d88dabe43 100644 --- a/common/values/struct_value_builder.cc +++ b/common/values/struct_value_builder.cc @@ -1560,6 +1560,16 @@ class StructValueBuilderImpl final : public StructValueBuilder { } // namespace +absl_nullable cel::ValueBuilderPtr NewValueBuilder( + Allocator<> allocator, + const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, + google::protobuf::MessageFactory* absl_nonnull message_factory, + const google::protobuf::Message* absl_nonnull prototype) { + return std::make_unique(allocator.arena(), descriptor_pool, + message_factory, + prototype->New(allocator.arena())); +} + absl_nullable cel::ValueBuilderPtr NewValueBuilder( Allocator<> allocator, const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, @@ -1580,9 +1590,8 @@ absl_nullable cel::ValueBuilderPtr NewValueBuilder( if (ABSL_PREDICT_FALSE(prototype == nullptr)) { return nullptr; } - return std::make_unique(allocator.arena(), descriptor_pool, - message_factory, - prototype->New(allocator.arena())); + return NewValueBuilder(allocator, descriptor_pool, message_factory, + prototype); } absl_nullable cel::StructValueBuilderPtr NewStructValueBuilder( diff --git a/common/values/value_builder.h b/common/values/value_builder.h index 685b13dd8..38fd6b1a8 100644 --- a/common/values/value_builder.h +++ b/common/values/value_builder.h @@ -31,6 +31,14 @@ absl_nullable cel::ValueBuilderPtr NewValueBuilder( google::protobuf::MessageFactory* absl_nonnull message_factory, absl::string_view name); +// Like above, but uses an already resolved `prototype` (obtained from +// `message_factory`) instead of looking up the type by name. +absl_nullable cel::ValueBuilderPtr NewValueBuilder( + Allocator<> allocator, + const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, + google::protobuf::MessageFactory* absl_nonnull message_factory, + const google::protobuf::Message* absl_nonnull prototype); + } // namespace cel::common_internal #endif // THIRD_PARTY_CEL_CPP_COMMON_VALUES_VALUE_BUILDER_H_ diff --git a/conformance/service.cc b/conformance/service.cc index 468eee980..28a44d2fe 100644 --- a/conformance/service.cc +++ b/conformance/service.cc @@ -295,6 +295,7 @@ class LegacyConformanceServiceImpl : public ConformanceServiceInterface { options.constant_folding = true; options.constant_arena = constant_arena; options.enable_typed_field_access = true; + options.enable_eager_struct_resolution = true; } if (extended_optimizations) { @@ -506,6 +507,7 @@ class ModernConformanceServiceImpl : public ConformanceServiceInterface { options.container = std::string(container); if (enable_optimizations_) { options.enable_typed_field_access = true; + options.enable_eager_struct_resolution = true; } CEL_ASSIGN_OR_RETURN( auto builder, CreateStandardRuntimeBuilder( diff --git a/eval/compiler/flat_expr_builder.cc b/eval/compiler/flat_expr_builder.cc index 3626937e9..e24952457 100644 --- a/eval/compiler/flat_expr_builder.cc +++ b/eval/compiler/flat_expr_builder.cc @@ -58,6 +58,7 @@ #include "common/expr.h" #include "common/kind.h" #include "common/type.h" +#include "common/type_reflector.h" #include "common/value.h" #include "eval/compiler/check_ast_extensions.h" #include "eval/compiler/flat_expr_builder_extensions.h" @@ -1400,8 +1401,16 @@ class FlatExprVisitor : public cel::AstVisitor { num_fields <= static_cast(std::numeric_limits::max()) ? 1 - static_cast(num_fields) : std::numeric_limits::max(); - AddStep(CreateCreateStructStep(std::move(resolved_name), std::move(fields), - MakeOptionalIndicesSet(struct_expr)), + cel::TypeReflector::ValueBuilderFactory builder_factory; + if (options_.enable_eager_struct_resolution) { + // Resolve the descriptor and prototype once at plan time instead of on + // every evaluation. + builder_factory = type_provider_.NewValueBuilderFactory( + resolved_name, extension_context_.MutableMessageFactory()); + } + AddStep(CreateCreateStructStep( + std::move(resolved_name), std::move(builder_factory), + std::move(fields), MakeOptionalIndicesSet(struct_expr)), expr.id(), stack_delta); } diff --git a/eval/eval/BUILD b/eval/eval/BUILD index c19252d02..f4c175126 100644 --- a/eval/eval/BUILD +++ b/eval/eval/BUILD @@ -346,6 +346,7 @@ cc_library( "//common:value", "//common/internal:attribute_trail", "//internal:status_macros", + "@com_google_absl//absl/base:nullability", "@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", diff --git a/eval/eval/create_struct_step.cc b/eval/eval/create_struct_step.cc index c7ac0f22e..7911987e7 100644 --- a/eval/eval/create_struct_step.cc +++ b/eval/eval/create_struct_step.cc @@ -20,6 +20,7 @@ #include #include +#include "absl/base/nullability.h" #include "absl/container/flat_hash_set.h" #include "absl/status/status.h" #include "absl/status/statusor.h" @@ -27,6 +28,7 @@ #include "absl/strings/string_view.h" #include "absl/types/optional.h" #include "common/internal/attribute_trail.h" +#include "common/type_reflector.h" #include "common/value.h" #include "eval/eval/evaluator_core.h" #include "eval/eval/expression_step_base.h" @@ -44,10 +46,13 @@ using ::cel::Value; // `CreateStruct` implementation for message/struct. class CreateStructStepForStruct final : public ExpressionStepBase { public: - CreateStructStepForStruct(std::string name, std::vector entries, - absl::flat_hash_set optional_indices) + CreateStructStepForStruct( + std::string name, cel::TypeReflector::ValueBuilderFactory builder_factory, + std::vector entries, + absl::flat_hash_set optional_indices) : ExpressionStepBase(), name_(std::move(name)), + builder_factory_(std::move(builder_factory)), entries_(std::move(entries)), optional_indices_(std::move(optional_indices)) {} @@ -56,7 +61,19 @@ class CreateStructStepForStruct final : public ExpressionStepBase { private: absl::StatusOr DoEvaluate(ExecutionFrame* frame) const; + absl::StatusOr NewValueBuilder( + ExecutionFrame* frame) const { + if (builder_factory_) { + return builder_factory_(frame->message_factory(), frame->arena()); + } + return frame->type_provider().NewValueBuilder( + name_, frame->message_factory(), frame->arena()); + } + std::string name_; + // Resolved at plan time, if eager struct resolution is enabled. Otherwise + // empty, and the type is looked up by name on every evaluation. + cel::TypeReflector::ValueBuilderFactory builder_factory_; std::vector entries_; absl::flat_hash_set optional_indices_; }; @@ -82,9 +99,7 @@ absl::StatusOr CreateStructStepForStruct::DoEvaluate( } } - CEL_ASSIGN_OR_RETURN(auto builder, - frame->type_provider().NewValueBuilder( - name_, frame->message_factory(), frame->arena())); + CEL_ASSIGN_OR_RETURN(auto builder, NewValueBuilder(frame)); if (builder == nullptr) { return ErrorValue::From( absl::NotFoundError(absl::StrCat("Unable to find builder: ", name_)), @@ -148,6 +163,16 @@ std::unique_ptr CreateCreateStructStep( absl::flat_hash_set optional_indices) { // MakeOptionalIndicesSet(create_struct_expr) return std::make_unique( - std::move(name), std::move(field_keys), std::move(optional_indices)); + std::move(name), /*builder_factory=*/nullptr, std::move(field_keys), + std::move(optional_indices)); +} + +std::unique_ptr CreateCreateStructStep( + std::string name, cel::TypeReflector::ValueBuilderFactory builder_factory, + std::vector field_keys, + absl::flat_hash_set optional_indices) { + return std::make_unique( + std::move(name), std::move(builder_factory), std::move(field_keys), + std::move(optional_indices)); } } // namespace google::api::expr::runtime diff --git a/eval/eval/create_struct_step.h b/eval/eval/create_struct_step.h index 1dd42273a..56676ad30 100644 --- a/eval/eval/create_struct_step.h +++ b/eval/eval/create_struct_step.h @@ -21,6 +21,7 @@ #include #include "absl/container/flat_hash_set.h" +#include "common/type_reflector.h" #include "eval/eval/expression_step_logic.h" namespace google::api::expr::runtime { @@ -31,6 +32,14 @@ std::unique_ptr CreateCreateStructStep( std::string name, std::vector field_keys, absl::flat_hash_set optional_indices); +// Like above, but creates the value builder with `builder_factory` (resolved at +// plan time) instead of looking up the type `name` on every evaluation. If +// `builder_factory` is empty, this is equivalent to the overload above. +std::unique_ptr CreateCreateStructStep( + std::string name, cel::TypeReflector::ValueBuilderFactory builder_factory, + std::vector field_keys, + absl::flat_hash_set optional_indices); + } // namespace google::api::expr::runtime #endif // THIRD_PARTY_CEL_CPP_EVAL_EVAL_CREATE_STRUCT_STEP_H_ diff --git a/eval/public/cel_options.cc b/eval/public/cel_options.cc index c3aef0167..4326d1b30 100644 --- a/eval/public/cel_options.cc +++ b/eval/public/cel_options.cc @@ -46,6 +46,7 @@ cel::RuntimeOptions ConvertToRuntimeOptions(const InterpreterOptions& options) { options.enable_precision_preserving_double_format, options.enable_typed_field_access, options.enable_subexpression_inlining, + options.enable_eager_struct_resolution, }; } diff --git a/eval/public/cel_options.h b/eval/public/cel_options.h index 0bb6d81ed..f963f272a 100644 --- a/eval/public/cel_options.h +++ b/eval/public/cel_options.h @@ -232,6 +232,15 @@ struct InterpreterOptions { // // Note: Enabling this option may change observed expression tracing behavior. bool enable_subexpression_inlining = false; + + // When enabled, the planner resolves the message type of struct creation + // expressions (descriptor and prototype lookups) at plan time, instead of + // looking up the type by name every time the expression is evaluated. + // + // Only takes effect for a `cel::Runtime` configured with these options (see + // `ConvertToRuntimeOptions`). Expressions created with `CelExpressionBuilder` + // still look up the type on every evaluation. + bool enable_eager_struct_resolution = false; }; // LINT.ThenChange(//depot/google3/runtime/runtime_options.h) diff --git a/eval/tests/modern_benchmark_test.cc b/eval/tests/modern_benchmark_test.cc index cdbb2f3f1..d0880901f 100644 --- a/eval/tests/modern_benchmark_test.cc +++ b/eval/tests/modern_benchmark_test.cc @@ -987,6 +987,53 @@ void BM_ProtoListAccess(benchmark::State& state) { BENCHMARK(BM_ProtoListAccess); +// Evaluates the struct creation expression `expr`. The benchmark argument +// toggles `RuntimeOptions::enable_eager_struct_resolution`. +void RunCreateStructBenchmark( + benchmark::State& state, + const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, + absl::string_view expr) { + RuntimeOptions options = GetOptions(); + options.enable_eager_struct_resolution = state.range(0) != 0; + auto runtime = StandardRuntimeOrDie(options, descriptor_pool); + + ASSERT_OK_AND_ASSIGN(auto builder, NewCompilerBuilder(descriptor_pool)); + ASSERT_THAT(builder->AddLibrary(StandardCompilerLibrary()), IsOk()); + ASSERT_OK_AND_ASSIGN(auto compiler, builder->Build()); + ASSERT_OK_AND_ASSIGN(ValidationResult validation_result, + compiler->Compile(expr)); + ASSERT_TRUE(validation_result.IsValid()); + ASSERT_OK_AND_ASSIGN(auto ast, validation_result.ReleaseAst()); + + ASSERT_OK_AND_ASSIGN(auto cel_expr, runtime->CreateProgram(std::move(ast))); + + for (auto _ : state) { + google::protobuf::Arena arena; + Activation activation; + ASSERT_OK_AND_ASSIGN(cel::Value result, + cel_expr->Evaluate(&arena, activation)); + ASSERT_TRUE(result.IsParsedMessage()); + } +} + +void BM_CreateGeneratedMessage(benchmark::State& state) { + RunCreateStructBenchmark(state, google::protobuf::DescriptorPool::generated_pool(), + R"cel( + google.api.expr.runtime.RequestContext{ip: '192.168.0.1', path: '/root'} + )cel"); +} + +BENCHMARK(BM_CreateGeneratedMessage)->Arg(0)->Arg(1); + +void BM_CreateDynamicMessage(benchmark::State& state) { + RunCreateStructBenchmark(state, internal::GetTestingDescriptorPool(), + R"cel( + cel.expr.conformance.proto3.TestAllTypes.NestedMessage{bb: 1} + )cel"); +} + +BENCHMARK(BM_CreateDynamicMessage)->Arg(0)->Arg(1); + // This expression has no equivalent CEL expression. // Sum a square with a nested comprehension constexpr char kNestedListSum[] = R"( diff --git a/runtime/internal/BUILD b/runtime/internal/BUILD index 0380de236..8ee397d1c 100644 --- a/runtime/internal/BUILD +++ b/runtime/internal/BUILD @@ -216,6 +216,7 @@ cc_library( "//common:descriptor_pool_type_introspector", "//common:type", "//common:value", + "@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/base:nullability", "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/status", @@ -226,3 +227,18 @@ cc_library( "@com_google_protobuf//:protobuf", ], ) + +cc_test( + name = "runtime_type_provider_test", + srcs = ["runtime_type_provider_test.cc"], + deps = [ + ":runtime_type_provider", + "//common:value", + "//internal:testing", + "//internal:testing_descriptor_pool", + "@cel_conformance//proto/cel/expr/conformance/proto3:test_all_types_cc_proto", + "@com_google_absl//absl/status:status_matchers", + "@com_google_absl//absl/status:statusor", + "@com_google_protobuf//:protobuf", + ], +) diff --git a/runtime/internal/runtime_type_provider.cc b/runtime/internal/runtime_type_provider.cc index 5314918bf..af6617954 100644 --- a/runtime/internal/runtime_type_provider.cc +++ b/runtime/internal/runtime_type_provider.cc @@ -15,9 +15,11 @@ #include "runtime/internal/runtime_type_provider.h" #include +#include #include #include "absl/base/nullability.h" +#include "absl/base/optimization.h" #include "absl/container/flat_hash_map.h" #include "absl/status/status.h" #include "absl/status/statusor.h" @@ -26,9 +28,11 @@ #include "absl/types/optional.h" #include "common/type.h" #include "common/type_introspector.h" +#include "common/type_reflector.h" #include "common/value.h" #include "common/values/value_builder.h" #include "google/protobuf/arena.h" +#include "google/protobuf/descriptor.h" #include "google/protobuf/message.h" namespace cel::runtime_internal { @@ -93,4 +97,31 @@ RuntimeTypeProvider::NewValueBuilder( message_factory, name); } +TypeReflector::ValueBuilderFactory RuntimeTypeProvider::NewValueBuilderFactory( + absl::string_view name, + google::protobuf::MessageFactory* absl_nonnull message_factory) const { + const google::protobuf::Message* absl_nullable prototype = nullptr; + if (const google::protobuf::Descriptor* absl_nullable descriptor = + descriptor_pool_->FindMessageTypeByName(name); + descriptor != nullptr) { + prototype = message_factory->GetPrototype(descriptor); + } + if (prototype == nullptr) { + // Not a message type known to the descriptor pool, use the generic lookup. + return TypeReflector::NewValueBuilderFactory(name, message_factory); + } + return [this, name = std::string(name), message_factory, prototype]( + google::protobuf::MessageFactory* absl_nonnull runtime_message_factory, + google::protobuf::Arena* absl_nonnull arena) + -> absl::StatusOr { + if (ABSL_PREDICT_TRUE(runtime_message_factory == message_factory)) { + return common_internal::NewValueBuilder(arena, descriptor_pool_, + message_factory, prototype); + } + // The prototype is owned by (and depends on) `message_factory`; use the + // message factory requested for this evaluation instead. + return NewValueBuilder(name, runtime_message_factory, arena); + }; +} + } // namespace cel::runtime_internal diff --git a/runtime/internal/runtime_type_provider.h b/runtime/internal/runtime_type_provider.h index fc7163f6c..61f610ef9 100644 --- a/runtime/internal/runtime_type_provider.h +++ b/runtime/internal/runtime_type_provider.h @@ -15,6 +15,7 @@ #ifndef THIRD_PARTY_CEL_CPP_RUNTIME_INTERNAL_RUNTIME_TYPE_PROVIDER_H_ #define THIRD_PARTY_CEL_CPP_RUNTIME_INTERNAL_RUNTIME_TYPE_PROVIDER_H_ +#include "absl/base/attributes.h" #include "absl/base/nullability.h" #include "absl/container/flat_hash_map.h" #include "absl/status/status.h" @@ -45,6 +46,14 @@ class RuntimeTypeProvider final : public TypeReflector { google::protobuf::MessageFactory* absl_nonnull message_factory, google::protobuf::Arena* absl_nonnull arena) const override; + // Resolves the descriptor and prototype for `name` once. The returned factory + // builds values directly from the prototype as long as it is called with the + // same `message_factory`, and falls back to `NewValueBuilder` otherwise. + ValueBuilderFactory NewValueBuilderFactory( + absl::string_view name, + google::protobuf::MessageFactory* absl_nonnull message_factory) const + ABSL_ATTRIBUTE_LIFETIME_BOUND override; + protected: absl::StatusOr> FindTypeImpl( absl::string_view name) const override; diff --git a/runtime/internal/runtime_type_provider_test.cc b/runtime/internal/runtime_type_provider_test.cc new file mode 100644 index 000000000..fa3c856a7 --- /dev/null +++ b/runtime/internal/runtime_type_provider_test.cc @@ -0,0 +1,119 @@ +// Copyright 2026 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "runtime/internal/runtime_type_provider.h" + +#include +#include + +#include "absl/status/status_matchers.h" +#include "absl/status/statusor.h" +#include "cel/expr/conformance/proto3/test_all_types.pb.h" +#include "common/value.h" +#include "internal/testing.h" +#include "internal/testing_descriptor_pool.h" +#include "google/protobuf/arena.h" +#include "google/protobuf/descriptor.h" +#include "google/protobuf/dynamic_message.h" +#include "google/protobuf/message.h" + +namespace cel::runtime_internal { +namespace { + +using ::absl_testing::IsOkAndHolds; +using ::cel::expr::conformance::proto3::TestAllTypes; +using ::testing::Eq; +using ::testing::HasSubstr; +using ::testing::IsNull; +using ::testing::NotNull; + +const google::protobuf::Message* BuildMessage(ValueBuilder& builder) { + absl::StatusOr value = std::move(builder).Build(); + if (!value.ok() || !value->IsParsedMessage()) { + return nullptr; + } + return value->GetParsedMessage().message(); +} + +TEST(RuntimeTypeProviderTest, NewValueBuilderFactorySameFactory) { + RuntimeTypeProvider type_provider(google::protobuf::DescriptorPool::generated_pool()); + auto factory = type_provider.NewValueBuilderFactory( + TestAllTypes::descriptor()->full_name(), + google::protobuf::MessageFactory::generated_factory()); + + google::protobuf::Arena arena; + for (int i = 0; i < 2; ++i) { + ASSERT_OK_AND_ASSIGN( + auto builder, + factory(google::protobuf::MessageFactory::generated_factory(), &arena)); + ASSERT_THAT(builder, NotNull()); + EXPECT_THAT(builder->SetFieldByName("single_int32", IntValue(1)), + IsOkAndHolds(Eq(std::nullopt))); + const google::protobuf::Message* msg = BuildMessage(*builder); + ASSERT_THAT(msg, NotNull()); + EXPECT_EQ(msg->GetDescriptor(), TestAllTypes::descriptor()); + EXPECT_EQ(msg->GetReflection()->GetMessageFactory(), + google::protobuf::MessageFactory::generated_factory()); + EXPECT_THAT(msg->DebugString(), HasSubstr("single_int32: 1")); + } +} + +TEST(RuntimeTypeProviderTest, NewValueBuilderFactoryDifferentFactory) { + RuntimeTypeProvider type_provider(google::protobuf::DescriptorPool::generated_pool()); + auto factory = type_provider.NewValueBuilderFactory( + TestAllTypes::descriptor()->full_name(), + google::protobuf::MessageFactory::generated_factory()); + + google::protobuf::DynamicMessageFactory custom_factory; + google::protobuf::Arena arena; + ASSERT_OK_AND_ASSIGN(auto builder, factory(&custom_factory, &arena)); + ASSERT_THAT(builder, NotNull()); + const google::protobuf::Message* msg = BuildMessage(*builder); + ASSERT_THAT(msg, NotNull()); + EXPECT_EQ(msg->GetReflection()->GetMessageFactory(), &custom_factory); +} + +TEST(RuntimeTypeProviderTest, NewValueBuilderFactoryDynamicPool) { + const google::protobuf::DescriptorPool* pool = internal::GetTestingDescriptorPool(); + RuntimeTypeProvider type_provider(pool); + google::protobuf::DynamicMessageFactory message_factory; + auto factory = type_provider.NewValueBuilderFactory( + "cel.expr.conformance.proto3.TestAllTypes", &message_factory); + + google::protobuf::Arena arena; + ASSERT_OK_AND_ASSIGN(auto builder, factory(&message_factory, &arena)); + ASSERT_THAT(builder, NotNull()); + EXPECT_THAT(builder->SetFieldByName("single_int64", IntValue(2)), + IsOkAndHolds(Eq(std::nullopt))); + const google::protobuf::Message* msg = BuildMessage(*builder); + ASSERT_THAT(msg, NotNull()); + EXPECT_EQ( + msg->GetDescriptor(), + pool->FindMessageTypeByName("cel.expr.conformance.proto3.TestAllTypes")); + EXPECT_EQ(msg->GetReflection()->GetMessageFactory(), &message_factory); + EXPECT_THAT(msg->DebugString(), HasSubstr("single_int64: 2")); +} + +TEST(RuntimeTypeProviderTest, NewValueBuilderFactoryUnknownType) { + RuntimeTypeProvider type_provider(google::protobuf::DescriptorPool::generated_pool()); + auto factory = type_provider.NewValueBuilderFactory( + "does.not.Exist", google::protobuf::MessageFactory::generated_factory()); + + google::protobuf::Arena arena; + EXPECT_THAT(factory(google::protobuf::MessageFactory::generated_factory(), &arena), + IsOkAndHolds(IsNull())); +} + +} // namespace +} // namespace cel::runtime_internal diff --git a/runtime/runtime_options.h b/runtime/runtime_options.h index 1c52e9b4c..23f5058b1 100644 --- a/runtime/runtime_options.h +++ b/runtime/runtime_options.h @@ -210,6 +210,14 @@ struct RuntimeOptions { // // Note: Enabling this option may change observed expression tracing behavior. bool enable_subexpression_inlining = false; + + // When enabled, the planner resolves the message type of struct creation + // expressions (descriptor and prototype lookups) at plan time, instead of + // looking up the type by name every time the expression is evaluated. + // + // The resolved prototype is only used if the program is evaluated with the + // runtime's message factory; otherwise the type is looked up by name. + bool enable_eager_struct_resolution = false; }; // LINT.ThenChange(//depot/google3/eval/public/cel_options.h)