Skip to content

perf(policies): reuse the compiled rego query across evaluations - #3288

Open
fschade wants to merge 1 commit into
opencloud-eu:mainfrom
fschade:feat-modernize-opa
Open

perf(policies): reuse the compiled rego query across evaluations#3288
fschade wants to merge 1 commit into
opencloud-eu:mainfrom
fschade:feat-modernize-opa

Conversation

@fschade

@fschade fschade commented Aug 11, 2026

Copy link
Copy Markdown
Member

Description

as pointed out by @pbleser-oc we re-load the rego policies on every evaluation, on small instances that is not nice but also not noticeable. As soon as the user base and usage grows, the allocation are measurable.

This PR changes the handling of how we load rego policies, it keeps the compiled query which reduces the allocations by a measurable amount! After changing the rego, a server restart is needed.

it contains a bench test and the numbers prove the idea behind it.

2 policy files before after factor
time 690 us 45 us
memory 332 KB 20 KB 17x
allocations 6496 472 14x

take the numbers with a pinch of salt, its measured in my leb env!

How Has This Been Tested?

  • lab installation & load test
  • perf test
  • unit tests

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Technical debt
  • Tests only (no source changes)

@fschade
fschade requested review from butonic, micbar and pbleser-oc and removed request for butonic August 11, 2026 13:31
@codacy-production

codacy-production Bot commented Aug 11, 2026

Copy link
Copy Markdown

Not up to standards ⛔

🔴 Issues 5 critical · 39 minor

Alerts:
⚠ 44 issues (≤ 0 issues of at least minor severity)

Results:
44 new issues

Category Results
Security 5 critical
CodeStyle 39 minor

View in Codacy

🟢 Metrics 38 complexity · -7 duplication

Metric Results
Complexity 38
Duplication -7

View in Codacy

🟢 Coverage 78.38% diff coverage · +0.10% coverage variation

Metric Results
Coverage variation +0.10% coverage variation (-1.00%)
Diff coverage 78.38% diff coverage

View coverage diff in Codacy

Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (8504e29) 84010 19259 22.92%
Head commit (1e7cfaf) 84039 (+29) 19350 (+91) 23.03% (+0.10%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#3288) 74 58 78.38%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@fschade
fschade force-pushed the feat-modernize-opa branch from 15ee3b1 to 04616a0 Compare August 11, 2026 13:35
func NewOPA(timeout time.Duration, logger log.Logger, conf config.Engine) (*OPA, error) {
for _, path := range conf.Policies {
if info, err := os.Stat(path); err == nil && info.IsDir() {
return nil, fmt.Errorf("policy path %q is a directory, list the rego files instead", path)

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.

Why this?
When a directory is specified, the engine loads all the .rego files that are in that directory, which sounds like a really convenient option for admins, no?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

we can change that, till today only .rego files were allowed, you think it makes sense?

./**/*.rego or ./*.rego?

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.

It worked for me in my tests when just indicating a directory there, I believe the recursive .rego file loading is in the OPA engine, if you look at loadFiles in rego.go, and the allRec func in loader.go in the opa library.

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.

So there is nothing to add to be able to recurse on directories, the OPA library can already do that on its own, which is why I'm asking why we would restrict it from doing that through that IsDir() check there.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

rego never loads files, it just "imports" whats there

Comment thread services/policies/pkg/engine/opa/engine.go Outdated
@fschade
fschade force-pushed the feat-modernize-opa branch from 04616a0 to 5d7793e Compare August 11, 2026 14:25
Comment thread services/policies/pkg/engine/opa/engine.go Outdated
Comment thread services/policies/pkg/engine/opa/engine.go Outdated
@fschade
fschade force-pushed the feat-modernize-opa branch from 5d7793e to 134c6be Compare August 12, 2026 14:51
@fschade
fschade force-pushed the feat-modernize-opa branch from 134c6be to 1e7cfaf Compare August 12, 2026 15:01
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