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 6c07fa1
Show file tree
Hide file tree
Showing 2 changed files with 35 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
32 changes: 32 additions & 0 deletions pack_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tarantool

import (
"fmt"
"sync"

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

key := string(o)

pool.Lock()
defer pool.Unlock()
if odata, ok := pool.pool[key]; ok {
return odata
}
pool.pool[key] = data
return data
}

0 comments on commit 6c07fa1

Please sign in to comment.