auth: pre-allocate slices in store

Signed-off-by: Gyu-Ho Lee <gyuhox@gmail.com>
release-3.3
Gyu-Ho Lee 2017-11-06 09:16:15 -08:00
parent ba233e2f4d
commit 568b856be8
1 changed files with 16 additions and 26 deletions

View File

@ -484,15 +484,13 @@ func (as *authStore) UserList(r *pb.AuthUserListRequest) (*pb.AuthUserListRespon
tx.Lock() tx.Lock()
defer tx.Unlock() defer tx.Unlock()
var resp pb.AuthUserListResponse
users := getAllUsers(tx) users := getAllUsers(tx)
for _, u := range users { resp := &pb.AuthUserListResponse{Users: make([]string, len(users))}
resp.Users = append(resp.Users, string(u.Name)) for i := range users {
resp.Users[i] = string(users[i].Name)
} }
return resp, nil
return &resp, nil
} }
func (as *authStore) UserRevokeRole(r *pb.AuthUserRevokeRoleRequest) (*pb.AuthUserRevokeRoleResponse, error) { func (as *authStore) UserRevokeRole(r *pb.AuthUserRevokeRoleRequest) (*pb.AuthUserRevokeRoleResponse, error) {
@ -555,15 +553,13 @@ func (as *authStore) RoleList(r *pb.AuthRoleListRequest) (*pb.AuthRoleListRespon
tx.Lock() tx.Lock()
defer tx.Unlock() defer tx.Unlock()
var resp pb.AuthRoleListResponse
roles := getAllRoles(tx) roles := getAllRoles(tx)
for _, r := range roles { resp := &pb.AuthRoleListResponse{Roles: make([]string, len(roles))}
resp.Roles = append(resp.Roles, string(r.Name)) for i := range roles {
resp.Roles[i] = string(roles[i].Name)
} }
return resp, nil
return &resp, nil
} }
func (as *authStore) RoleRevokePermission(r *pb.AuthRoleRevokePermissionRequest) (*pb.AuthRoleRevokePermissionResponse, error) { func (as *authStore) RoleRevokePermission(r *pb.AuthRoleRevokePermissionRequest) (*pb.AuthRoleRevokePermissionResponse, error) {
@ -823,18 +819,15 @@ func getAllUsers(tx backend.BatchTx) []*authpb.User {
return nil return nil
} }
var users []*authpb.User users := make([]*authpb.User, len(vs))
for i := range vs {
for _, v := range vs {
user := &authpb.User{} user := &authpb.User{}
err := user.Unmarshal(v) err := user.Unmarshal(vs[i])
if err != nil { if err != nil {
plog.Panicf("failed to unmarshal user struct: %s", err) plog.Panicf("failed to unmarshal user struct: %s", err)
} }
users[i] = user
users = append(users, user)
} }
return users return users
} }
@ -870,18 +863,15 @@ func getAllRoles(tx backend.BatchTx) []*authpb.Role {
return nil return nil
} }
var roles []*authpb.Role roles := make([]*authpb.Role, len(vs))
for i := range vs {
for _, v := range vs {
role := &authpb.Role{} role := &authpb.Role{}
err := role.Unmarshal(v) err := role.Unmarshal(vs[i])
if err != nil { if err != nil {
plog.Panicf("failed to unmarshal role struct: %s", err) plog.Panicf("failed to unmarshal role struct: %s", err)
} }
roles[i] = role
roles = append(roles, role)
} }
return roles return roles
} }