mirror of
https://github.com/coder/coder.git
synced 2025-07-09 11:45:56 +00:00
chore: Optimize Filter() for small lists (#4282)
This commit is contained in:
@ -39,8 +39,26 @@ func Filter[O Objecter](ctx context.Context, auth Authorizer, subjID string, sub
|
|||||||
return objects, nil
|
return objects, nil
|
||||||
}
|
}
|
||||||
objectType := objects[0].RBACObject().Type
|
objectType := objects[0].RBACObject().Type
|
||||||
|
|
||||||
filtered := make([]O, 0)
|
filtered := make([]O, 0)
|
||||||
|
|
||||||
|
// Running benchmarks on this function, it is **always** faster to call
|
||||||
|
// auth.ByRoleName on <10 objects. This is because the overhead of
|
||||||
|
// 'PrepareByRoleName'. Once we cross 10 objects, then it starts to become
|
||||||
|
// faster
|
||||||
|
if len(objects) < 10 {
|
||||||
|
for _, o := range objects {
|
||||||
|
rbacObj := o.RBACObject()
|
||||||
|
if rbacObj.Type != objectType {
|
||||||
|
return nil, xerrors.Errorf("object types must be uniform across the set (%s), found %s", objectType, rbacObj)
|
||||||
|
}
|
||||||
|
err := auth.ByRoleName(ctx, subjID, subjRoles, scope, action, o.RBACObject())
|
||||||
|
if err == nil {
|
||||||
|
filtered = append(filtered, o)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return filtered, nil
|
||||||
|
}
|
||||||
|
|
||||||
prepared, err := auth.PrepareByRoleName(ctx, subjID, subjRoles, scope, action, objectType)
|
prepared, err := auth.PrepareByRoleName(ctx, subjID, subjRoles, scope, action, objectType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, xerrors.Errorf("prepare: %w", err)
|
return nil, xerrors.Errorf("prepare: %w", err)
|
||||||
|
Reference in New Issue
Block a user