chore: Minor rbac memory optimization #7391
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What this does
RBAC roles are returned from a function to handle organization dynamic roles. The downside to this is that every time
Authorize()
is called, we have to allocate the Roles for said user.For most of our roles, the properties are static; owner, member, template/user-admin, and auditor. For these, we can share the same
Role
struct and values. This role is completely protected by the RBAC package, so no external packages can mutate these in any way.This is a cheap win. Further reduction in allocations is an ongoing effort.
Metrics
These metrics come from the newly added
StaticRoles
benchmark case. This case uses all the static roles for the actor. In production the benefits will not be as much since users still have organization roles which have the allocation penalty on each call.Before
The
Roles.Expand
for the Subject struct:After
The
Roles.Expand
for the Subject struct:Subject Caching
Subject.astValue()
copies all theRole.astValue()
s into a slice. So we still pay an allocation cost on eachauthorize
for a subject. I added the ability to cache the subject's ast value, so now each http.Request only pays the allocation cost once.All
dbauthz.AsSystem
use a cachedSubject
to prevent some allocs.This is not as large of an optimization, but it does help. Code was added to cache this in the same way as the roles. The savings of this is larger for larger roles.