Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d8008de

Browse files
authored
chore: Optimize Filter() for small lists (#4282)
1 parent 69c73b2 commit d8008de

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

coderd/rbac/authz.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,26 @@ func Filter[O Objecter](ctx context.Context, auth Authorizer, subjID string, sub
3939
return objects, nil
4040
}
4141
objectType := objects[0].RBACObject().Type
42-
4342
filtered := make([]O, 0)
43+
44+
// Running benchmarks on this function, it is **always** faster to call
45+
// auth.ByRoleName on <10 objects. This is because the overhead of
46+
// 'PrepareByRoleName'. Once we cross 10 objects, then it starts to become
47+
// faster
48+
if len(objects) < 10 {
49+
for _, o := range objects {
50+
rbacObj := o.RBACObject()
51+
if rbacObj.Type != objectType {
52+
return nil, xerrors.Errorf("object types must be uniform across the set (%s), found %s", objectType, rbacObj)
53+
}
54+
err := auth.ByRoleName(ctx, subjID, subjRoles, scope, action, o.RBACObject())
55+
if err == nil {
56+
filtered = append(filtered, o)
57+
}
58+
}
59+
return filtered, nil
60+
}
61+
4462
prepared, err := auth.PrepareByRoleName(ctx, subjID, subjRoles, scope, action, objectType)
4563
if err != nil {
4664
return nil, xerrors.Errorf("prepare: %w", err)

0 commit comments

Comments
 (0)