1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-24 10:07:04 +02:00

preallocated slices (#917)

This commit is contained in:
Till Knuesting 2019-11-05 17:43:12 +00:00 committed by Asim Aslam
parent 4a694c9d02
commit f67c5e779f
10 changed files with 10 additions and 10 deletions

View File

@ -101,7 +101,7 @@ func (r *routerSelector) getRoutes(service string) ([]router.Route, error) {
return nil, selector.ErrNoneAvailable
}
var routes []router.Route
routes := make([]router.Route, 0, len(pbRoutes.Routes))
// convert from pb to []*router.Route
for _, r := range pbRoutes.Routes {

View File

@ -14,7 +14,7 @@ func init() {
// Random is a random strategy algorithm for node selection
func Random(services []*registry.Service) Next {
var nodes []*registry.Node
nodes := make([]*registry.Node, 0, len(services))
for _, service := range services {
nodes = append(nodes, service.Nodes...)

View File

@ -38,7 +38,7 @@ func (c *etcd) Read() (*source.ChangeSet, error) {
return nil, fmt.Errorf("source not found: %s", c.prefix)
}
var kvs []*mvccpb.KeyValue
kvs := make([]*mvccpb.KeyValue, 0, len(rsp.Kvs))
for _, v := range rsp.Kvs {
kvs = append(kvs, (*mvccpb.KeyValue)(v))
}

View File

@ -282,7 +282,7 @@ func (n *node) Peers() []Node {
n.RLock()
defer n.RUnlock()
var peers []Node
peers := make([]Node, 0, len(n.peers))
for _, nodePeer := range n.peers {
peer := nodePeer.getTopology(MaxDepth)
peers = append(peers, peer)

View File

@ -38,7 +38,7 @@ func toValues(v []*pb.Value) []*registry.Value {
}
func ToProto(s *registry.Service) *pb.Service {
var endpoints []*pb.Endpoint
endpoints := make([]*pb.Endpoint, 0, len(s.Endpoints))
for _, ep := range s.Endpoints {
var request, response *pb.Value

View File

@ -22,7 +22,7 @@ func (r *Router) Lookup(ctx context.Context, req *pb.LookupRequest, resp *pb.Loo
return errors.InternalServerError("go.micro.router", "failed to lookup routes: %v", err)
}
var respRoutes []*pb.Route
respRoutes := make([]*pb.Route, 0, len(routes))
for _, route := range routes {
respRoute := &pb.Route{
Service: route.Service,

View File

@ -188,7 +188,7 @@ func (s *svc) Advertise() (<-chan *router.Advert, error) {
// Process processes incoming adverts
func (s *svc) Process(advert *router.Advert) error {
var events []*pb.Event
events := make([]*pb.Event, 0, len(advert.Events))
for _, event := range advert.Events {
route := &pb.Route{
Service: event.Route.Service,

View File

@ -194,7 +194,7 @@ func (t *table) Query(q ...QueryOption) ([]Route, error) {
return findRoutes(t.routes[opts.Service], opts.Address, opts.Gateway, opts.Network, opts.Router), nil
}
var results []Route
results := make([]Route, 0, len(t.routes))
// search through all destinations
for _, routes := range t.routes {
results = append(results, findRoutes(routes, opts.Address, opts.Gateway, opts.Network, opts.Router)...)

View File

@ -139,7 +139,7 @@ func (k *kubernetes) Update(s *runtime.Service) error {
func (k *kubernetes) List() ([]*runtime.Service, error) {
// TODO: this should list the k8s deployments
// but for now we return in-memory tracked services
var services []*runtime.Service
services := make([]*runtime.Service, 0, len(k.services))
k.RLock()
defer k.RUnlock()

View File

@ -490,7 +490,7 @@ func (s *rpcServer) Register() error {
return subscriberList[i].topic > subscriberList[j].topic
})
var endpoints []*registry.Endpoint
endpoints := make([]*registry.Endpoint, 0, len(handlerList)+len(subscriberList))
for _, n := range handlerList {
endpoints = append(endpoints, s.handlers[n].Endpoints()...)
}