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
4 changes: 4 additions & 0 deletions common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@ cc_library(
"//eval/internal:cel_value_equal",
"//eval/public:cel_value",
"//eval/public:message_wrapper",
"//eval/public/containers:internal_field_backed_list_impl",
"//eval/public/structs:cel_proto_wrap_value_to_message",
"//eval/public/structs:legacy_type_info_apis",
"//eval/public/structs:proto_message_type_adapter",
Expand Down Expand Up @@ -834,6 +835,8 @@ cc_test(
":value_testing",
"//base:attributes",
"//eval/public:cel_value",
"//eval/public/containers:internal_field_backed_list_impl",
"//eval/public/structs:cel_proto_wrapper",
"//eval/public/structs:proto_message_type_adapter",
"//internal:parse_text_proto",
"//internal:status_macros",
Expand All @@ -846,6 +849,7 @@ cc_test(
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/functional:function_ref",
"@com_google_absl//absl/hash",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/log:die_if_null",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:status_matchers",
Expand Down
60 changes: 45 additions & 15 deletions common/value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,9 @@ void BoolRepeatedFieldAccessor(
*result = BoolValue(reflection->GetRepeatedBool(*message, field, index));
}

// If `Unsafe` is `std::true_type`, elements are borrowed rather than copied to
// `arena`, as with `Value::WrapRepeatedFieldUnsafe`.
template <typename Unsafe>
void StringRepeatedFieldAccessor(
int index, const google::protobuf::Message* absl_nonnull message,
const google::protobuf::FieldDescriptor* absl_nonnull field,
Expand Down Expand Up @@ -964,20 +967,26 @@ void StringRepeatedFieldAccessor(
string.size() == scratch.size()) {
*result = StringValue::From(std::move(scratch), arena);
} else {
if (message->GetArena() == nullptr) {
*result = StringValue::From(string, arena);
if constexpr (Unsafe::value) {
*result = StringValue::WrapUnsafe(string);
} else {
*result = StringValue::Wrap(string, message->GetArena());
if (google::protobuf::Arena* message_arena = message->GetArena();
message_arena != nullptr) {
*result = StringValue::Wrap(string, message_arena);
} else {
*result = StringValue::From(string, arena);
}
}
}
},
[&](absl::Cord&& cord) {
*result = StringValue::From(std::move(cord), arena);
}),
well_known_types::AsVariant(well_known_types::GetRepeatedStringField(
*message, field, index, scratch)));
reflection, *message, field, index, scratch)));
}

template <typename Unsafe>
void MessageRepeatedFieldAccessor(
int index, const google::protobuf::Message* absl_nonnull message,
const google::protobuf::FieldDescriptor* absl_nonnull field,
Expand All @@ -999,11 +1008,18 @@ void MessageRepeatedFieldAccessor(
ABSL_DCHECK_GE(index, 0);
ABSL_DCHECK_LT(index, reflection->FieldSize(*message, field));

*result = Value::WrapMessage(
&reflection->GetRepeatedMessage(*message, field, index), descriptor_pool,
message_factory, arena);
if constexpr (Unsafe::value) {
*result = Value::WrapMessageUnsafe(
&reflection->GetRepeatedMessage(*message, field, index),
descriptor_pool, message_factory, arena);
} else {
*result = Value::WrapMessage(
&reflection->GetRepeatedMessage(*message, field, index),
descriptor_pool, message_factory, arena);
}
}

template <typename Unsafe>
void BytesRepeatedFieldAccessor(
int index, const google::protobuf::Message* absl_nonnull message,
const google::protobuf::FieldDescriptor* absl_nonnull field,
Expand Down Expand Up @@ -1033,18 +1049,23 @@ void BytesRepeatedFieldAccessor(
string.size() == scratch.size()) {
*result = BytesValue::From(std::move(scratch), arena);
} else {
if (message->GetArena() == nullptr) {
*result = BytesValue::From(string, arena);
if constexpr (Unsafe::value) {
*result = BytesValue::WrapUnsafe(string);
} else {
*result = BytesValue::Wrap(string, message->GetArena());
if (google::protobuf::Arena* message_arena = message->GetArena();
message_arena != nullptr) {
*result = BytesValue::Wrap(string, message_arena);
} else {
*result = BytesValue::From(string, arena);
}
}
}
},
[&](absl::Cord&& cord) {
*result = BytesValue::From(std::move(cord), arena);
}),
well_known_types::AsVariant(well_known_types::GetRepeatedBytesField(
*message, field, index, scratch)));
reflection, *message, field, index, scratch)));
}

