Skip to content

Commit 49ad515

Browse files
jnthntatumcopybara-github
authored andcommitted
Default opt in select optimization to use eagerly resolve field descriptor lookups.
PiperOrigin-RevId: 1002720724
1 parent e51ef9d commit 49ad515

9 files changed

Lines changed: 2254 additions & 832 deletions

‎eval/compiler/BUILD‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ cc_library(
3131
":resolver",
3232
"//base:ast",
3333
"//base:data",
34+
"//common:ast",
3435
"//common:expr",
3536
"//common:type",
37+
"//common:type_spec_resolver",
3638
"//common:value",
3739
"//eval/eval:evaluator_core",
3840
"//eval/eval:expression_step_logic",
@@ -147,21 +149,11 @@ cc_test(
147149
":cel_expression_builder_flat_impl",
148150
":constant_folding",
149151
":flat_expr_builder",
150-
":flat_expr_builder_extensions",
151152
":qualified_reference_resolver",
152153
"//base:builtins",
153-
"//checker:validation_result",
154-
"//common:ast",
155-
"//common:decl",
156-
"//common:expr",
157154
"//common:function_descriptor",
158155
"//common:kind",
159-
"//common:type",
160156
"//common:value",
161-
"//compiler",
162-
"//compiler:compiler_factory",
163-
"//compiler:standard_library",
164-
"//eval/eval:evaluator_core",
165157
"//eval/eval:expression_step_logic",
166158
"//eval/public:activation",
167159
"//eval/public:ast_rewrite",

‎eval/compiler/flat_expr_builder.cc‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,13 +1860,7 @@ void FlatExprVisitor::MaybeResolveType(const cel::Expr& expr) {
18601860
if (it == type_map_.end()) {
18611861
return;
18621862
}
1863-
absl::StatusOr<cel::Type> type = cel::ConvertTypeSpecToType(
1864-
it->second, extension_context_.type_reflector(),
1865-
extension_context_.MutableArena());
1866-
if (!type.ok()) {
1867-
return;
1868-
}
1869-
extension_context_.SetResolvedType(expr.id(), *type);
1863+
extension_context_.TryResolveType(expr.id(), it->second);
18701864
}
18711865

18721866
void LogicalCondVisitor::PreVisit(const cel::Expr* expr) {

‎eval/compiler/flat_expr_builder_extensions.cc‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
#include "eval/compiler/flat_expr_builder_extensions.h"
1515

1616
#include <cstddef>
17+
#include <cstdint>
1718
#include <iterator>
1819
#include <memory>
20+
#include <optional>
1921
#include <utility>
2022
#include <vector>
2123

@@ -27,7 +29,10 @@
2729
#include "absl/status/status.h"
2830
#include "absl/status/statusor.h"
2931
#include "absl/types/variant.h"
32+
#include "common/ast.h"
3033
#include "common/expr.h"
34+
#include "common/type.h"
35+
#include "common/type_spec_resolver.h"
3136
#include "eval/eval/evaluator_core.h"
3237

3338
namespace google::api::expr::runtime {
@@ -350,6 +355,21 @@ absl::Status PlannerContext::ReplaceSubplan(const cel::Expr& node,
350355
return absl::OkStatus();
351356
}
352357

358+
std::optional<cel::Type> PlannerContext::TryResolveType(
359+
int64_t expr_id, const cel::TypeSpec& ast_type) {
360+
auto it = resolved_types_.find(expr_id);
361+
if (it != resolved_types_.end()) {
362+
return it->second;
363+
}
364+
absl::StatusOr<cel::Type> type =
365+
cel::ConvertTypeSpecToType(ast_type, type_reflector_, MutableArena());
366+
if (!type.ok()) {
367+
return std::nullopt;
368+
}
369+
resolved_types_.insert(it, {expr_id, *type});
370+
return *type;
371+
}
372+
353373
void ProgramBuilder::Reset() {
354374
root_ = nullptr;
355375
current_ = nullptr;

‎eval/compiler/flat_expr_builder_extensions.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,9 @@ class PlannerContext {
381381
return std::nullopt;
382382
}
383383

384+
std::optional<cel::Type> TryResolveType(int64_t expr_id,
385+
const cel::TypeSpec& ast_type);
386+
384387
void SetResolvedType(int64_t expr_id, cel::Type type) {
385388
resolved_types_[expr_id] = std::move(type);
386389
}

‎eval/compiler/flat_expr_builder_test.cc‎

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include <functional>
1919
#include <limits>
2020
#include <memory>
21-
#include <optional>
2221
#include <string>
2322
#include <utility>
2423
#include <vector>
@@ -36,22 +35,12 @@
3635
#include "absl/time/time.h"
3736
#include "absl/types/span.h"
3837
#include "base/builtins.h"
39-
#include "checker/validation_result.h"
40-
#include "common/ast.h"
41-
#include "common/decl.h"
42-
#include "common/expr.h"
4338
#include "common/function_descriptor.h"
4439
#include "common/kind.h"
45-
#include "common/type.h"
4640
#include "common/value.h"
47-
#include "compiler/compiler.h"
48-
#include "compiler/compiler_factory.h"
49-
#include "compiler/standard_library.h"
5041
#include "eval/compiler/cel_expression_builder_flat_impl.h"
5142
#include "eval/compiler/constant_folding.h"
52-
#include "eval/compiler/flat_expr_builder_extensions.h"
5343
#include "eval/compiler/qualified_reference_resolver.h"
54-
#include "eval/eval/evaluator_core.h"
5544
#include "eval/eval/expression_step_logic.h"
5645
#include "eval/public/activation.h"
5746
#include "eval/public/ast_rewrite.h"
@@ -3417,22 +3406,33 @@ TEST(FlatExprBuilderTest, ListAppendComprehensionWithTargetFailsPlanning) {
34173406
}
34183407

34193408
TEST(FlatExprBuilderTest, ProgramOptimizerResolvesTypesWhenRequested) {
3420-
std::shared_ptr<cel::runtime_internal::RuntimeEnv> env =
3421-
NewTestingRuntimeEnv();
3422-
ASSERT_OK_AND_ASSIGN(std::unique_ptr<cel::CompilerBuilder> compiler_builder,
3423-
cel::NewCompilerBuilder(env->descriptor_pool));
3424-
ASSERT_THAT(compiler_builder->AddLibrary(cel::StandardCompilerLibrary()),
3425-
IsOk());
3426-
ASSERT_THAT(compiler_builder->GetCheckerBuilder().AddVariable(
3427-
cel::MakeVariableDecl("x", cel::IntType())),
3428-
IsOk());
3429-
ASSERT_OK_AND_ASSIGN(std::unique_ptr<cel::Compiler> compiler,
3430-
compiler_builder->Build());
3431-
ASSERT_OK_AND_ASSIGN(cel::ValidationResult validation_result,
3432-
compiler->Compile("x == 1"));
3433-
ASSERT_TRUE(validation_result.IsValid());
3434-
ASSERT_OK_AND_ASSIGN(std::unique_ptr<cel::Ast> ast,
3435-
validation_result.ReleaseAst());
3409+
CheckedExpr expr;
3410+
ASSERT_TRUE(google::protobuf::TextFormat::ParseFromString(
3411+
R"pb(
3412+
type_map {
3413+
key: 1
3414+
value { primitive: BOOL }
3415+
}
3416+
type_map {
3417+
key: 2
3418+
value { primitive: INT64 }
3419+
}
3420+
expr {
3421+
id: 1
3422+
call_expr {
3423+
function: "_==_"
3424+
args {
3425+
id: 2
3426+
ident_expr { name: "x" }
3427+
}
3428+
args {
3429+
id: 3
3430+
const_expr { int64_value: 1 }
3431+
}
3432+
}
3433+
}
3434+
)pb",
3435+
&expr));
34363436

34373437
class TypeRecordingOptimizer : public ProgramOptimizer {
34383438
public:
@@ -3467,7 +3467,7 @@ TEST(FlatExprBuilderTest, ProgramOptimizerResolvesTypesWhenRequested) {
34673467

34683468
cel::RuntimeOptions options;
34693469
options.enable_typed_field_access = false;
3470-
CelExpressionBuilderFlatImpl builder(env, options);
3470+
CelExpressionBuilderFlatImpl builder(NewTestingRuntimeEnv(), options);
34713471
ASSERT_THAT(RegisterBuiltinFunctions(builder.GetRegistry()), IsOk());
34723472

34733473
absl::flat_hash_map<int64_t, cel::Type> pre_visit_types;
@@ -3480,17 +3480,14 @@ TEST(FlatExprBuilderTest, ProgramOptimizerResolvesTypesWhenRequested) {
34803480
post_visit_types);
34813481
});
34823482

3483-
ASSERT_OK_AND_ASSIGN(FlatExpression flat_expr,
3484-
builder.flat_expr_builder().CreateExpressionImpl(
3485-
std::move(ast), /*issues=*/nullptr));
3483+
ASSERT_OK_AND_ASSIGN(std::unique_ptr<CelExpression> cel_expr,
3484+
builder.CreateExpression(&expr));
34863485
EXPECT_THAT(pre_visit_types,
3487-
testing::UnorderedElementsAre(testing::Pair(1, cel::IntType()),
3488-
testing::Pair(2, cel::BoolType()),
3489-
testing::Pair(3, cel::IntType())));
3486+
testing::UnorderedElementsAre(testing::Pair(1, cel::BoolType()),
3487+
testing::Pair(2, cel::IntType())));
34903488
EXPECT_THAT(post_visit_types,
3491-
testing::UnorderedElementsAre(testing::Pair(1, cel::IntType()),
3492-
testing::Pair(2, cel::BoolType()),
3493-
testing::Pair(3, cel::IntType())));
3489+
testing::UnorderedElementsAre(testing::Pair(1, cel::BoolType()),
3490+
testing::Pair(2, cel::IntType())));
34943491
}
34953492

34963493
} // namespace

‎extensions/BUILD‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ cc_library(
329329
"//common:expr",
330330
"//common:function_descriptor",
331331
"//common:kind",
332+
"//common:legacy_value",
332333
"//common:native_type",
333334
"//common:type",
334335
"//common:value",
@@ -337,6 +338,7 @@ cc_library(
337338
"//eval/eval:attribute_trail",
338339
"//eval/eval:evaluator_core",
339340
"//eval/eval:expression_step_base",
341+
"//extensions/protobuf/internal:map_reflection",
340342
"//internal:casts",
341343
"//internal:number",
342344
"//internal:status_macros",
@@ -346,6 +348,7 @@ cc_library(
346348
"//runtime/internal:runtime_friend_access",
347349
"//runtime/internal:runtime_impl",
348350
"@com_google_absl//absl/algorithm:container",
351+
"@com_google_absl//absl/base:core_headers",
349352
"@com_google_absl//absl/base:nullability",
350353
"@com_google_absl//absl/container:flat_hash_map",
351354
"@com_google_absl//absl/functional:overload",
@@ -394,15 +397,19 @@ cc_test(
394397
"//internal:status_macros",
395398
"//internal:testing",
396399
"//parser",
400+
"//runtime",
397401
"//runtime:activation",
398402
"//runtime:function_adapter",
399403
"//runtime:function_registry",
404+
"//runtime:runtime_builder",
400405
"//runtime:runtime_issue",
401406
"//runtime:runtime_options",
407+
"//runtime:standard_runtime_builder_factory",
402408
"//runtime:type_registry",
403409
"//runtime/internal:issue_collector",
404410
"//runtime/internal:runtime_env",
405411
"//runtime/internal:runtime_env_testing",
412+
"//testutil:test_external_extensions_descriptor_set",
406413
"@com_google_absl//absl/base:nullability",
407414
"@com_google_absl//absl/container:flat_hash_map",
408415
"@com_google_absl//absl/log:absl_check",

0 commit comments

Comments
 (0)