Skip to content

Commit d8fb286

Browse files
CEL Dev Teamcopybara-github
authored andcommitted
Accumulate unknowns into a set to avoid intermediate duplication.
PiperOrigin-RevId: 831492436
1 parent 4cacabb commit d8fb286

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

runtime/src/main/java/dev/cel/runtime/AccumulatedUnknowns.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
import java.util.ArrayList;
1919
import java.util.Arrays;
2020
import java.util.Collection;
21-
import java.util.List;
21+
import java.util.HashSet;
22+
import java.util.Set;
2223

2324
/**
2425
* An internal representation used for fast accumulation of unknown expr IDs and attributes. For
@@ -27,14 +28,14 @@
2728
*/
2829
final class AccumulatedUnknowns {
2930
private static final int MAX_UNKNOWN_ATTRIBUTE_SIZE = 500_000;
30-
private final List<Long> exprIds;
31-
private final List<CelAttribute> attributes;
31+
private final Set<Long> exprIds;
32+
private final Set<CelAttribute> attributes;
3233

33-
List<Long> exprIds() {
34+
Set<Long> exprIds() {
3435
return exprIds;
3536
}
3637

37-
List<CelAttribute> attributes() {
38+
Set<CelAttribute> attributes() {
3839
return attributes;
3940
}
4041

@@ -55,19 +56,20 @@ static AccumulatedUnknowns create(Collection<Long> ids) {
5556
}
5657

5758
static AccumulatedUnknowns create(Collection<Long> exprIds, Collection<CelAttribute> attributes) {
58-
return new AccumulatedUnknowns(new ArrayList<>(exprIds), new ArrayList<>(attributes));
59+
return new AccumulatedUnknowns(new HashSet<>(exprIds), new HashSet<>(attributes));
5960
}
6061

6162
private static void enforceMaxAttributeSize(
62-
List<CelAttribute> lhsAttributes, List<CelAttribute> rhsAttributes) {
63-
int totalSize = lhsAttributes.size() + rhsAttributes.size();
64-
if (totalSize > MAX_UNKNOWN_ATTRIBUTE_SIZE) {
63+
Set<CelAttribute> lhsAttributes, Set<CelAttribute> rhsAttributes) {
64+
if (lhsAttributes.size() + rhsAttributes.size() > MAX_UNKNOWN_ATTRIBUTE_SIZE) {
6565
throw new IllegalArgumentException(
66-
"Exceeded maximum allowed unknown attributes: " + totalSize);
66+
String.format(
67+
"Exceeded maximum allowed unknown attributes when merging: %s, %s",
68+
lhsAttributes.size(), rhsAttributes.size()));
6769
}
6870
}
6971

70-
private AccumulatedUnknowns(List<Long> exprIds, List<CelAttribute> attributes) {
72+
private AccumulatedUnknowns(Set<Long> exprIds, Set<CelAttribute> attributes) {
7173
this.exprIds = exprIds;
7274
this.attributes = attributes;
7375
}

0 commit comments

Comments
 (0)