From 73a8323ee7166fe6373f39774390cd89a8e64639 Mon Sep 17 00:00:00 2001 From: "jesse.tang" <1430482733@qq.com> Date: Wed, 24 Aug 2022 18:19:07 +0800 Subject: [PATCH] fix: slice/map make init cap (#2300) --- cmd/protoc-gen-go-http/http.go | 2 +- config/value.go | 4 ++-- contrib/registry/consul/client.go | 2 +- contrib/registry/discovery/discovery.go | 2 +- transport/grpc/balancer.go | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/protoc-gen-go-http/http.go b/cmd/protoc-gen-go-http/http.go index 3eb91c061..cebbb6b15 100644 --- a/cmd/protoc-gen-go-http/http.go +++ b/cmd/protoc-gen-go-http/http.go @@ -213,9 +213,9 @@ func buildPathVars(path string) (res map[string]*string) { if strings.HasSuffix(path, "/") { fmt.Fprintf(os.Stderr, "\u001B[31mWARN\u001B[m: Path %s should not end with \"/\" \n", path) } - res = make(map[string]*string) pattern := regexp.MustCompile(`(?i){([a-z\.0-9_\s]*)=?([^{}]*)}`) matches := pattern.FindAllStringSubmatch(path, -1) + res = make(map[string]*string, len(matches)) for _, m := range matches { name := strings.TrimSpace(m[1]) if len(name) > 1 && len(m[2]) > 0 { diff --git a/config/value.go b/config/value.go index 805f155e5..46417e773 100644 --- a/config/value.go +++ b/config/value.go @@ -93,9 +93,9 @@ func (v *atomicValue) Slice() ([]Value, error) { func (v *atomicValue) Map() (map[string]Value, error) { if vals, ok := v.Load().(map[string]interface{}); ok { - m := make(map[string]Value) + m := make(map[string]Value, len(vals)) for key, val := range vals { - a := &atomicValue{} + a := new(atomicValue) a.Store(val) m[key] = a } diff --git a/contrib/registry/consul/client.go b/contrib/registry/consul/client.go index 80ea383a6..e97ec7e42 100644 --- a/contrib/registry/consul/client.go +++ b/contrib/registry/consul/client.go @@ -95,7 +95,7 @@ func (c *Client) Service(ctx context.Context, service string, index uint64, pass // Register register service instance to consul func (c *Client) Register(_ context.Context, svc *registry.ServiceInstance, enableHealthCheck bool) error { - addresses := make(map[string]api.ServiceAddress) + addresses := make(map[string]api.ServiceAddress, len(svc.Endpoints)) checkAddresses := make([]string, 0, len(svc.Endpoints)) for _, endpoint := range svc.Endpoints { raw, err := url.Parse(endpoint) diff --git a/contrib/registry/discovery/discovery.go b/contrib/registry/discovery/discovery.go index f6df56dc0..dfff0a972 100644 --- a/contrib/registry/discovery/discovery.go +++ b/contrib/registry/discovery/discovery.go @@ -439,7 +439,7 @@ func (r *Resolve) fetch(ctx context.Context) (ins *disInstancesInfo, ok bool) { ins = new(disInstancesInfo) ins.LastTs = appIns.LastTs ins.Scheduler = appIns.Scheduler - ins.Instances = make(map[string][]*discoveryInstance) + ins.Instances = make(map[string][]*discoveryInstance, len(appIns.Instances)) for zone, in := range appIns.Instances { ins.Instances[zone] = in } diff --git a/transport/grpc/balancer.go b/transport/grpc/balancer.go index bd50f0983..2047eb0db 100644 --- a/transport/grpc/balancer.go +++ b/transport/grpc/balancer.go @@ -40,7 +40,7 @@ func (b *balancerBuilder) Build(info base.PickerBuildInfo) balancer.Picker { // Block the RPC until a new picker is available via UpdateState(). return base.NewErrPicker(balancer.ErrNoSubConnAvailable) } - nodes := make([]selector.Node, 0) + nodes := make([]selector.Node, 0, len(info.ReadySCs)) for conn, info := range info.ReadySCs { ins, _ := info.Address.Attributes.Value("rawServiceInstance").(*registry.ServiceInstance) nodes = append(nodes, &grpcNode{