-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Fix/virtual-cache ref unification scope #6957
Conversation
…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>
✅ Deploy Preview for openpolicyagent ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
There was a problem hiding this 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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit
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 |
There was a problem hiding this comment.
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 🤔
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
* Only skipping key scope element comparison if both elements are vars Signed-off-by: Johan Fylling <johan.dev@fylling.se>
There was a problem hiding this 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.
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)) | ||
} | ||
} |
There was a problem hiding this comment.
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.
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
topdown: Adding scope component to virtual cache key to capture pre-eval ref unification
Fixes: #6926