From 144014db3465047cc459aa5383e1b2d2338f7f39 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Mon, 24 Feb 2020 17:15:20 +0300 Subject: [PATCH] allocations improvements and tunnel fixes (#1248) * reduce allocations in tunnel code Signed-off-by: Vasiliy Tolstov * another allocation fix Signed-off-by: Vasiliy Tolstov * allocate maps with len if it known Signed-off-by: Vasiliy Tolstov * allocate key for send once Signed-off-by: Vasiliy Tolstov --- codec.go | 2 +- grpc.go | 13 +++++++++++-- response.go | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/codec.go b/codec.go index ff37769..0366675 100644 --- a/codec.go +++ b/codec.go @@ -155,7 +155,7 @@ func (g *grpcCodec) ReadHeader(m *codec.Message, mt codec.MessageType) error { m = new(codec.Message) } if m.Header == nil { - m.Header = make(map[string]string) + m.Header = make(map[string]string, len(md)) } for k, v := range md { m.Header[k] = strings.Join(v, ",") diff --git a/grpc.go b/grpc.go index 519b445..0905eac 100644 --- a/grpc.go +++ b/grpc.go @@ -110,13 +110,18 @@ func (g *grpcClient) next(request client.Request, opts client.CallOptions) (sele } func (g *grpcClient) call(ctx context.Context, node *registry.Node, req client.Request, rsp interface{}, opts client.CallOptions) error { + var header map[string]string + address := node.Address - header := make(map[string]string) + header = make(map[string]string) if md, ok := metadata.FromContext(ctx); ok { + header = make(map[string]string, len(md)) for k, v := range md { header[k] = v } + } else { + header = make(map[string]string) } // set timeout in nanoseconds @@ -182,13 +187,17 @@ func (g *grpcClient) call(ctx context.Context, node *registry.Node, req client.R } func (g *grpcClient) stream(ctx context.Context, node *registry.Node, req client.Request, opts client.CallOptions) (client.Stream, error) { + var header map[string]string + address := node.Address - header := make(map[string]string) if md, ok := metadata.FromContext(ctx); ok { + header = make(map[string]string, len(md)) for k, v := range md { header[k] = v } + } else { + header = make(map[string]string) } // set timeout in nanoseconds diff --git a/response.go b/response.go index 5fd4016..cd4d319 100644 --- a/response.go +++ b/response.go @@ -27,7 +27,7 @@ func (r *response) Header() map[string]string { if err != nil { return map[string]string{} } - hdr := make(map[string]string) + hdr := make(map[string]string, len(md)) for k, v := range md { hdr[k] = strings.Join(v, ",") }