diff options
author | Yuxuan 'fishy' Wang <yuxuan.wang@reddit.com> | 2020-09-22 15:03:57 -0700 |
---|---|---|
committer | Yuxuan 'fishy' Wang <fishywang@gmail.com> | 2020-09-23 09:16:42 -0700 |
commit | 6dcd64ee5c0886697b38278956335ae4e945341b (patch) | |
tree | 38986b372d8a2d806d11ec6c0f96593c8e3f0e8c | |
parent | a2c44665b416522477cffa6752c2f323768d0507 (diff) | |
download | thrift-6dcd64ee5c0886697b38278956335ae4e945341b.tar.gz |
THRIFT-5279: Go serializer/deserializer cleanups
Client: go
Cleanup the default NewTSerializer and NewTDeserializer implementations
to save an unnecessary allocation, and provide
NewTSerializerPoolSizeFactory and NewTDeserializerPoolSizeFactory for
easier non-default pool usages.
-rw-r--r-- | lib/go/thrift/deserializer.go | 32 | ||||
-rw-r--r-- | lib/go/thrift/serializer.go | 31 |
2 files changed, 54 insertions, 9 deletions
diff --git a/lib/go/thrift/deserializer.go b/lib/go/thrift/deserializer.go index e1203a868..cefc7ecda 100644 --- a/lib/go/thrift/deserializer.go +++ b/lib/go/thrift/deserializer.go @@ -31,12 +31,12 @@ type TDeserializer struct { func NewTDeserializer() *TDeserializer { transport := NewTMemoryBufferLen(1024) - - protocol := NewTBinaryProtocolFactoryDefault().GetProtocol(transport) + protocol := NewTBinaryProtocolTransport(transport) return &TDeserializer{ - transport, - protocol} + Transport: transport, + Protocol: protocol, + } } func (t *TDeserializer) ReadString(ctx context.Context, msg TStruct, s string) (err error) { @@ -68,7 +68,8 @@ func (t *TDeserializer) Read(ctx context.Context, msg TStruct, b []byte) (err er // TDeserializerPool is the thread-safe version of TDeserializer, // it uses resource pool of TDeserializer under the hood. // -// It must be initialized with NewTDeserializerPool. +// It must be initialized with either NewTDeserializerPool or +// NewTDeserializerPoolSizeFactory. type TDeserializerPool struct { pool sync.Pool } @@ -86,6 +87,27 @@ func NewTDeserializerPool(f func() *TDeserializer) *TDeserializerPool { } } +// NewTDeserializerPoolSizeFactory creates a new TDeserializerPool with +// the given size and protocol factory. +// +// Note that the size is not the limit. The TMemoryBuffer underneath can grow +// larger than that. It just dictates the initial size. +func NewTDeserializerPoolSizeFactory(size int, factory TProtocolFactory) *TDeserializerPool { + return &TDeserializerPool{ + pool: sync.Pool{ + New: func() interface{} { + transport := NewTMemoryBufferLen(size) + protocol := factory.GetProtocol(transport) + + return &TDeserializer{ + Transport: transport, + Protocol: protocol, + } + }, + }, + } +} + func (t *TDeserializerPool) ReadString(ctx context.Context, msg TStruct, s string) error { d := t.pool.Get().(*TDeserializer) defer t.pool.Put(d) diff --git a/lib/go/thrift/serializer.go b/lib/go/thrift/serializer.go index b1b8061d6..c44979094 100644 --- a/lib/go/thrift/serializer.go +++ b/lib/go/thrift/serializer.go @@ -36,11 +36,12 @@ type TStruct interface { func NewTSerializer() *TSerializer { transport := NewTMemoryBufferLen(1024) - protocol := NewTBinaryProtocolFactoryDefault().GetProtocol(transport) + protocol := NewTBinaryProtocolTransport(transport) return &TSerializer{ - transport, - protocol} + Transport: transport, + Protocol: protocol, + } } func (t *TSerializer) WriteString(ctx context.Context, msg TStruct) (s string, err error) { @@ -82,7 +83,8 @@ func (t *TSerializer) Write(ctx context.Context, msg TStruct) (b []byte, err err // TSerializerPool is the thread-safe version of TSerializer, it uses resource // pool of TSerializer under the hood. // -// It must be initialized with NewTSerializerPool. +// It must be initialized with either NewTSerializerPool or +// NewTSerializerPoolSizeFactory. type TSerializerPool struct { pool sync.Pool } @@ -100,6 +102,27 @@ func NewTSerializerPool(f func() *TSerializer) *TSerializerPool { } } +// NewTSerializerPoolSizeFactory creates a new TSerializerPool with the given +// size and protocol factory. +// +// Note that the size is not the limit. The TMemoryBuffer underneath can grow +// larger than that. It just dictates the initial size. +func NewTSerializerPoolSizeFactory(size int, factory TProtocolFactory) *TSerializerPool { + return &TSerializerPool{ + pool: sync.Pool{ + New: func() interface{} { + transport := NewTMemoryBufferLen(size) + protocol := factory.GetProtocol(transport) + + return &TSerializer{ + Transport: transport, + Protocol: protocol, + } + }, + }, + } +} + func (t *TSerializerPool) WriteString(ctx context.Context, msg TStruct) (string, error) { s := t.pool.Get().(*TSerializer) defer t.pool.Put(s) |