void EnumRepeatedFieldAccessor(
Expand Down Expand Up @@ -1101,7 +1122,7 @@ void NullRepeatedFieldAccessor(
} // namespace

absl::StatusOr<RepeatedFieldAccessor> RepeatedFieldAccessorFor(
const google::protobuf::FieldDescriptor* absl_nonnull field) {
const google::protobuf::FieldDescriptor* absl_nonnull field, bool unsafe) {
switch (field->type()) {
case google::protobuf::FieldDescriptor::TYPE_DOUBLE:
return &DoubleRepeatedFieldAccessor;
Expand All @@ -1126,13 +1147,22 @@ absl::StatusOr<RepeatedFieldAccessor> RepeatedFieldAccessorFor(
case google::protobuf::FieldDescriptor::TYPE_BOOL:
return &BoolRepeatedFieldAccessor;
case google::protobuf::FieldDescriptor::TYPE_STRING:
return &StringRepeatedFieldAccessor;
if (unsafe) {
return &StringRepeatedFieldAccessor<std::true_type>;
}
return &StringRepeatedFieldAccessor<std::false_type>;
case google::protobuf::FieldDescriptor::TYPE_GROUP:
ABSL_FALLTHROUGH_INTENDED;
case google::protobuf::FieldDescriptor::TYPE_MESSAGE:
return &MessageRepeatedFieldAccessor;
if (unsafe) {
return &MessageRepeatedFieldAccessor<std::true_type>;
}
return &MessageRepeatedFieldAccessor<std::false_type>;
case google::protobuf::FieldDescriptor::TYPE_BYTES:
return &BytesRepeatedFieldAccessor;
if (unsafe) {
return &BytesRepeatedFieldAccessor<std::true_type>;
}
return &BytesRepeatedFieldAccessor<std::false_type>;
case google::protobuf::FieldDescriptor::TYPE_FIXED32:
ABSL_FALLTHROUGH_INTENDED;
case google::protobuf::FieldDescriptor::TYPE_UINT32:
Expand Down
5 changes: 4 additions & 1 deletion common/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -2888,8 +2888,11 @@ using RepeatedFieldAccessor =
google::protobuf::MessageFactory* absl_nonnull, google::protobuf::Arena* absl_nonnull,
Value* absl_nonnull);

// If `unsafe` is true, message, string, and bytes elements are borrowed rather
// than copied, as with `Value::WrapRepeatedFieldUnsafe`. Caller is responsible
// for ensuring the resulting values do not outlive the input message.
absl::StatusOr<RepeatedFieldAccessor> RepeatedFieldAccessorFor(
const google::protobuf::FieldDescriptor* absl_nonnull field);
const google::protobuf::FieldDescriptor* absl_nonnull field, bool unsafe = false);

} // namespace common_internal

Expand Down
30 changes: 30 additions & 0 deletions common/values/legacy_list_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "common/values/list_value_builder.h"
#include "common/values/values.h"
#include "eval/public/cel_value.h"
#include "eval/public/containers/internal_field_backed_list_impl.h"
#include "internal/casts.h"
#include "internal/status_macros.h"
#include "google/protobuf/arena.h"
Expand Down Expand Up @@ -300,10 +301,39 @@ CustomListValue WrapLegacyParsedJsonList(ParsedJsonListValue value,
arena);
}

namespace {

// Returns true if `list` can be replaced by an `UnsafeParsedRepeatedFieldValue`
// over the same field without changing the elements it produces.
bool CanViewAsParsedRepeatedField(
const google::api::expr::runtime::internal::FieldBackedListImpl& list) {
// Elements of arena messages would be copied rather than borrowed.
if (list.message()->GetArena() != nullptr) {
return false;
}
// `FieldBackedListImpl` produces integers for `google.protobuf.NullValue`.
const google::protobuf::FieldDescriptor* field = list.field_descriptor();
return field->cpp_type() != google::protobuf::FieldDescriptor::CPPTYPE_ENUM ||
field->enum_type()->full_name() != "google.protobuf.NullValue";
}

} // namespace

ListValue FromLegacyList(
const google::api::expr::runtime::CelList* absl_nullable cel_list) {
if (cel_list != nullptr) {
NativeTypeId native_type_id = NativeTypeId::Of(*cel_list);
if (native_type_id ==
NativeTypeId::For<
google::api::expr::runtime::internal::FieldBackedListImpl>()) {
const auto* impl = cel::internal::down_cast<
const google::api::expr::runtime::internal::FieldBackedListImpl*>(
cel_list);
if (CanViewAsParsedRepeatedField(*impl)) {
return UnsafeParsedRepeatedFieldValue(impl->message(),
impl->field_descriptor());
}
}
if (native_type_id ==
NativeTypeId::For<LegacyParsedRepeatedFieldListValue>()) {
const auto* impl =
Expand Down
Loading
Loading