Skip to content

compiler: replace true expr when appending to empty body - #8299

Merged
anderseknert merged 1 commit into
open-policy-agent:mainfrom
anderseknert:compiler-remove-true
Feb 6, 2026
Merged

anderseknert merged 1 commit into
open-policy-agent:mainfrom
anderseknert:compiler-remove-true

Conversation

@anderseknert

Copy link
Copy Markdown
Member

The OPA parser places a single boolean true term inside an expression to populate empty rule bodies, so that e.g. x := input.x parses as:

x := input.x if {
    true
}

When the compiler rewrites rules in its various stages, it would previously ignore such "true expressions" and simply append what it rewrote as the next expression:

x := __local1__ if {
    true
    __local1__ = input.x
}

This is of course redundant, as the only purpose of the true expr was to mark the body as empty — which it no longer is! While fairly harmless, this contributes noise to the compiler's output, which is then passed onwards to topdown evaluation. Not a huge deal, but measurable!

BenchmarkRegalLintingItselfPrepareOnce

502062750 ns/op    1900946509 B/op    45426050 allocs/op
496509250 ns/op    1893094357 B/op    45112660 allocs/op

With this change, the compiler now replaces an empty true expression with a new one whenever we append to a body. Meaning true remains to mark empty bodies, but not in non-empty ones.

This is more about elegance than performance though :)

@netlify

netlify Bot commented Feb 6, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for openpolicyagent ready!

Name Link
🔨 Latest commit 0d32e83
🔍 Latest deploy log https://app.netlify.com/projects/openpolicyagent/deploys/6985babc21976c0008ec1719
😎 Deploy Preview https://deploy-preview-8299--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.

@srenatus srenatus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Solid choice. I guess one step further would be to just accept empty bodies. No conditions == true, actually putting a true there was a bit of a hack in the first place. 🤔

Comment thread v1/util/performance.go Resolved
@anderseknert

Copy link
Copy Markdown
Member Author

@srenatus indeed. I guess there's some risk that external tools working with the AST would break, but a good next step would IMHO be to ensure that OPA handles nil bodies anywhere they may appear. Once we do, we could make it a parser option to leave empty bodies be, and have OPA use that internally anywhere the AST isn't exposed. And have external tools benefit should they choose to.

The OPA parser places a single boolean true term inside an expression
to populate empty rule bodies, so that e.g. `x := input.x` parses as:

```rego
x := input.x if {
    true
}
```

When the compiler rewrites rules in its various stages, it would
previously ignore such "true expressions" and simply append what
it rewrote as the next expression:

```
x := __local1__ if {
    true
    __local1__ = input.x
}
```

This is of course redundant, as the only purpose of the `true` expr
was to mark the body as empty — which it no longer is! While fairly
harmless, this contributes noise to the compiler's output, which is
then passed onwards to topdown evaluation. Not a huge deal, but
measurable!

**BenchmarkRegalLintingItselfPrepareOnce**
```
502062750 ns/op    1900946509 B/op    45426050 allocs/op
496509250 ns/op    1893094357 B/op    45112660 allocs/op
```

With this change, the compiler now replaces an empty true expression with
a new one whenever we append to a body. Meaning `true` remains to mark
empty bodies, but not in non-empty ones.

This is more about elegance than performance though :)

Signed-off-by: Anders Eknert <anders.eknert@apple.com>
@anderseknert
anderseknert enabled auto-merge (squash) February 6, 2026 09:59
@anderseknert
anderseknert merged commit 55683c7 into open-policy-agent:main Feb 6, 2026
34 checks passed
@anderseknert
anderseknert deleted the compiler-remove-true branch February 6, 2026 11:12
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