preallocating slices (#904)

* preallocated some slices when size is known

* gofmt

* gofmt
This commit is contained in:
Till Knuesting 2019-11-04 10:33:53 +00:00 committed by Asim Aslam
parent 2f3c251b00
commit 24b8d2a315
6 changed files with 8 additions and 8 deletions

View File

@ -171,7 +171,7 @@ func (n *Network) Routes(ctx context.Context, req *pbNet.RoutesRequest, resp *pb
return errors.InternalServerError("go.micro.network", "failed to list routes: %s", err) return errors.InternalServerError("go.micro.network", "failed to list routes: %s", err)
} }
var respRoutes []*pbRtr.Route respRoutes := make([]*pbRtr.Route, 0, len(routes))
for _, route := range routes { for _, route := range routes {
respRoute := &pbRtr.Route{ respRoute := &pbRtr.Route{
Service: route.Service, Service: route.Service,

View File

@ -95,7 +95,7 @@ func toNodes(routes []router.Route) []string {
} }
func toSlice(r map[uint64]router.Route) []router.Route { func toSlice(r map[uint64]router.Route) []router.Route {
var routes []router.Route routes := make([]router.Route, 0, len(r))
for _, v := range r { for _, v := range r {
routes = append(routes, v) routes = append(routes, v)
} }
@ -224,7 +224,7 @@ func (p *Proxy) cacheRoutes(service string) ([]router.Route, error) {
// refreshMetrics will refresh any metrics for our local cached routes. // refreshMetrics will refresh any metrics for our local cached routes.
// we may not receive new watch events for these as they change. // we may not receive new watch events for these as they change.
func (p *Proxy) refreshMetrics() { func (p *Proxy) refreshMetrics() {
var services []string services := make([]string, 0, len(p.Routes))
// get a list of services to update // get a list of services to update
p.RLock() p.RLock()

View File

@ -89,7 +89,7 @@ func (s *serviceRegistry) GetService(name string) ([]*registry.Service, error) {
return nil, err return nil, err
} }
var services []*registry.Service services := make([]*registry.Service, 0, len(rsp.Services))
for _, service := range rsp.Services { for _, service := range rsp.Services {
services = append(services, ToService(service)) services = append(services, ToService(service))
} }
@ -102,7 +102,7 @@ func (s *serviceRegistry) ListServices() ([]*registry.Service, error) {
return nil, err return nil, err
} }
var services []*registry.Service services := make([]*registry.Service, 0, len(rsp.Services))
for _, service := range rsp.Services { for _, service := range rsp.Services {
services = append(services, ToService(service)) services = append(services, ToService(service))
} }

View File

@ -123,7 +123,7 @@ func (w *workersKV) List() ([]*store.Record, error) {
return nil, errors.New(messages) return nil, errors.New(messages)
} }
var keys []string keys := make([]string, 0, len(a.Result))
for _, r := range a.Result { for _, r := range a.Result {
keys = append(keys, r.Name) keys = append(keys, r.Name)

View File

@ -32,7 +32,7 @@ func (e *ekv) Read(keys ...string) ([]*store.Record, error) {
values = append(values, keyval.Kvs...) values = append(values, keyval.Kvs...)
} }
var records []*store.Record records := make([]*store.Record, 0, len(values))
for _, kv := range values { for _, kv := range values {
records = append(records, &store.Record{ records = append(records, &store.Record{

View File

@ -1186,7 +1186,7 @@ func (t *tun) Links() []Link {
t.RLock() t.RLock()
defer t.RUnlock() defer t.RUnlock()
var links []Link links := make([]Link, 0, len(t.links))
for _, link := range t.links { for _, link := range t.links {
links = append(links, link) links = append(links, link)