Skip to content

Optimize and improve code for built-in functions#1788

Merged
anderseknert merged 1 commit intoopen-policy-agent:mainfrom
anderseknert:safety-theatre-opt-out
Dec 2, 2025
Merged

Optimize and improve code for built-in functions#1788
anderseknert merged 1 commit intoopen-policy-agent:mainfrom
anderseknert:safety-theatre-opt-out

Conversation

@anderseknert
Copy link
Member

  • Register Regal's built-in functions globally only once and in a single location. The various rego APIs for working with custom functions haven't been a great experience for us to work with, and consolidating our code for this simplifies things quite a bit. Note that we've tried to do this in the past but never got it quite working. Seems like it does now.
  • Set CanSkipBctx to true on all the OPA built-in functions that we use internally, as we don't need the cancellation or inter-query caches, and building the context for each call isn't cheap.
  • Pass a no-op external cancel function to eval to avoid having OPA start one per each file linted.

While working on this I also found an optimization opportunity in OPA, which I will submit a PR for too.

* Register Regal's built-in functions globally only once and in a
  single location. The various `rego` APIs for working with custom
  functions haven't been a great experience for us to work with, and
  consolidating our code for this simplifies things quite a bit. Note
  that we've tried to do this in the past but never got it quite working.
  Seems like it does now.
* Set `CanSkipBctx` to `true` on all the OPA built-in functions that we
  use internally, as we don't need the cancellation or inter-query caches,
  and building the context for each call isn't cheap.
* Pass a no-op external cancel function to eval to avoid having OPA start
  one per each file linted.

While working on this I also found an optimization opportunity in OPA, which
I will submit a PR for too.

Signed-off-by: Anders Eknert <anders.eknert@apple.com>
@anderseknert anderseknert force-pushed the safety-theatre-opt-out branch from 1661373 to 88e637b Compare December 2, 2025 15:10
"github.com/open-policy-agent/regal/pkg/config"
"github.com/open-policy-agent/regal/pkg/roast/transform"

_ "github.com/open-policy-agent/regal/pkg/builtins"
Copy link
Contributor

Choose a reason for hiding this comment

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

We have this required now in many places, what guards are there (or could there be) for in future if we need this _ import but forget to do 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.

Since they are functions, missing this import would typically result in OPA not compiling a policy that makes use of them. Certainly a risk, and you're right this is hard to future-proof, but at least it should fail rather loudly.

I believe the best remedy is to have a single entry point for all evaluation throughout the Regal codebase. As you know, I've been rather conscious about this already, and we're now down to... 3 or so? But ideally only a single Regal module wraps rego1 directly, and any code doing evals route through that. At that point, it should be simple to assert programmatically that no other code imports rego directly.

Footnotes

  1. Tbh, I'm increasingly considering the feasibility of using the even more "low level" query API (which rego is largely a wrapper around) directly in Regal, as I think we're well past the point of convenience being more important than control. And as I think this PR demonstrates, the convenience is often quite inconvenient (for our use case).

@anderseknert anderseknert merged commit 9e43c50 into open-policy-agent:main Dec 2, 2025
8 checks passed
@anderseknert anderseknert deleted the safety-theatre-opt-out branch December 2, 2025 20:04
// While this comes with some overhead, it too is rather insignificant. It does however mean
// that the type of multi-threaded evaluations we typically in the linter will have pprof
// report 99% of all "blocked" time here, making it much harder to identify blocking we do
// ourselves / care about.
Copy link
Contributor

Choose a reason for hiding this comment

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

Still sounds like something we could fix upstream. "unreadable pprof output" sounds like reason enough

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed. This was just an easier fix for the time being :)

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.

3 participants