-
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
Improve evaluator to detect and perform early-exit if possible #2092
Comments
This was raised in the recent GK security audit. |
The issue of side effects was brought up: technically, any one of the
👍 Those would be safe cases: anything iterated on from the base documents (the base doc portion of For general short-cutting the evaluation of virtual document rules, a new keyword mirroring allow {
any x { input.container[x] }
startswith(input.container[x].image, "example.com")
} It would have the effect that after the first ☝️ Picked "any" here for illustration purposes, and because another |
@srenatus I've filed an issue to improve the docs around If we make this assumption then the evaluator just has to know when it can short-circuit. Evaluation of complete docs where the value is constant is the simplest case though I could imagine others:
I'd think the analysis could be implemented in the compiler and the result could be cached. When the interpreter looks up rules during complete doc evaluation, it could check the compiler data structure to see if early-exit is allowed. If early-exit is allowed, it could stop iterating once a result was found. Perhaps the early-exit bit could be stored on the rule index lookup result. That would work for both cases of early exit (i.e., early exit on rule sets and early exit on iteration within a single rule.) |
Agreed! Let's add that warning and assume purity. |
🚧 #3898 |
We could do this in a follow-up, but for my understanding -- I currently think that while complete rules and functions are pretty analogous (wrt. their code changes), these would be different.... 🤔 But I also don't have a clear idea what we're aiming for here, so an example/sketch would be appreciated. |
Thoughts on the partial set membership early exit: when we have multiple p {
s[x]
x == 10
} we could early exit after the first binding of |
First steps have been merged 🎉 #3898 I'll create a few follow-up tickets, and reference them here. |
I'm going to close this issue now that we have landed early-exit support on functions and complete rules. |
Currently the evaluator does not implement any kind of "early-exit" (or short-circuit or find-one) behavior that stops evaluation when results are no longer needed. For example, given a rule set where all of the rules return the same value (e.g.,
allow = true { ... }
), OPA will evaluate every rule returned by the indexer. In some cases, it would be simple enough to detect and stop evaluation of subsequent rules once one result had been found. A simple heuristic to start would be complete rules where that assign the same value (under different conditions.) For example:Similarly, the evaluator should also be able to short-circuit any unnecessary backtracking in the case of iteration on data:
If a single match is found, no backtracking needs to be performed.
For debug/test purposes, there should be a flag that lets users disable this behavior.
Note this was raised long ago in #234 but never got implemented.
The text was updated successfully, but these errors were encountered: