From 8b936e927a989fde0592590adc90c73f32876e29 Mon Sep 17 00:00:00 2001 From: Peter Mattis Date: Wed, 9 Mar 2016 10:27:53 -0500 Subject: [PATCH] transport: remove unnecessary allocation Storing an integer in an interface requires an allocation. Storing an empty struct does not. This matches the pattern used for peer.NewContext and metadata.NewContext. --- transport/transport.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/transport/transport.go b/transport/transport.go index 3b934b4d..6eca1b3b 100644 --- a/transport/transport.go +++ b/transport/transport.go @@ -299,20 +299,18 @@ func (s *Stream) Read(p []byte) (n int, err error) { return } -type key int - // The key to save transport.Stream in the context. -const streamKey = key(0) +type streamKey struct{} // newContextWithStream creates a new context from ctx and attaches stream // to it. func newContextWithStream(ctx context.Context, stream *Stream) context.Context { - return context.WithValue(ctx, streamKey, stream) + return context.WithValue(ctx, streamKey{}, stream) } // StreamFromContext returns the stream saved in ctx. func StreamFromContext(ctx context.Context) (s *Stream, ok bool) { - s, ok = ctx.Value(streamKey).(*Stream) + s, ok = ctx.Value(streamKey{}).(*Stream) return }