mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
perf: avoid allocating in binary and/or when possible (#8689)
Avoid allocation in `&` and `|` calls when either of the operands is an empty set. Signed-off-by: Anders Eknert <anders.eknert@apple.com>
This commit is contained in:
+12
-2
@@ -10,7 +10,6 @@ import (
|
||||
)
|
||||
|
||||
func builtinBinaryAnd(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
|
||||
s1, err := builtins.SetOperand(operands[0].Value, 1)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -21,6 +20,10 @@ func builtinBinaryAnd(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Ter
|
||||
return err
|
||||
}
|
||||
|
||||
if s1.Len() == 0 || s2.Len() == 0 {
|
||||
return iter(ast.InternedEmptySet)
|
||||
}
|
||||
|
||||
i := s1.Intersect(s2)
|
||||
if i.Len() == 0 {
|
||||
return iter(ast.InternedEmptySet)
|
||||
@@ -30,7 +33,6 @@ func builtinBinaryAnd(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Ter
|
||||
}
|
||||
|
||||
func builtinBinaryOr(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
|
||||
|
||||
s1, err := builtins.SetOperand(operands[0].Value, 1)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -41,6 +43,14 @@ func builtinBinaryOr(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term
|
||||
return err
|
||||
}
|
||||
|
||||
if s1.Len() == 0 {
|
||||
return iter(operands[1])
|
||||
}
|
||||
|
||||
if s2.Len() == 0 {
|
||||
return iter(operands[0])
|
||||
}
|
||||
|
||||
return iter(ast.NewTerm(s1.Union(s2)))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user