This is an automated email from the ASF dual-hosted git repository.

yuxuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/thrift.git


The following commit(s) were added to refs/heads/master by this push:
     new 6dcd64e  THRIFT-5279: Go serializer/deserializer cleanups
6dcd64e is described below

commit 6dcd64ee5c0886697b38278956335ae4e945341b
Author: Yuxuan 'fishy' Wang <[email protected]>
AuthorDate: Tue Sep 22 15:03:57 2020 -0700

    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.
---
 lib/go/thrift/deserializer.go | 32 +++++++++++++++++++++++++++-----
 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 e1203a8..cefc7ec 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 b1b8061..c449790 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)

Reply via email to