Skip to content

Conversation

@anderseknert
Copy link
Member

@anderseknert anderseknert commented Oct 8, 2025

  • Use new util.SlicePool to avoid cost of temporary slices in formatTerm
  • Add SkipDefensiveCopying option and enable it for all Source* functions

BenchmarkFormatLargePolicy

676179 ns/op    995204 B/op    8850 allocs/op // regression in 0fb7526
513570 ns/op    378787 B/op    8775 allocs/op // addressed regression with sync.Pool
481681 ns/op    352130 B/op    7954 allocs/op // new util.NewSlicePool using only pointers
365116 ns/op    160528 B/op    2098 allocs/op // new SkipDefensiveCopying option

@netlify
Copy link

netlify bot commented Oct 8, 2025

Deploy Preview for openpolicyagent ready!

Name Link
🔨 Latest commit 4c81f4f
🔍 Latest deploy log https://app.netlify.com/projects/openpolicyagent/deploys/68e650ba55df0400089eb398
😎 Deploy Preview https://deploy-preview-7967--openpolicyagent.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link
Contributor

@srenatus srenatus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice reduction! 👏 Thanks

const defaultLocationFile = "__format_default__"

var (
elseVar ast.Value = ast.Var("else")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm the indentation suggests that it's all ast.Value below. But that's not the case, is it? 💭

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I did not think of that. Clearly our formatter is not the only one having some issues ;) I'll add a newline in between that and the rest to avoid this.


// saveComments saves a copy of the comments slice in a pooled slice to and returns it.
// This is to avoid having to create a new slice every time we need to save comments.
// The caller is responsible for putting the slice back in the pool when done.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This struck me as slightly odd, but since we're not using this in many places, it's probably fine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this not how all sync pools work? I guess one option now that we have a slice pool in our tool belt could be to add something like a WithSlice(func(s []T) error) function to the SlicePool struct, making it possible to do what you need with an ephemeral slice within the function body and not have to worry about returning it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose I just stumbled over the helper method for getting stuff out of the pool, without a second helper for returning it. We could return a closure, but it's not important, LGTM.

- Use new `util.SlicePool` to avoid cost of temporary slices in `formatTerm`
- Add `SkipDefensiveCopying` option and enable it for all `Source`* functions

```
676179 ns/op    995204 B/op    8850 allocs/op // regression in 0fb7526
513570 ns/op    378787 B/op    8775 allocs/op // addressed regression with sync.Pool
481681 ns/op    352130 B/op    7954 allocs/op // new util.NewSlicePool using only pointers
365116 ns/op    160528 B/op    2098 allocs/op // new SkipDefensiveCopying option
```

Signed-off-by: Anders Eknert <[email protected]>
@anderseknert anderseknert merged commit c122df1 into open-policy-agent:main Oct 8, 2025
55 of 57 checks passed
@anderseknert anderseknert deleted the formatter-perf branch October 8, 2025 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants