Skip to content

Commit d0759b0

Browse files
jckingcopybara-github
authored andcommitted
Remove dependency on getting list/map value builders from factory/managers
PiperOrigin-RevId: 689544397
1 parent aff8696 commit d0759b0

File tree

5 files changed

+45
-48
lines changed

5 files changed

+45
-48
lines changed

common/values/list_value_builder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ const MutableListValue& GetMutableListValue(
9696
const MutableListValue& GetMutableListValue(
9797
const ListValue& value ABSL_ATTRIBUTE_LIFETIME_BOUND);
9898

99+
absl::Nonnull<cel::ListValueBuilderPtr> NewListValueBuilder(
100+
Allocator<> allocator);
99101
absl::Nonnull<cel::ListValueBuilderPtr> NewListValueBuilder(
100102
ValueFactory& value_factory);
101103

common/values/map_value_builder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ const MutableMapValue& GetMutableMapValue(
9696
const MutableMapValue& GetMutableMapValue(
9797
const MapValue& value ABSL_ATTRIBUTE_LIFETIME_BOUND);
9898

99+
absl::Nonnull<cel::MapValueBuilderPtr> NewMapValueBuilder(
100+
Allocator<> allocator);
99101
absl::Nonnull<cel::MapValueBuilderPtr> NewMapValueBuilder(
100102
ValueFactory& value_factory);
101103

common/values/value_builder.cc

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -502,11 +502,8 @@ class NonTrivialMutableListValueImpl final : public MutableListValue {
502502

503503
class TrivialListValueBuilderImpl final : public ListValueBuilder {
504504
public:
505-
TrivialListValueBuilderImpl(ValueFactory& value_factory,
506-
absl::Nonnull<google::protobuf::Arena*> arena)
507-
: value_factory_(value_factory), elements_(arena) {
508-
ABSL_DCHECK_EQ(value_factory_.GetMemoryManager().arena(), arena);
509-
}
505+
explicit TrivialListValueBuilderImpl(absl::Nonnull<google::protobuf::Arena*> arena)
506+
: arena_(arena), elements_(arena_) {}
510507

511508
absl::Status Add(Value value) override {
512509
CEL_RETURN_IF_ERROR(CheckListElement(value));
@@ -524,19 +521,18 @@ class TrivialListValueBuilderImpl final : public ListValueBuilder {
524521
return ListValue();
525522
}
526523
return ParsedListValue(
527-
value_factory_.GetMemoryManager().MakeShared<TrivialListValueImpl>(
524+
MemoryManager::Pooling(arena_).MakeShared<TrivialListValueImpl>(
528525
std::move(elements_)));
529526
}
530527

531528
private:
532-
ValueFactory& value_factory_;
529+
absl::Nonnull<google::protobuf::Arena*> const arena_;
533530
TrivialValueVector elements_;
534531
};
535532

536533
class NonTrivialListValueBuilderImpl final : public ListValueBuilder {
537534
public:
538-
explicit NonTrivialListValueBuilderImpl(ValueFactory& value_factory)
539-
: value_factory_(value_factory) {}
535+
NonTrivialListValueBuilderImpl() = default;
540536

541537
absl::Status Add(Value value) override {
542538
CEL_RETURN_IF_ERROR(CheckListElement(value));
@@ -553,12 +549,11 @@ class NonTrivialListValueBuilderImpl final : public ListValueBuilder {
553549
return ListValue();
554550
}
555551
return ParsedListValue(
556-
value_factory_.GetMemoryManager().MakeShared<NonTrivialListValueImpl>(
552+
MemoryManager::ReferenceCounting().MakeShared<NonTrivialListValueImpl>(
557553
std::move(elements_)));
558554
}
559555

560556
private:
561-
ValueFactory& value_factory_;
562557
NonTrivialValueVector elements_;
563558
};
564559

@@ -676,13 +671,17 @@ const MutableListValue& GetMutableListValue(const ListValue& value) {
676671
}
677672

678673
absl::Nonnull<cel::ListValueBuilderPtr> NewListValueBuilder(
679-
ValueFactory& value_factory) {
680-
if (absl::Nullable<google::protobuf::Arena*> arena =
681-
value_factory.GetMemoryManager().arena();
674+
Allocator<> allocator) {
675+
if (absl::Nullable<google::protobuf::Arena*> arena = allocator.arena();
682676
arena != nullptr) {
683-
return std::make_unique<TrivialListValueBuilderImpl>(value_factory, arena);
677+
return std::make_unique<TrivialListValueBuilderImpl>(arena);
684678
}
685-
return std::make_unique<NonTrivialListValueBuilderImpl>(value_factory);
679+
return std::make_unique<NonTrivialListValueBuilderImpl>();
680+
}
681+
682+
absl::Nonnull<cel::ListValueBuilderPtr> NewListValueBuilder(
683+
ValueFactory& value_factory) {
684+
return NewListValueBuilder(value_factory.GetMemoryManager());
686685
}
687686

688687
} // namespace common_internal
@@ -1451,11 +1450,8 @@ class NonTrivialMutableMapValueImpl final : public MutableMapValue {
14511450

14521451
class TrivialMapValueBuilderImpl final : public MapValueBuilder {
14531452
public:
1454-
TrivialMapValueBuilderImpl(ValueFactory& value_factory,
1455-
absl::Nonnull<google::protobuf::Arena*> arena)
1456-
: value_factory_(value_factory), map_(arena) {
1457-
ABSL_DCHECK_EQ(value_factory_.GetMemoryManager().arena(), arena);
1458-
}
1453+
explicit TrivialMapValueBuilderImpl(absl::Nonnull<google::protobuf::Arena*> arena)
1454+
: arena_(arena), map_(arena_) {}
14591455

14601456
absl::Status Put(Value key, Value value) override {
14611457
CEL_RETURN_IF_ERROR(CheckMapKey(key));
@@ -1480,20 +1476,18 @@ class TrivialMapValueBuilderImpl final : public MapValueBuilder {
14801476
return MapValue();
14811477
}
14821478
return ParsedMapValue(
1483-
value_factory_.GetMemoryManager().MakeShared<TrivialMapValueImpl>(
1479+
MemoryManager::Pooling(arena_).MakeShared<TrivialMapValueImpl>(
14841480
std::move(map_)));
14851481
}
14861482

14871483
private:
1488-
ValueFactory& value_factory_;
1484+
absl::Nonnull<google::protobuf::Arena*> const arena_;
14891485
TrivialValueFlatHashMap map_;
14901486
};
14911487

14921488
class NonTrivialMapValueBuilderImpl final : public MapValueBuilder {
14931489
public:
1494-
explicit NonTrivialMapValueBuilderImpl(ValueFactory& value_factory)
1495-
: value_factory_(value_factory),
1496-
map_(NonTrivialValueFlatHashMapAllocator{}) {}
1490+
NonTrivialMapValueBuilderImpl() = default;
14971491

14981492
absl::Status Put(Value key, Value value) override {
14991493
CEL_RETURN_IF_ERROR(CheckMapKey(key));
@@ -1517,12 +1511,11 @@ class NonTrivialMapValueBuilderImpl final : public MapValueBuilder {
15171511
return MapValue();
15181512
}
15191513
return ParsedMapValue(
1520-
value_factory_.GetMemoryManager().MakeShared<NonTrivialMapValueImpl>(
1514+
MemoryManager::ReferenceCounting().MakeShared<NonTrivialMapValueImpl>(
15211515
std::move(map_)));
15221516
}
15231517

15241518
private:
1525-
ValueFactory& value_factory_;
15261519
NonTrivialValueFlatHashMap map_;
15271520
};
15281521

@@ -1644,13 +1637,17 @@ const MutableMapValue& GetMutableMapValue(const MapValue& value) {
16441637
}
16451638

16461639
absl::Nonnull<cel::MapValueBuilderPtr> NewMapValueBuilder(
1647-
ValueFactory& value_factory) {
1648-
if (absl::Nullable<google::protobuf::Arena*> arena =
1649-
value_factory.GetMemoryManager().arena();
1640+
Allocator<> allocator) {
1641+
if (absl::Nullable<google::protobuf::Arena*> arena = allocator.arena();
16501642
arena != nullptr) {
1651-
return std::make_unique<TrivialMapValueBuilderImpl>(value_factory, arena);
1643+
return std::make_unique<TrivialMapValueBuilderImpl>(arena);
16521644
}
1653-
return std::make_unique<NonTrivialMapValueBuilderImpl>(value_factory);
1645+
return std::make_unique<NonTrivialMapValueBuilderImpl>();
1646+
}
1647+
1648+
absl::Nonnull<cel::MapValueBuilderPtr> NewMapValueBuilder(
1649+
ValueFactory& value_factory) {
1650+
return NewMapValueBuilder(value_factory.GetMemoryManager());
16541651
}
16551652

16561653
} // namespace common_internal

eval/eval/create_list_step.cc

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "absl/types/optional.h"
1313
#include "base/ast_internal/expr.h"
1414
#include "common/casting.h"
15-
#include "common/type.h"
1615
#include "common/value.h"
1716
#include "common/values/list_value_builder.h"
1817
#include "eval/eval/attribute_trail.h"
@@ -29,9 +28,10 @@ namespace {
2928
using ::cel::Cast;
3029
using ::cel::ErrorValue;
3130
using ::cel::InstanceOf;
32-
using ::cel::ListValueBuilderInterface;
31+
using ::cel::ListValueBuilderPtr;
3332
using ::cel::UnknownValue;
3433
using ::cel::Value;
34+
using ::cel::common_internal::NewListValueBuilder;
3535

3636
class CreateListStep : public ExpressionStepBase {
3737
public:
@@ -83,8 +83,7 @@ absl::Status CreateListStep::Evaluate(ExecutionFrame* frame) const {
8383
}
8484
}
8585

86-
CEL_ASSIGN_OR_RETURN(auto builder, frame->value_manager().NewListValueBuilder(
87-
cel::ListType()));
86+
ListValueBuilderPtr builder = NewListValueBuilder(frame->memory_manager());
8887

8988
builder->Reserve(args.size());
9089
for (size_t i = 0; i < args.size(); ++i) {
@@ -130,9 +129,8 @@ class CreateListDirectStep : public DirectExpressionStep {
130129

131130
absl::Status Evaluate(ExecutionFrameBase& frame, Value& result,
132131
AttributeTrail& attribute_trail) const override {
133-
CEL_ASSIGN_OR_RETURN(
134-
auto builder,
135-
frame.value_manager().NewListValueBuilder(cel::ListType()));
132+
ListValueBuilderPtr builder =
133+
NewListValueBuilder(frame.value_manager().GetMemoryManager());
136134

137135
builder->Reserve(elements_.size());
138136
AttributeUtility::Accumulator unknowns =
@@ -231,8 +229,6 @@ class DirectMutableListStep : public DirectExpressionStep {
231229
absl::Status DirectMutableListStep::Evaluate(
232230
ExecutionFrameBase& frame, Value& result,
233231
AttributeTrail& attribute_trail) const {
234-
CEL_ASSIGN_OR_RETURN(
235-
auto builder, frame.value_manager().NewListValueBuilder(cel::ListType()));
236232
result = cel::ParsedListValue(cel::common_internal::NewMutableListValue(
237233
frame.value_manager().GetMemoryManager().arena()));
238234
return absl::OkStatus();

eval/eval/create_map_step.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
#include "absl/strings/string_view.h"
2727
#include "absl/types/optional.h"
2828
#include "common/casting.h"
29-
#include "common/type.h"
3029
#include "common/value.h"
3130
#include "common/value_manager.h"
31+
#include "common/values/map_value_builder.h"
3232
#include "eval/eval/attribute_trail.h"
3333
#include "eval/eval/direct_expression_step.h"
3434
#include "eval/eval/evaluator_core.h"
@@ -42,9 +42,10 @@ namespace {
4242
using ::cel::Cast;
4343
using ::cel::ErrorValue;
4444
using ::cel::InstanceOf;
45-
using ::cel::StructValueBuilderInterface;
45+
using ::cel::MapValueBuilderPtr;
4646
using ::cel::UnknownValue;
4747
using ::cel::Value;
48+
using ::cel::common_internal::NewMapValueBuilder;
4849

4950
// `CreateStruct` implementation for map.
5051
class CreateStructStepForMap final : public ExpressionStepBase {
@@ -77,8 +78,7 @@ absl::StatusOr<Value> CreateStructStepForMap::DoEvaluate(
7778
}
7879
}
7980

80-
CEL_ASSIGN_OR_RETURN(
81-
auto builder, frame->value_manager().NewMapValueBuilder(cel::MapType{}));
81+
MapValueBuilderPtr builder = NewMapValueBuilder(frame->memory_manager());
8282
builder->Reserve(entry_count_);
8383

8484
for (size_t i = 0; i < entry_count_; i += 1) {
@@ -151,8 +151,8 @@ absl::Status DirectCreateMapStep::Evaluate(
151151
AttributeTrail tmp_attr;
152152
auto unknowns = frame.attribute_utility().CreateAccumulator();
153153

154-
CEL_ASSIGN_OR_RETURN(
155-
auto builder, frame.value_manager().NewMapValueBuilder(cel::MapType()));
154+
MapValueBuilderPtr builder =
155+
NewMapValueBuilder(frame.value_manager().GetMemoryManager());
156156
builder->Reserve(entry_count_);
157157

158158
for (size_t i = 0; i < entry_count_; i += 1) {

0 commit comments

Comments
 (0)