Files
Anders Eknert fc55be83e6 perf: json.patch + interning improvements (#8289)
The `json.patch` built-in is quite versatile, and compared to
patching via e.g. `object.union` et. al. often communicates
intent better, IMO. But while it uses some fairly advanced
logic for complex patch operations, it doesn't perform all that
great on simple ones. This is a first and pretty basic attempt
to improve that somewhat by picking the most low-hangig performance
fruits, like avoiding repeated allocations of temporary term pointers.

The main allocation source is the creation of EditTree's, and this
remains a problem. I have created a sync pool but only managed to
get the outermost edit tree to recycle, as I found it really hard
to track where it's safe to release those created in the deeply
nested calls. Additionally, I managed to trigger stack overflows
trying to recycle child trees, so there seems to be some circular
refs? Or I just did something wrong.

If someone wants to look into this and pick up where
I left, that'd be great!

- Add InternedIntRange for testing, primarily
- Intern keys used in json.patch patches
- Clean up json.X built-in benchmarks
- Reduce allocations in edit tree function
- Avoid using intermediate data structures
  for JSON patches
- Some unrelated interning fixes to reduce noise
  in tests and benchmarks (e.g. do less stuff in
  var inits)

Selected benchmark that I used while working on this:

**Before**
```
BenchmarkJSONPatchAddShallowScalar/object-10-16    147853      8008 ns/op    9667 B/op    206 allocs/op
BenchmarkJSONPatchAddShallowScalar/array-10-16     201704      5889 ns/op    7256 B/op    173 allocs/op
BenchmarkJSONPatchAddShallowScalar/set-10-16       182566      6733 ns/op    8103 B/op    156 allocs/op
```

**After**
```
BenchmarkJSONPatchAddShallowScalar/object-10-16    197414      6066 ns/op    7256 B/op    133 allocs/op
BenchmarkJSONPatchAddShallowScalar/array-10-16     278121      4427 ns/op    5285 B/op    100 allocs/op
BenchmarkJSONPatchAddShallowScalar/set-10-16       233884      4839 ns/op    6243 B/op    113 allocs/op
```

Signed-off-by: Anders Eknert <anders.eknert@apple.com>
2026-02-10 20:57:12 +00:00

153 lines
3.4 KiB
Go

// Copyright 2018 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package topdown
import (
"github.com/open-policy-agent/opa/v1/ast"
"github.com/open-policy-agent/opa/v1/topdown/builtins"
)
func builtinArrayConcat(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
arrA, err := builtins.ArrayOperand(operands[0].Value, 1)
if err != nil {
return err
}
arrB, err := builtins.ArrayOperand(operands[1].Value, 2)
if err != nil {
return err
}
if arrA.Len() == 0 {
return iter(operands[1])
}
if arrB.Len() == 0 {
return iter(operands[0])
}
arrC := make([]*ast.Term, arrA.Len()+arrB.Len())
i := 0
arrA.Foreach(func(elemA *ast.Term) {
arrC[i] = elemA
i++
})
arrB.Foreach(func(elemB *ast.Term) {
arrC[i] = elemB
i++
})
return iter(ast.ArrayTerm(arrC...))
}
func builtinArrayFlatten(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
arr, err := builtins.ArrayOperand(operands[0].Value, 1)
if err != nil {
return err
}
size := arr.Len()
preAlloc := size
containsArray := false
for i := range size {
if nested, ok := arr.Elem(i).Value.(*ast.Array); ok {
containsArray = true
preAlloc += nested.Len() - 1
}
}
if !containsArray && size == preAlloc {
return iter(operands[0]) // Empty array, or no nested arrays -> nothing to flatten.
}
flattened := make([]*ast.Term, 0, preAlloc)
for i := range size {
elem := arr.Elem(i)
if nested, ok := elem.Value.(*ast.Array); ok {
for j := range nested.Len() {
flattened = append(flattened, nested.Elem(j))
}
} else {
flattened = append(flattened, elem)
}
}
return iter(ast.ArrayTerm(flattened...))
}
func builtinArraySlice(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
arr, err := builtins.ArrayOperand(operands[0].Value, 1)
if err != nil {
return err
}
startIndex, err := builtins.IntOperand(operands[1].Value, 2)
if err != nil {
return err
}
stopIndex, err := builtins.IntOperand(operands[2].Value, 3)
if err != nil {
return err
}
l := arr.Len()
// Clamp stopIndex to avoid out-of-range errors. If negative, clamp to zero.
// Otherwise, clamp to length of array.
if stopIndex < 0 {
stopIndex = 0
} else if stopIndex > l {
stopIndex = l
}
// Clamp startIndex to avoid out-of-range errors. If negative, clamp to zero.
// Otherwise, clamp to stopIndex to avoid to avoid cases like arr[1:0].
if startIndex < 0 {
startIndex = 0
} else if startIndex > stopIndex {
startIndex = stopIndex
}
if startIndex == 0 && stopIndex >= l {
return iter(operands[0])
}
return iter(ast.NewTerm(arr.Slice(startIndex, stopIndex)))
}
func builtinArrayReverse(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
arr, err := builtins.ArrayOperand(operands[0].Value, 1)
if err != nil {
return err
}
length := arr.Len()
if length == 0 {
return iter(ast.InternedEmptyArray)
}
if length == 1 {
return iter(operands[0])
}
reversedArr := make([]*ast.Term, length)
for index := range length {
reversedArr[index] = arr.Elem(length - index - 1)
}
return iter(ast.ArrayTerm(reversedArr...))
}
func init() {
RegisterBuiltinFunc(ast.ArrayConcat.Name, builtinArrayConcat)
RegisterBuiltinFunc(ast.ArrayFlatten.Name, builtinArrayFlatten)
RegisterBuiltinFunc(ast.ArraySlice.Name, builtinArraySlice)
RegisterBuiltinFunc(ast.ArrayReverse.Name, builtinArrayReverse)
}