diff --git a/connection.go b/connection.go index f1cec3a..72bfa79 100644 --- a/connection.go +++ b/connection.go @@ -104,6 +104,9 @@ func connect(ctx context.Context, scheme, addr string, opts Options) (conn *Conn return } + // store or fetch uniq instance of packdata in the global pool + conn.packData = globalPackDataPool.Put(conn.packData) + // remove deadline conn.tcpConn.SetDeadline(time.Time{}) diff --git a/pack_data.go b/pack_data.go index 56c177e..7381b49 100644 --- a/pack_data.go +++ b/pack_data.go @@ -1,7 +1,9 @@ package tarantool import ( + "bytes" "fmt" + "sync" "github.com/tinylib/msgp/msgp" ) @@ -20,6 +22,13 @@ type packData struct { primaryKeyMap map[uint64][]int } +type packDataPool struct { + sync.Mutex + pool map[string]*packData +} + +var globalPackDataPool packDataPool + func encodeValues2(v1, v2 interface{}) []byte { o := make([]byte, 0) o, _ = msgp.AppendIntf(o, v1) @@ -158,3 +167,25 @@ func (data *packData) packIndex(space interface{}, index interface{}, o []byte) o = msgp.AppendUint64(o, indexNo) return o, nil } + +func (pool *packDataPool) Put(data *packData) *packData { + var err error + + o := make([]byte, 0) + o, err = msgp.AppendIntf(o, data) + if err != nil { + return data + } + + var buf bytes.Buffer + msgp.CopyToJSON(&buf, bytes.NewReader(o)) + key := buf.String() + + pool.Lock() + defer pool.Unlock() + if odata, ok := pool.pool[key]; ok { + return odata + } + pool.pool[key] = data + return data +}