/proc/self/cwd/common/internal/attribute_trail.cc
Line | Count | Source |
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 "common/internal/attribute_trail.h" |
16 | | |
17 | | #include <algorithm> |
18 | | #include <cstdint> |
19 | | #include <optional> |
20 | | |
21 | | #include "absl/base/optimization.h" |
22 | | #include "absl/container/btree_set.h" |
23 | | #include "absl/log/absl_check.h" |
24 | | #include "common/internal/unknowns.h" |
25 | | #include "common/value.h" |
26 | | |
27 | | namespace cel::common_internal { |
28 | | |
29 | 0 | void UnknownAccumulator::Add(const UnknownSet& value) { |
30 | 0 | empty_ = false; |
31 | 0 | if (value.root == nullptr) { |
32 | | // Ugh, there is some client code intentionally using empty unknowns to |
33 | | // trigger unknown handling and we also have some tests which do evil |
34 | | // things. We should fix them. |
35 | 0 | ABSL_DCHECK(value.attributes == nullptr); |
36 | 0 | ABSL_DCHECK(value.functions == nullptr); |
37 | 0 | return; |
38 | 0 | } |
39 | 0 | if (ABSL_PREDICT_FALSE(value.root != tree_->Root())) { |
40 | | // Slow path. Somebody returned an unknown value from a separate evaluation. |
41 | | // This is technically not supported but handling it is better than |
42 | | // crashing. |
43 | 0 | Add(tree_->Graft(value)); |
44 | 0 | return; |
45 | 0 | } |
46 | | // We perform adapting merging for performance reasons. If only two unknown |
47 | | // sets (attribute or function) are added to the accumulator, we can trigger a |
48 | | // faster path in UnknownAccumulator::Accumulate. This complexity exists to |
49 | | // enable that. |
50 | 0 | if (value.attributes != nullptr) { |
51 | | // Keep track of the largest set. If after merging the resulting set is the |
52 | | // same size as the largest, they are equal. |
53 | 0 | if (largest_unknown_attribute_set_ != nullptr) { |
54 | 0 | if (value.attributes->size > largest_unknown_attribute_set_->size) { |
55 | 0 | largest_unknown_attribute_set_ = value.attributes; |
56 | 0 | } |
57 | 0 | } else { |
58 | 0 | largest_unknown_attribute_set_ = value.attributes; |
59 | 0 | } |
60 | | // The adaptive merging shenanigans mentioned above. Keep track if we have |
61 | | // one or two sets so we can trigger faster path handling. |
62 | 0 | if (unknown_attribute_set_pair_[0] == nullptr) { |
63 | 0 | unknown_attribute_set_pair_[0] = value.attributes; |
64 | 0 | } else if (unknown_attribute_set_pair_[1] == nullptr) { |
65 | 0 | unknown_attribute_set_pair_[1] = value.attributes; |
66 | 0 | } else { |
67 | 0 | if (!added_unknown_attribute_set_pair_) { |
68 | 0 | Add(unknown_attribute_set_pair_[0]); |
69 | 0 | Add(unknown_attribute_set_pair_[1]); |
70 | 0 | added_unknown_attribute_set_pair_ = true; |
71 | 0 | } |
72 | 0 | Add(value.attributes); |
73 | 0 | } |
74 | 0 | } |
75 | 0 | if (value.functions != nullptr) { |
76 | | // Keep track of the largest set. If after merging the resulting set is the |
77 | | // same size as the largest, they are equal. |
78 | 0 | if (largest_unknown_function_set_ != nullptr) { |
79 | 0 | if (value.functions->size > largest_unknown_function_set_->size) { |
80 | 0 | largest_unknown_function_set_ = value.functions; |
81 | 0 | } |
82 | 0 | } else { |
83 | 0 | largest_unknown_function_set_ = value.functions; |
84 | 0 | } |
85 | | // The adaptive merging shenanigans mentioned above. Keep track if we have |
86 | | // one or two sets so we can trigger faster path handling. |
87 | 0 | if (unknown_function_set_pair_[0] == nullptr) { |
88 | 0 | unknown_function_set_pair_[0] = value.functions; |
89 | 0 | } else if (unknown_function_set_pair_[1] == nullptr) { |
90 | 0 | unknown_function_set_pair_[1] = value.functions; |
91 | 0 | } else { |
92 | 0 | if (!added_unknown_function_set_pair_) { |
93 | 0 | Add(unknown_function_set_pair_[0]); |
94 | 0 | Add(unknown_function_set_pair_[1]); |
95 | 0 | added_unknown_function_set_pair_ = true; |
96 | 0 | } |
97 | 0 | Add(value.functions); |
98 | 0 | } |
99 | 0 | } |
100 | 0 | } |
101 | | |
102 | 0 | std::optional<UnknownValue> UnknownAccumulator::Accumulate() && { |
103 | 0 | if (IsEmpty()) { |
104 | 0 | return std::nullopt; |
105 | 0 | } |
106 | 0 | const UnknownAttributeSet* unknown_attribute_set; |
107 | 0 | if (unknown_attributes_.empty()) { |
108 | 0 | if (unknown_attribute_set_pair_[0] == nullptr) { |
109 | 0 | ABSL_DCHECK(unknown_attribute_set_pair_[1] == nullptr); |
110 | 0 | unknown_attribute_set = nullptr; |
111 | 0 | } else if (unknown_attribute_set_pair_[1] == nullptr) { |
112 | 0 | unknown_attribute_set = unknown_attribute_set_pair_[0]; |
113 | 0 | } else { |
114 | | // Two way merger can be done with no additional memory. |
115 | 0 | unknown_attribute_set = MergeUnknownAttributeSets( |
116 | 0 | unknown_attribute_set_pair_[0], unknown_attribute_set_pair_[1], |
117 | 0 | tree_->GetArena()); |
118 | 0 | } |
119 | 0 | } else { |
120 | 0 | if (largest_unknown_attribute_set_ != nullptr && |
121 | 0 | largest_unknown_attribute_set_->size == unknown_attributes_.size()) { |
122 | | // We have more than 2 sets and the resulting set would have the same |
123 | | // amount of entries as the largest we encountered, they are the same. |
124 | | // Just return it. |
125 | 0 | unknown_attribute_set = largest_unknown_attribute_set_; |
126 | 0 | } else { |
127 | 0 | UnknownAttributeSet* mutable_unknown_attribute_set = |
128 | 0 | UnknownAttributeSet::Allocate(unknown_attributes_.size(), |
129 | 0 | tree_->GetArena()); |
130 | 0 | mutable_unknown_attribute_set->max_depth = 0; |
131 | 0 | uint32_t i = 0; |
132 | 0 | for (UnknownAttributeNode* unknown_attribute : unknown_attributes_) { |
133 | 0 | mutable_unknown_attribute_set->nodes[i++] = unknown_attribute; |
134 | 0 | mutable_unknown_attribute_set->max_depth = |
135 | 0 | std::max(mutable_unknown_attribute_set->max_depth, |
136 | 0 | static_cast<uint32_t>(unknown_attribute->depth())); |
137 | 0 | } |
138 | 0 | unknown_attribute_set = mutable_unknown_attribute_set; |
139 | 0 | } |
140 | 0 | unknown_attributes_.clear(); |
141 | 0 | } |
142 | 0 | const UnknownFunctionSet* unknown_function_set; |
143 | 0 | if (unknown_functions_.empty()) { |
144 | 0 | if (unknown_function_set_pair_[0] == nullptr) { |
145 | 0 | ABSL_DCHECK(unknown_function_set_pair_[1] == nullptr); |
146 | 0 | unknown_function_set = nullptr; |
147 | 0 | } else if (unknown_function_set_pair_[1] == nullptr) { |
148 | 0 | unknown_function_set = unknown_function_set_pair_[0]; |
149 | 0 | } else { |
150 | | // Two way merger can be done with no additional memory. |
151 | 0 | unknown_function_set = MergeUnknownFunctionSets( |
152 | 0 | unknown_function_set_pair_[0], unknown_function_set_pair_[1], |
153 | 0 | tree_->GetArena()); |
154 | 0 | } |
155 | 0 | } else { |
156 | 0 | if (largest_unknown_function_set_ != nullptr && |
157 | 0 | largest_unknown_function_set_->size == unknown_functions_.size()) { |
158 | | // We have more than 2 sets and the resulting set would have the same |
159 | | // amount of entries as the largest we encountered, they are the same. |
160 | | // Just return it. |
161 | 0 | unknown_function_set = largest_unknown_function_set_; |
162 | 0 | } else { |
163 | 0 | UnknownFunctionSet* mutable_unknown_function_set = |
164 | 0 | UnknownFunctionSet::Allocate(unknown_functions_.size(), |
165 | 0 | tree_->GetArena()); |
166 | 0 | uint32_t i = 0; |
167 | 0 | for (UnknownFunctionNode* unknown_function : unknown_functions_) { |
168 | 0 | mutable_unknown_function_set->nodes[i++] = unknown_function; |
169 | 0 | } |
170 | 0 | unknown_function_set = mutable_unknown_function_set; |
171 | 0 | } |
172 | 0 | unknown_functions_.clear(); |
173 | 0 | } |
174 | 0 | return common_internal::MakeUnknownValue(tree_->Root(), unknown_attribute_set, |
175 | 0 | unknown_function_set); |
176 | 0 | } |
177 | | |
178 | 0 | void UnknownAccumulator::Add(const UnknownAttributeSet* set) { |
179 | 0 | for (uint32_t i = 0; i < set->size; ++i) { |
180 | 0 | unknown_attributes_.insert(set->nodes[i]); |
181 | 0 | } |
182 | 0 | } |
183 | | |
184 | 0 | void UnknownAccumulator::Add(const UnknownFunctionSet* set) { |
185 | 0 | for (uint32_t i = 0; i < set->size; ++i) { |
186 | 0 | unknown_functions_.insert(set->nodes[i]); |
187 | 0 | } |
188 | 0 | } |
189 | | |
190 | | } // namespace cel::common_internal |