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 8ab9653
Show file tree
Hide file tree
Showing 2 changed files with 31 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
28 changes: 28 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,23 @@ 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 {
if data == nil {
return nil
}

key := fmt.Sprintf("%+v", data)

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
}

0 comments on commit 8ab9653

Please sign in to comment.