Skip to content
Open
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
1 change: 1 addition & 0 deletions common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
35 changes: 35 additions & 0 deletions common/type_reflector.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
#ifndef THIRD_PARTY_CEL_CPP_COMMON_TYPE_REFLECTOR_H_
#define THIRD_PARTY_CEL_CPP_COMMON_TYPE_REFLECTOR_H_

#include <string>

#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"
Expand All @@ -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<absl::StatusOr<absl_nullable ValueBuilderPtr>(
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<void>(message_factory);
return [this, name = std::string(name)](
google::protobuf::MessageFactory* absl_nonnull message_factory,
google::protobuf::Arena* absl_nonnull arena)
-> absl::StatusOr<absl_nullable ValueBuilderPtr> {
return NewValueBuilder(name, message_factory, arena);
};
}
};

} // namespace cel
Expand Down
15 changes: 12 additions & 3 deletions common/values/struct_value_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<ValueBuilderImpl>(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,
Expand All @@ -1580,9 +1590,8 @@ absl_nullable cel::ValueBuilderPtr NewValueBuilder(
if (ABSL_PREDICT_FALSE(prototype == nullptr)) {
return nullptr;
}
return std::make_unique<ValueBuilderImpl>(allocator.arena(), descriptor_pool,
message_factory,
prototype->New(allocator.arena()));
return NewValueBuilder(allocator, descriptor_pool, message_factory,
prototype);
}

absl_nullable cel::StructValueBuilderPtr NewStructValueBuilder(
Expand Down
8 changes: 8 additions & 0 deletions common/values/value_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
2 changes: 2 additions & 0 deletions conformance/service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down
13 changes: 11 additions & 2 deletions eval/compiler/flat_expr_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -1400,8 +1401,16 @@ class FlatExprVisitor : public cel::AstVisitor {
num_fields <= static_cast<size_t>(std::numeric_limits<int64_t>::max())
? 1 - static_cast<int64_t>(num_fields)
: std::numeric_limits<int16_t>::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);
}

Expand Down
1 change: 1 addition & 0 deletions eval/eval/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
37 changes: 31 additions & 6 deletions eval/eval/create_struct_step.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
#include <utility>
#include <vector>

#include "absl/base/nullability.h"
#include "absl/container/flat_hash_set.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#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"
Expand All @@ -44,10 +46,13 @@ using ::cel::Value;
// `CreateStruct` implementation for message/struct.
class CreateStructStepForStruct final : public ExpressionStepBase {
public:
CreateStructStepForStruct(std::string name, std::vector<std::string> entries,
absl::flat_hash_set<int32_t> optional_indices)
CreateStructStepForStruct(
std::string name, cel::TypeReflector::ValueBuilderFactory builder_factory,
std::vector<std::string> entries,
absl::flat_hash_set<int32_t> optional_indices)
: ExpressionStepBase(),
name_(std::move(name)),
builder_factory_(std::move(builder_factory)),
entries_(std::move(entries)),
optional_indices_(std::move(optional_indices)) {}

Expand All @@ -56,7 +61,19 @@ class CreateStructStepForStruct final : public ExpressionStepBase {
private:
absl::StatusOr<Value> DoEvaluate(ExecutionFrame* frame) const;

absl::StatusOr<absl_nullable cel::ValueBuilderPtr> 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<std::string> entries_;
absl::flat_hash_set<int32_t> optional_indices_;
};
Expand All @@ -82,9 +99,7 @@ absl::StatusOr<Value> 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_)),
Expand Down Expand Up @@ -148,6 +163,16 @@ std::unique_ptr<ExpressionStepLogic> CreateCreateStructStep(
absl::flat_hash_set<int32_t> optional_indices) {
// MakeOptionalIndicesSet(create_struct_expr)
return std::make_unique<CreateStructStepForStruct>(
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<ExpressionStepLogic> CreateCreateStructStep(
std::string name, cel::TypeReflector::ValueBuilderFactory builder_factory,
std::vector<std::string> field_keys,
absl::flat_hash_set<int32_t> optional_indices) {
return std::make_unique<CreateStructStepForStruct>(
std::move(name), std::move(builder_factory), std::move(field_keys),
std::move(optional_indices));
}
} // namespace google::api::expr::runtime
9 changes: 9 additions & 0 deletions eval/eval/create_struct_step.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <vector>

#include "absl/container/flat_hash_set.h"
#include "common/type_reflector.h"
#include "eval/eval/expression_step_logic.h"

namespace google::api::expr::runtime {
Expand All @@ -31,6 +32,14 @@ std::unique_ptr<ExpressionStepLogic> CreateCreateStructStep(
std::string name, std::vector<std::string> field_keys,
absl::flat_hash_set<int32_t> 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<ExpressionStepLogic> CreateCreateStructStep(
std::string name, cel::TypeReflector::ValueBuilderFactory builder_factory,
std::vector<std::string> field_keys,
absl::flat_hash_set<int32_t> optional_indices);

} // namespace google::api::expr::runtime

#endif // THIRD_PARTY_CEL_CPP_EVAL_EVAL_CREATE_STRUCT_STEP_H_
1 change: 1 addition & 0 deletions eval/public/cel_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}

Expand Down
9 changes: 9 additions & 0 deletions eval/public/cel_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
47 changes: 47 additions & 0 deletions eval/tests/modern_benchmark_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"(
Expand Down
16 changes: 16 additions & 0 deletions runtime/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
],
)
Loading
Loading