util: make reduce tsset object allocation (#218)

Signed-off-by: tiancaiamao <tiancaiamao@gmail.com>
This commit is contained in:
tiancaiamao 2021-07-08 14:56:17 +08:00 committed by GitHub
parent 16fb6792bf
commit a1adc5b85b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 13 deletions

View File

@ -104,7 +104,7 @@ type KVSnapshot struct {
keyOnly bool keyOnly bool
vars *kv.Variables vars *kv.Variables
replicaReadSeed uint32 replicaReadSeed uint32
resolvedLocks *util.TSSet resolvedLocks util.TSSet
scanBatchSize int scanBatchSize int
// Cache the result of BatchGet. // Cache the result of BatchGet.
@ -146,7 +146,6 @@ func newTiKVSnapshot(store *KVStore, ts uint64, replicaReadSeed uint32) *KVSnaps
priority: PriorityNormal, priority: PriorityNormal,
vars: kv.DefaultVars, vars: kv.DefaultVars,
replicaReadSeed: replicaReadSeed, replicaReadSeed: replicaReadSeed,
resolvedLocks: util.NewTSSet(5),
} }
} }
@ -164,8 +163,6 @@ func (s *KVSnapshot) SetSnapshotTS(ts uint64) {
s.mu.Lock() s.mu.Lock()
s.mu.cached = nil s.mu.cached = nil
s.mu.Unlock() s.mu.Unlock()
// And also the minCommitTS pushed information.
s.resolvedLocks = util.NewTSSet(5)
} }
// BatchGet gets all the keys' value from kv-server and returns a map contains key/value pairs. // BatchGet gets all the keys' value from kv-server and returns a map contains key/value pairs.
@ -328,7 +325,7 @@ func (s *KVSnapshot) batchGetKeysByRegions(bo *Backoffer, keys [][]byte, collect
} }
func (s *KVSnapshot) batchGetSingleRegion(bo *Backoffer, batch batchKeys, collectF func(k, v []byte)) error { func (s *KVSnapshot) batchGetSingleRegion(bo *Backoffer, batch batchKeys, collectF func(k, v []byte)) error {
cli := NewClientHelper(s.store, s.resolvedLocks) cli := NewClientHelper(s.store, &s.resolvedLocks)
s.mu.RLock() s.mu.RLock()
if s.mu.stats != nil { if s.mu.stats != nil {
cli.Stats = make(map[tikvrpc.CmdType]*locate.RPCRuntimeStats) cli.Stats = make(map[tikvrpc.CmdType]*locate.RPCRuntimeStats)
@ -497,7 +494,7 @@ func (s *KVSnapshot) get(ctx context.Context, bo *Backoffer, k []byte) ([]byte,
} }
}) })
cli := NewClientHelper(s.store, s.resolvedLocks) cli := NewClientHelper(s.store, &s.resolvedLocks)
s.mu.RLock() s.mu.RLock()
if s.mu.stats != nil { if s.mu.stats != nil {

View File

@ -40,17 +40,18 @@ type TSSet struct {
m map[uint64]struct{} m map[uint64]struct{}
} }
// NewTSSet creates a set to store timestamps.
func NewTSSet(capacity int) *TSSet {
return &TSSet{
m: make(map[uint64]struct{}, capacity),
}
}
// Put puts timestamps into the map. // Put puts timestamps into the map.
func (s *TSSet) Put(tss ...uint64) { func (s *TSSet) Put(tss ...uint64) {
s.Lock() s.Lock()
defer s.Unlock() defer s.Unlock()
// Lazy initialization..
// Most of the time, there is no transaction lock conflict.
// So allocate this in advance is unnecessary and bad for performance.
if s.m == nil {
s.m = make(map[uint64]struct{}, 5)
}
for _, ts := range tss { for _, ts := range tss {
s.m[ts] = struct{}{} s.m[ts] = struct{}{}
} }