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.
diff --git a/lib/go/thrift/deserializer.go b/lib/go/thrift/deserializer.go
index e1203a8..cefc7ec 100644
--- a/lib/go/thrift/deserializer.go
+++ b/lib/go/thrift/deserializer.go
@@ -31,12 +31,12 @@
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 @@
// 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 @@
}
}
+// 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 b1b8061..c449790 100644
--- a/lib/go/thrift/serializer.go
+++ b/lib/go/thrift/serializer.go
@@ -36,11 +36,12 @@
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 @@
// 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 @@
}
}
+// 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)