From cb8af49ed50b6682c9651954f1a00074b568cd0a Mon Sep 17 00:00:00 2001 From: Victor Luchits Date: Thu, 2 Nov 2023 17:04:44 +0300 Subject: [PATCH] Add global pack data pool --- connection.go | 3 +++ pack_data.go | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) 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..b8ec3b5 100644 --- a/pack_data.go +++ b/pack_data.go @@ -2,6 +2,7 @@ package tarantool import ( "fmt" + "sync" "github.com/tinylib/msgp/msgp" ) @@ -20,6 +21,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 +166,70 @@ func (data *packData) packIndex(space interface{}, index interface{}, o []byte) o = msgp.AppendUint64(o, indexNo) return o, nil } + +func (data *packData) MarshalMsg(o []byte) ([]byte, error) { + var err error + + if o, err = msgp.AppendIntf(o, data.defaultSpace); err != nil { + return o, err + } + o = msgp.AppendBytes(o, data.packedDefaultSpace) + o = msgp.AppendBytes(o, data.packedDefaultIndex) + o = msgp.AppendBytes(o, data.packedIterEq) + o = msgp.AppendBytes(o, data.packedDefaultLimit) + o = msgp.AppendBytes(o, data.packedDefaultOffset) + o = msgp.AppendBytes(o, data.packedSingleKey) + + o = msgp.AppendMapHeader(o, uint32(len(data.spaceMap))) + for key, val := range data.spaceMap { + o = msgp.AppendString(o, key) + o = msgp.AppendUint64(o, val) + } + + o = msgp.AppendMapHeader(o, uint32(len(data.indexMap))) + for key, val := range data.indexMap { + o = msgp.AppendUint64(o, key) + o = msgp.AppendMapHeader(o, uint32(len(val))) + for key2, val2 := range val { + o = msgp.AppendString(o, key2) + o = msgp.AppendUint64(o, val2) + } + } + + o = msgp.AppendMapHeader(o, uint32(len(data.primaryKeyMap))) + for key, val := range data.primaryKeyMap { + o = msgp.AppendUint64(o, key) + if o, err = msgp.AppendIntf(o, val); err != nil { + return o, err + } + } + + return o, nil +} + +func (pool *packDataPool) Put(data *packData) *packData { + var err error + + if data == nil { + return nil + } + + o, err := msgp.AppendIntf(nil, data) + if err != nil { + return data + } + + key := string(o) + + pool.Lock() + defer pool.Unlock() + + if pool.pool == nil { + pool.pool = make(map[string]*packData) + } + if odata, ok := pool.pool[key]; ok { + return odata + } + pool.pool[key] = data + return data +}