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
20 changes: 20 additions & 0 deletions base/attribute_matcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ class AttributeMatcherNode {
return children_ != nullptr || wildcard_ != nullptr;
}

// True if this is a partial match and the only child of this node is a
// wildcard and the wildcard has no children.
[[nodiscard]]
bool IsPartialWildcard() const {
return children_ == nullptr && wildcard_ != nullptr && wildcard_->IsFull();
}

[[nodiscard]]
bool IsFull() const {
return !IsPartial();
Expand Down Expand Up @@ -229,6 +236,13 @@ class [[nodiscard]] AttributeMatch {
return node_ != nullptr && node_->IsPartial();
}

// True if this is a partial match and the only possible next match is a
// wildcard and there is no next match after the wildcard.
[[nodiscard]]
bool IsPartialWildcard() const {
return node_ != nullptr && node_->IsPartialWildcard();
}

[[nodiscard]]
bool IsFull() const {
return node_ != nullptr && node_->IsFull();
Expand Down Expand Up @@ -381,6 +395,12 @@ class AttributeMatcher {
return !children_.empty();
}

friend void swap(AttributeMatcher& lhs, AttributeMatcher& rhs) {
using std::swap;
swap(lhs.children_, rhs.children_);
swap(lhs.attributes_, rhs.attributes_);
}

private:
struct AttributeCollector;

Expand Down
2 changes: 2 additions & 0 deletions common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -756,8 +756,10 @@ cc_library(
":unknown",
":value_kind",
"//base:attributes",
"//base:function_result",
"//base:function_result_set",
"//common/internal:byte_string",
"//common/internal:unknowns",
"//eval/internal:cel_value_equal",
"//eval/public:cel_value",
"//eval/public:message_wrapper",
Expand Down
18 changes: 18 additions & 0 deletions common/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@ load("@rules_cc//cc:cc_test.bzl", "cc_test")

package(default_visibility = ["//visibility:public"])

cc_library(
name = "attribute_trail",
srcs = ["attribute_trail.cc"],
hdrs = ["attribute_trail.h"],
deps = [
":unknowns",
"//base:attribute_matcher",
"//base:attributes",
"//common:value",
"//runtime/internal:errors",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:btree",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/strings:string_view",
"@com_google_absl//absl/types:span",
],
)

cc_library(
name = "unknowns",
srcs = ["unknowns.cc"],
Expand Down
190 changes: 190 additions & 0 deletions common/internal/attribute_trail.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "common/internal/attribute_trail.h"

#include <algorithm>
#include <cstdint>
#include <optional>

#include "absl/base/optimization.h"
#include "absl/container/btree_set.h"
#include "absl/log/absl_check.h"
#include "common/internal/unknowns.h"
#include "common/value.h"

namespace cel::common_internal {

void UnknownAccumulator::Add(const UnknownSet& value) {
empty_ = false;
if (value.root == nullptr) {
// Ugh, there is some client code intentionally using empty unknowns to
// trigger unknown handling and we also have some tests which do evil
// things. We should fix them.
ABSL_DCHECK(value.attributes == nullptr);
ABSL_DCHECK(value.functions == nullptr);
return;
}
if (ABSL_PREDICT_FALSE(value.root != tree_->Root())) {
// Slow path. Somebody returned an unknown value from a separate evaluation.
// This is technically not supported but handling it is better than
// crashing.
Add(tree_->Graft(value));
return;
}
// We perform adapting merging for performance reasons. If only two unknown
// sets (attribute or function) are added to the accumulator, we can trigger a
// faster path in UnknownAccumulator::Accumulate. This complexity exists to
// enable that.
if (value.attributes != nullptr) {
// Keep track of the largest set. If after merging the resulting set is the
// same size as the largest, they are equal.
if (largest_unknown_attribute_set_ != nullptr) {
if (value.attributes->size > largest_unknown_attribute_set_->size) {
largest_unknown_attribute_set_ = value.attributes;
}
} else {
largest_unknown_attribute_set_ = value.attributes;
}
// The adaptive merging shenanigans mentioned above. Keep track if we have
// one or two sets so we can trigger faster path handling.
if (unknown_attribute_set_pair_[0] == nullptr) {
unknown_attribute_set_pair_[0] = value.attributes;
} else if (unknown_attribute_set_pair_[1] == nullptr) {
unknown_attribute_set_pair_[1] = value.attributes;
} else {
if (!added_unknown_attribute_set_pair_) {
Add(unknown_attribute_set_pair_[0]);
Add(unknown_attribute_set_pair_[1]);
added_unknown_attribute_set_pair_ = true;
}
Add(value.attributes);
}
}
if (value.functions != nullptr) {
// Keep track of the largest set. If after merging the resulting set is the
// same size as the largest, they are equal.
if (largest_unknown_function_set_ != nullptr) {
if (value.functions->size > largest_unknown_function_set_->size) {
largest_unknown_function_set_ = value.functions;
}
} else {
largest_unknown_function_set_ = value.functions;
}
// The adaptive merging shenanigans mentioned above. Keep track if we have
// one or two sets so we can trigger faster path handling.
if (unknown_function_set_pair_[0] == nullptr) {
unknown_function_set_pair_[0] = value.functions;
} else if (unknown_function_set_pair_[1] == nullptr) {
unknown_function_set_pair_[1] = value.functions;
} else {
if (!added_unknown_function_set_pair_) {
Add(unknown_function_set_pair_[0]);
Add(unknown_function_set_pair_[1]);
added_unknown_function_set_pair_ = true;
}
Add(value.functions);
}
}
}

std::optional<UnknownValue> UnknownAccumulator::Accumulate() && {
if (IsEmpty()) {
return std::nullopt;
}
const UnknownAttributeSet* unknown_attribute_set;
if (unknown_attributes_.empty()) {
if (unknown_attribute_set_pair_[0] == nullptr) {
ABSL_DCHECK(unknown_attribute_set_pair_[1] == nullptr);
unknown_attribute_set = nullptr;
} else if (unknown_attribute_set_pair_[1] == nullptr) {
unknown_attribute_set = unknown_attribute_set_pair_[0];
} else {
// Two way merger can be done with no additional memory.
unknown_attribute_set = MergeUnknownAttributeSets(
unknown_attribute_set_pair_[0], unknown_attribute_set_pair_[1],
tree_->GetArena());
}
} else {
if (largest_unknown_attribute_set_ != nullptr &&
largest_unknown_attribute_set_->size == unknown_attributes_.size()) {
// We have more than 2 sets and the resulting set would have the same
// amount of entries as the largest we encountered, they are the same.
// Just return it.
unknown_attribute_set = largest_unknown_attribute_set_;
} else {
UnknownAttributeSet* mutable_unknown_attribute_set =
UnknownAttributeSet::Allocate(unknown_attributes_.size(),
tree_->GetArena());
mutable_unknown_attribute_set->max_depth = 0;
uint32_t i = 0;
for (UnknownAttributeNode* unknown_attribute : unknown_attributes_) {
mutable_unknown_attribute_set->nodes[i++] = unknown_attribute;
mutable_unknown_attribute_set->max_depth =
std::max(mutable_unknown_attribute_set->max_depth,
static_cast<uint32_t>(unknown_attribute->depth()));
}
unknown_attribute_set = mutable_unknown_attribute_set;
}
unknown_attributes_.clear();
}
const UnknownFunctionSet* unknown_function_set;
if (unknown_functions_.empty()) {
if (unknown_function_set_pair_[0] == nullptr) {
ABSL_DCHECK(unknown_function_set_pair_[1] == nullptr);
unknown_function_set = nullptr;
} else if (unknown_function_set_pair_[1] == nullptr) {
unknown_function_set = unknown_function_set_pair_[0];
} else {
// Two way merger can be done with no additional memory.
unknown_function_set = MergeUnknownFunctionSets(
unknown_function_set_pair_[0], unknown_function_set_pair_[1],
tree_->GetArena());
}
} else {
if (largest_unknown_function_set_ != nullptr &&
largest_unknown_function_set_->size == unknown_functions_.size()) {
// We have more than 2 sets and the resulting set would have the same
// amount of entries as the largest we encountered, they are the same.
// Just return it.
unknown_function_set = largest_unknown_function_set_;
} else {
UnknownFunctionSet* mutable_unknown_function_set =
UnknownFunctionSet::Allocate(unknown_functions_.size(),
tree_->GetArena());
uint32_t i = 0;
for (UnknownFunctionNode* unknown_function : unknown_functions_) {
mutable_unknown_function_set->nodes[i++] = unknown_function;
}
unknown_function_set = mutable_unknown_function_set;
}
unknown_functions_.clear();
}
return common_internal::MakeUnknownValue(tree_->Root(), unknown_attribute_set,
unknown_function_set);
}

void UnknownAccumulator::Add(const UnknownAttributeSet* set) {
for (uint32_t i = 0; i < set->size; ++i) {
unknown_attributes_.insert(set->nodes[i]);
}
}

void UnknownAccumulator::Add(const UnknownFunctionSet* set) {
for (uint32_t i = 0; i < set->size; ++i) {
unknown_functions_.insert(set->nodes[i]);
}
}

} // namespace cel::common_internal
Loading
Loading