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

Fix/virtual-cache ref unification scope #6957

Conversation

johanfylling
Copy link
Contributor

topdown: Adding scope component to virtual cache key to capture pre-eval ref unification

Fixes: #6926

…val ref unification

Fixes: open-policy-agent#6926
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
Copy link

netlify bot commented Aug 23, 2024

Deploy Preview for openpolicyagent ready!

Name Link
🔨 Latest commit 92852f5
🔍 Latest deploy log https://app.netlify.com/sites/openpolicyagent/deploys/66ceeeede5783a0008c05372
😎 Deploy Preview https://deploy-preview-6957--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 site configuration.

srenatus
srenatus previously approved these changes Aug 26, 2024
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.

I'm glad there are tests to show this does its thing 😄

Thanks!

topdown/eval.go Outdated
return 1
}

func (q vcKeyScope) Find(_ ast.Ref) (ast.Value, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit

Suggested change
func (q vcKeyScope) Find(_ ast.Ref) (ast.Value, error) {
func (vcKeyScope) Find(ast.Ref) (ast.Value, error) {

@@ -2861,6 +2898,82 @@ func (e evalVirtualPartial) evalCache(iter unifyIterator) (evalVirtualPartialCac
return hint, nil
}

// vcKeyScope represents the scoping that pre-rule-eval ref unification imposes on a virtual cache entry.
type vcKeyScope struct {
ast.Ref
Copy link
Contributor

Choose a reason for hiding this comment

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

Would we safe memory if we kept the index next to the ast.Ref?

type vcKeyScope struct {
  ref ast.Ref
  idx int
}

so that we can avoid copying the ast.Ref? I guess it doesn't make a huge difference 🤔

topdown/eval.go Outdated Show resolved Hide resolved
* Only skipping key scope element comparison if both elements are vars

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
Copy link
Member

@ashutosh-narkar ashutosh-narkar left a comment

Choose a reason for hiding this comment

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

This looks fine to me.

Comment on lines +2859 to 2876
if plugged.IsGround() && !scoping {
hintKeyEnd = i
hint.key = append(e.plugged[:i], plugged)
} else {
scoping = true
hl := len(hint.key)
if hl == 0 {
break
}
if scope, ok := hint.key[hl-1].Value.(vcKeyScope); ok {
scope.Ref = append(scope.Ref, plugged)
hint.key[len(hint.key)-1] = ast.NewTerm(scope)
} else {
scope = vcKeyScope{}
scope.Ref = append(scope.Ref, plugged)
hint.key = append(hint.key, ast.NewTerm(scope))
}
}
Copy link
Member

Choose a reason for hiding this comment

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

It would be helpful if we were to add a comment here explaining the code in the loop with an example if feasible. It would probably help a future reviewer.

@johanfylling johanfylling merged commit 5d08783 into open-policy-agent:main Aug 28, 2024
28 checks passed
@johanfylling johanfylling deleted the fix/virtual_cache_ref_unification_scope branch August 28, 2024 09:50
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.

Lost in iteration: values lost when iterating ref head rules mixing static and dynamic paths
3 participants