Skip to content

Commit 04299ff

Browse files
CEL Dev Teamcopybara-github
authored andcommitted
Internal change for memory efficiency.
PiperOrigin-RevId: 951144028
1 parent 4bf59ba commit 04299ff

2 files changed

Lines changed: 40 additions & 8 deletions

File tree

runtime/function_registry.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,19 @@ FunctionRegistry::FindStaticOverloadsByArity(absl::string_view name,
145145
return matched_funcs;
146146
}
147147

148+
size_t count = 0;
149+
for (const auto& overload : overloads->second.static_overloads) {
150+
if (overload.descriptor->receiver_style() == receiver_style &&
151+
overload.descriptor->types().size() == arity) {
152+
++count;
153+
}
154+
}
155+
// per go/dram-cy we are OK trading CPU for RAM as long as it's net positive
156+
// in SWE terms
157+
if (count > 1) {
158+
matched_funcs.reserve(count);
159+
}
160+
148161
for (const auto& overload : overloads->second.static_overloads) {
149162
if (overload.descriptor->receiver_style() == receiver_style &&
150163
overload.descriptor->types().size() == arity) {

runtime/function_registry_test.cc

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ TEST(FunctionRegistryTest, InsertAndRetrieveLazyFunction) {
5959
cel::FunctionDescriptor lazy_function_desc{"LazyFunction", false, {}};
6060
FunctionRegistry registry;
6161
Activation activation;
62-
ASSERT_OK(registry.RegisterLazyFunction(lazy_function_desc));
62+
ASSERT_THAT(registry.RegisterLazyFunction(lazy_function_desc));
6363

6464
const auto descriptors =
6565
registry.FindLazyOverloads("LazyFunction", false, {});
@@ -72,7 +72,7 @@ TEST(FunctionRegistryTest, InsertAndRetrieveLazyFunction) {
7272
TEST(FunctionRegistryTest, LazyAndStaticFunctionShareDescriptorSpace) {
7373
FunctionRegistry registry;
7474
cel::FunctionDescriptor desc = ConstIntFunction::MakeDescriptor();
75-
ASSERT_OK(registry.RegisterLazyFunction(desc));
75+
ASSERT_THAT(registry.RegisterLazyFunction(desc));
7676

7777
absl::Status status = registry.Register(ConstIntFunction::MakeDescriptor(),
7878
std::make_unique<ConstIntFunction>());
@@ -82,7 +82,7 @@ TEST(FunctionRegistryTest, LazyAndStaticFunctionShareDescriptorSpace) {
8282
TEST(FunctionRegistryTest, FindStaticOverloadsReturns) {
8383
FunctionRegistry registry;
8484
cel::FunctionDescriptor desc = ConstIntFunction::MakeDescriptor();
85-
ASSERT_OK(registry.Register(desc, std::make_unique<ConstIntFunction>()));
85+
ASSERT_THAT(registry.Register(desc, std::make_unique<ConstIntFunction>()));
8686

8787
std::vector<cel::FunctionOverloadReference> overloads =
8888
registry.FindStaticOverloads(desc.name(), false, {});
@@ -95,13 +95,32 @@ TEST(FunctionRegistryTest, FindStaticOverloadsReturns) {
9595
<< "Expected single ConstFunction()";
9696
}
9797

98+
TEST(FunctionRegistryTest, FindStaticOverloadsByArityExactCapacity) {
99+
FunctionRegistry registry;
100+
ASSERT_THAT(registry.Register({"Func", false, {Kind::kInt64}},
101+
std::make_unique<ConstIntFunction>()),
102+
IsOk());
103+
ASSERT_THAT(registry.Register({"Func", false, {Kind::kDouble}},
104+
std::make_unique<ConstIntFunction>()),
105+
IsOk());
106+
ASSERT_THAT(registry.Register({"Func", false, {Kind::kBool}},
107+
std::make_unique<ConstIntFunction>()),
108+
IsOk());
109+
110+
std::vector<cel::FunctionOverloadReference> overloads =
111+
registry.FindStaticOverloadsByArity("Func", false, 1);
112+
EXPECT_EQ(overloads.size(), 3);
113+
EXPECT_EQ(overloads.size(), overloads.capacity());
114+
}
115+
98116
TEST(FunctionRegistryTest, ListFunctions) {
99117
cel::FunctionDescriptor lazy_function_desc{"LazyFunction", false, {}};
100118
FunctionRegistry registry;
101119

102-
ASSERT_OK(registry.RegisterLazyFunction(lazy_function_desc));
120+
ASSERT_THAT(registry.RegisterLazyFunction(lazy_function_desc));
103121
EXPECT_OK(registry.Register(ConstIntFunction::MakeDescriptor(),
104-
std::make_unique<ConstIntFunction>()));
122+
std::make_unique<ConstIntFunction>()),
123+
IsOk());
105124

106125
auto registered_functions = registry.ListFunctions();
107126

@@ -217,7 +236,7 @@ TEST_P(NonStrictRegistrationFailTest,
217236
/*receiver_style=*/false, {Kind::kAny},
218237
/*is_strict=*/true);
219238
if (existing_function_is_lazy) {
220-
ASSERT_OK(registry.RegisterLazyFunction(descriptor));
239+
ASSERT_THAT(registry.RegisterLazyFunction(descriptor));
221240
} else {
222241
ASSERT_OK(
223242
registry.Register(descriptor, std::make_unique<ConstIntFunction>()));
@@ -246,7 +265,7 @@ TEST_P(NonStrictRegistrationFailTest,
246265
/*receiver_style=*/false, {Kind::kAny},
247266
/*is_strict=*/false);
248267
if (existing_function_is_lazy) {
249-
ASSERT_OK(registry.RegisterLazyFunction(descriptor));
268+
ASSERT_THAT(registry.RegisterLazyFunction(descriptor));
250269
} else {
251270
ASSERT_OK(
252271
registry.Register(descriptor, std::make_unique<ConstIntFunction>()));
@@ -274,7 +293,7 @@ TEST_P(NonStrictRegistrationFailTest, CanRegisterStrictFunctionsWithoutLimit) {
274293
/*receiver_style=*/false, {Kind::kAny},
275294
/*is_strict=*/true);
276295
if (existing_function_is_lazy) {
277-
ASSERT_OK(registry.RegisterLazyFunction(descriptor));
296+
ASSERT_THAT(registry.RegisterLazyFunction(descriptor));
278297
} else {
279298
ASSERT_OK(
280299
registry.Register(descriptor, std::make_unique<ConstIntFunction>()));

0 commit comments

Comments
 (0)