-
Notifications
You must be signed in to change notification settings - Fork 896
chore: Optimize parial rego execution byte allocations #6144
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
Conversation
Identify time + alloc cost before optimizing
This reverts commit 0ef4b5c. This addition made a unit test fail with a supporting statemetn
@@ -166,6 +168,21 @@ func NewAuthorizer(registry prometheus.Registerer) *RegoAuthorizer { | |||
if err != nil { | |||
panic(xerrors.Errorf("compile rego: %w", err)) | |||
} | |||
|
|||
partialQuery, err = rego.New( |
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.
review: this is inside queryOnce.Do()
}), | ||
rego.Query("data.authz.allow = true"), | ||
rego.Module("policy.rego", policy), | ||
).PrepareForPartial(context.Background()) |
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.
review: I wondered whether it would make sense to pass through a higher-level context here (e.g. NewAuthorizer(ctx, registry)
) but the place we call NewAuthorizer
doesn't appear to have any application-level context already available.
Preventing some allocations by reusing the same prepared partial query. Unfortunately the bulk of the work happens when the inputs are passed in, which cannot be done in advance.
The unit tests that are most affected by this change are
Owner
role users. Since the partial execution results in a single empty query. Meaning most of the memory allocations happens before the input is passed in.Before
After
EMPTY POLICY
This is an illustration if
input
has 0 impact on the output prepared queries. I did this to illustrate the speedup of thePartial()
call on the rego policy.You can see the difference if you use an empty
policy.rego
so that theinputs
do not really affect the output. This shows that the impact ofinputs
is a magnitude larger than the compilation of the original policy.Before
After