|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <string> |
| 16 | + |
| 17 | +#include "eval/public/cel_options.h" |
| 18 | +#include "eval/public/cel_value.h" |
| 19 | +#include "eval/public/structs/cel_proto_wrapper.h" |
| 20 | +#include "eval/public/structs/field_access_impl.h" |
| 21 | +#include "extensions/protobuf/internal/map_reflection.h" |
| 22 | +#include "internal/benchmark.h" |
| 23 | +#include "internal/testing.h" |
| 24 | +#include "cel/expr/conformance/proto3/test_all_types.pb.h" |
| 25 | +#include "google/protobuf/arena.h" |
| 26 | +#include "google/protobuf/descriptor.h" |
| 27 | +#include "google/protobuf/map_field.h" |
| 28 | +#include "google/protobuf/message.h" |
| 29 | + |
| 30 | +namespace google::api::expr::runtime::internal { |
| 31 | +namespace { |
| 32 | + |
| 33 | +using ::cel::expr::conformance::proto3::TestAllTypes; |
| 34 | + |
| 35 | +void BM_CreateValueFromSingleField_Int64(benchmark::State& state) { |
| 36 | + google::protobuf::Arena arena; |
| 37 | + TestAllTypes msg; |
| 38 | + msg.set_single_int64(42); |
| 39 | + const google::protobuf::FieldDescriptor* desc = |
| 40 | + TestAllTypes::descriptor()->FindFieldByName("single_int64"); |
| 41 | + |
| 42 | + for (auto _ : state) { |
| 43 | + auto value = CreateValueFromSingleField( |
| 44 | + &msg, desc, ProtoWrapperTypeOptions::kUnsetProtoDefault, |
| 45 | + &CelProtoWrapper::InternalWrapMessage, &arena); |
| 46 | + benchmark::DoNotOptimize(value); |
| 47 | + } |
| 48 | +} |
| 49 | +BENCHMARK(BM_CreateValueFromSingleField_Int64); |
| 50 | + |
| 51 | +void BM_CreateValueFromSingleField_String(benchmark::State& state) { |
| 52 | + google::protobuf::Arena arena; |
| 53 | + TestAllTypes msg; |
| 54 | + msg.set_single_string("hello world"); |
| 55 | + const google::protobuf::FieldDescriptor* desc = |
| 56 | + TestAllTypes::descriptor()->FindFieldByName("single_string"); |
| 57 | + |
| 58 | + for (auto _ : state) { |
| 59 | + auto value = CreateValueFromSingleField( |
| 60 | + &msg, desc, ProtoWrapperTypeOptions::kUnsetProtoDefault, |
| 61 | + &CelProtoWrapper::InternalWrapMessage, &arena); |
| 62 | + benchmark::DoNotOptimize(value); |
| 63 | + } |
| 64 | +} |
| 65 | +BENCHMARK(BM_CreateValueFromSingleField_String); |
| 66 | + |
| 67 | +void BM_CreateValueFromSingleField_Message(benchmark::State& state) { |
| 68 | + google::protobuf::Arena arena; |
| 69 | + TestAllTypes msg; |
| 70 | + msg.mutable_standalone_message()->set_bb(123); |
| 71 | + const google::protobuf::FieldDescriptor* desc = |
| 72 | + TestAllTypes::descriptor()->FindFieldByName("standalone_message"); |
| 73 | + |
| 74 | + for (auto _ : state) { |
| 75 | + auto value = CreateValueFromSingleField( |
| 76 | + &msg, desc, ProtoWrapperTypeOptions::kUnsetProtoDefault, |
| 77 | + &CelProtoWrapper::InternalWrapMessage, &arena); |
| 78 | + benchmark::DoNotOptimize(value); |
| 79 | + } |
| 80 | +} |
| 81 | +BENCHMARK(BM_CreateValueFromSingleField_Message); |
| 82 | + |
| 83 | +void BM_CreateValueFromRepeatedField_Int64(benchmark::State& state) { |
| 84 | + google::protobuf::Arena arena; |
| 85 | + TestAllTypes msg; |
| 86 | + msg.add_repeated_int64(42); |
| 87 | + const google::protobuf::FieldDescriptor* desc = |
| 88 | + TestAllTypes::descriptor()->FindFieldByName("repeated_int64"); |
| 89 | + |
| 90 | + for (auto _ : state) { |
| 91 | + auto value = CreateValueFromRepeatedField( |
| 92 | + &msg, desc, 0, &CelProtoWrapper::InternalWrapMessage, &arena); |
| 93 | + benchmark::DoNotOptimize(value); |
| 94 | + } |
| 95 | +} |
| 96 | +BENCHMARK(BM_CreateValueFromRepeatedField_Int64); |
| 97 | + |
| 98 | +void BM_CreateValueFromRepeatedField_String(benchmark::State& state) { |
| 99 | + google::protobuf::Arena arena; |
| 100 | + TestAllTypes msg; |
| 101 | + msg.add_repeated_string("hello world"); |
| 102 | + const google::protobuf::FieldDescriptor* desc = |
| 103 | + TestAllTypes::descriptor()->FindFieldByName("repeated_string"); |
| 104 | + |
| 105 | + for (auto _ : state) { |
| 106 | + auto value = CreateValueFromRepeatedField( |
| 107 | + &msg, desc, 0, &CelProtoWrapper::InternalWrapMessage, &arena); |
| 108 | + benchmark::DoNotOptimize(value); |
| 109 | + } |
| 110 | +} |
| 111 | +BENCHMARK(BM_CreateValueFromRepeatedField_String); |
| 112 | + |
| 113 | +void BM_CreateValueFromMapValue_Int64(benchmark::State& state) { |
| 114 | + google::protobuf::Arena arena; |
| 115 | + TestAllTypes msg; |
| 116 | + (*msg.mutable_map_int64_int64())[42] = 100; |
| 117 | + const google::protobuf::FieldDescriptor* map_desc = |
| 118 | + TestAllTypes::descriptor()->FindFieldByName("map_int64_int64"); |
| 119 | + const google::protobuf::FieldDescriptor* value_desc = |
| 120 | + map_desc->message_type()->FindFieldByName("value"); |
| 121 | + |
| 122 | + google::protobuf::ConstMapIterator iter = |
| 123 | + cel::extensions::protobuf_internal::ConstMapBegin(*msg.GetReflection(), |
| 124 | + msg, *map_desc); |
| 125 | + google::protobuf::MapValueConstRef value_ref = iter.GetValueRef(); |
| 126 | + |
| 127 | + for (auto _ : state) { |
| 128 | + auto value = |
| 129 | + CreateValueFromMapValue(&msg, value_desc, &value_ref, |
| 130 | + &CelProtoWrapper::InternalWrapMessage, &arena); |
| 131 | + benchmark::DoNotOptimize(value); |
| 132 | + } |
| 133 | +} |
| 134 | +BENCHMARK(BM_CreateValueFromMapValue_Int64); |
| 135 | + |
| 136 | +void BM_SetValueToSingleField_Int64(benchmark::State& state) { |
| 137 | + google::protobuf::Arena arena; |
| 138 | + TestAllTypes msg; |
| 139 | + const google::protobuf::FieldDescriptor* desc = |
| 140 | + TestAllTypes::descriptor()->FindFieldByName("single_int64"); |
| 141 | + CelValue val = CelValue::CreateInt64(42); |
| 142 | + |
| 143 | + for (auto _ : state) { |
| 144 | + auto status = SetValueToSingleField(val, desc, &msg, &arena); |
| 145 | + benchmark::DoNotOptimize(status); |
| 146 | + } |
| 147 | +} |
| 148 | +BENCHMARK(BM_SetValueToSingleField_Int64); |
| 149 | + |
| 150 | +void BM_SetValueToSingleField_String(benchmark::State& state) { |
| 151 | + google::protobuf::Arena arena; |
| 152 | + TestAllTypes msg; |
| 153 | + const google::protobuf::FieldDescriptor* desc = |
| 154 | + TestAllTypes::descriptor()->FindFieldByName("single_string"); |
| 155 | + CelValue val = CelValue::CreateStringView("hello world"); |
| 156 | + |
| 157 | + for (auto _ : state) { |
| 158 | + auto status = SetValueToSingleField(val, desc, &msg, &arena); |
| 159 | + benchmark::DoNotOptimize(status); |
| 160 | + } |
| 161 | +} |
| 162 | +BENCHMARK(BM_SetValueToSingleField_String); |
| 163 | + |
| 164 | +void BM_SetValueToSingleField_Message(benchmark::State& state) { |
| 165 | + google::protobuf::Arena arena; |
| 166 | + TestAllTypes msg; |
| 167 | + const google::protobuf::FieldDescriptor* desc = |
| 168 | + TestAllTypes::descriptor()->FindFieldByName("standalone_message"); |
| 169 | + |
| 170 | + TestAllTypes::NestedMessage nested_msg; |
| 171 | + nested_msg.set_bb(123); |
| 172 | + CelValue val = CelProtoWrapper::CreateMessage(&nested_msg, &arena); |
| 173 | + |
| 174 | + for (auto _ : state) { |
| 175 | + auto status = SetValueToSingleField(val, desc, &msg, &arena); |
| 176 | + benchmark::DoNotOptimize(status); |
| 177 | + } |
| 178 | +} |
| 179 | +BENCHMARK(BM_SetValueToSingleField_Message); |
| 180 | + |
| 181 | +void BM_AddValueToRepeatedField_Int64(benchmark::State& state) { |
| 182 | + google::protobuf::Arena arena; |
| 183 | + TestAllTypes msg; |
| 184 | + const google::protobuf::FieldDescriptor* desc = |
| 185 | + TestAllTypes::descriptor()->FindFieldByName("repeated_int64"); |
| 186 | + CelValue val = CelValue::CreateInt64(42); |
| 187 | + |
| 188 | + for (auto _ : state) { |
| 189 | + msg.clear_repeated_int64(); |
| 190 | + auto status = AddValueToRepeatedField(val, desc, &msg, &arena); |
| 191 | + benchmark::DoNotOptimize(status); |
| 192 | + } |
| 193 | +} |
| 194 | +BENCHMARK(BM_AddValueToRepeatedField_Int64); |
| 195 | + |
| 196 | +void BM_AddValueToRepeatedField_String(benchmark::State& state) { |
| 197 | + google::protobuf::Arena arena; |
| 198 | + TestAllTypes msg; |
| 199 | + const google::protobuf::FieldDescriptor* desc = |
| 200 | + TestAllTypes::descriptor()->FindFieldByName("repeated_string"); |
| 201 | + CelValue val = CelValue::CreateStringView("hello world"); |
| 202 | + |
| 203 | + for (auto _ : state) { |
| 204 | + msg.clear_repeated_string(); |
| 205 | + auto status = AddValueToRepeatedField(val, desc, &msg, &arena); |
| 206 | + benchmark::DoNotOptimize(status); |
| 207 | + } |
| 208 | +} |
| 209 | +BENCHMARK(BM_AddValueToRepeatedField_String); |
| 210 | + |
| 211 | +void BM_CreateValueFromRepeatedField_StringPiece(benchmark::State& state) { |
| 212 | + google::protobuf::Arena arena; |
| 213 | + TestAllTypes msg; |
| 214 | + msg.add_repeated_string_piece("hello world"); |
| 215 | + const google::protobuf::FieldDescriptor* desc = |
| 216 | + TestAllTypes::descriptor()->FindFieldByName("repeated_string_piece"); |
| 217 | + |
| 218 | + for (auto _ : state) { |
| 219 | + auto value = CreateValueFromRepeatedField( |
| 220 | + &msg, desc, 0, &CelProtoWrapper::InternalWrapMessage, &arena); |
| 221 | + benchmark::DoNotOptimize(value); |
| 222 | + } |
| 223 | +} |
| 224 | +BENCHMARK(BM_CreateValueFromRepeatedField_StringPiece); |
| 225 | + |
| 226 | +void BM_AddValueToRepeatedField_StringPiece(benchmark::State& state) { |
| 227 | + google::protobuf::Arena arena; |
| 228 | + TestAllTypes msg; |
| 229 | + const google::protobuf::FieldDescriptor* desc = |
| 230 | + TestAllTypes::descriptor()->FindFieldByName("repeated_string_piece"); |
| 231 | + CelValue val = CelValue::CreateStringView("hello world"); |
| 232 | + |
| 233 | + for (auto _ : state) { |
| 234 | + msg.clear_repeated_string_piece(); |
| 235 | + auto status = AddValueToRepeatedField(val, desc, &msg, &arena); |
| 236 | + benchmark::DoNotOptimize(status); |
| 237 | + } |
| 238 | +} |
| 239 | +BENCHMARK(BM_AddValueToRepeatedField_StringPiece); |
| 240 | + |
| 241 | +} // namespace |
| 242 | +} // namespace google::api::expr::runtime::internal |
0 commit comments