Skip to content

Commit

Permalink
Add global pack data pool
Browse files Browse the repository at this point in the history
  • Loading branch information
viciious committed Nov 2, 2023
1 parent 72b2439 commit 67e6cc5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})

Expand Down
35 changes: 35 additions & 0 deletions pack_data.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package tarantool

import (
"bytes"
"fmt"
"sync"

"github.com/tinylib/msgp/msgp"
)
Expand All @@ -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)
Expand Down Expand Up @@ -158,3 +167,29 @@ 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

if data == nil {
return nil
}

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
}

0 comments on commit 67e6cc5

Please sign in to comment.