Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: opa fmt 3x faster formatting #7341

Merged
merged 1 commit into from
Feb 6, 2025

Conversation

anderseknert
Copy link
Member

We have a Regal rule checking that a file is formatted, making the performance of the formatter more important than it normally might be.

Since I did some work on this in Regal recently, I was curious to see if this could be improved. Turned out to be a few bottlenecks that we could remove with great results. The Regal policies are possibly not representative for all kinds of Rego, and one thing that stands out is thay they have a lot of metadata annotations. Turned out that comment processing was the main bottleneck, so improving this meant a huge boost for formatting a typical Regal policy. The improvements here should make formatting of any file faster though.

BenchmarkFormatLargePolicy-10 main

382	   3064960 ns/op	 4573131 B/op	   26266 allocs/op

BenchmarkFormatLargePolicy-10 opa-fmt-perf

1396	    812859 ns/op	  362651 B/op	    8811 allocs/op

We have a Regal rule checking that a file is formatted, making the
performance of the formatter more important than it normally might be.

Since I did some work on this in Regal recently, I was curious to see
if this could be improved. Turned out to be a few bottlenecks that we
could remove with great results. The Regal policies are possibly not
representative for all kinds of Rego, and one thing that stands out is
thay they have a *lot* of metadata annotations. Turned out that comment
processing was the main bottleneck, so improving this meant a huge
boost for formatting a typical Regal policy. The improvements here
should make formatting of any file faster though.

**BenchmarkFormatLargePolicy-10 main**
```
382	   3064960 ns/op	 4573131 B/op	   26266 allocs/op
```

**BenchmarkFormatLargePolicy-10 opa-fmt-perf**
```
1396	    812859 ns/op	  362651 B/op	    8811 allocs/op
```

Signed-off-by: Anders Eknert <[email protected]>
@anderseknert
Copy link
Member Author

Might be woth pointing out to the casual observer — this makes the formatting part of opa fmt faster. The total cost of that command is however dominated by parsing the files. So outside of the formatter's Go API, the improvement is sadly more modest.

@anderseknert anderseknert merged commit cc4783a into open-policy-agent:main Feb 6, 2025
27 checks passed
@anderseknert anderseknert deleted the opa-fmt-perf branch February 6, 2025 09:38
@@ -281,7 +281,7 @@ func init() {
addRegoV0V1FlagWithDescription(formatCommand.Flags(), &fmtParams.regoV1, false, "format module(s) to be compatible with both Rego v0 and v1")
addV0CompatibleFlag(formatCommand.Flags(), &fmtParams.v0Compatible, false)
addV1CompatibleFlag(formatCommand.Flags(), &fmtParams.v1Compatible, false)
formatCommand.Flags().BoolVar(&fmtParams.checkResult, "check-result", true, "assert that the formatted code is valid and can be successfully parsed (default true)")
formatCommand.Flags().BoolVar(&fmtParams.checkResult, "check-result", true, "assert that the formatted code is valid and can be successfully parsed")
Copy link
Member Author

Choose a reason for hiding this comment

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

This was printed twice
Screenshot 2025-02-06 at 00 57 54

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