Skip to content

Commit

Permalink
Merge PR: optimize calling for txKey (#2083)
Browse files Browse the repository at this point in the history
Co-authored-by: KamiD <[email protected]>
  • Loading branch information
MinZhu09 and KamiD authored May 23, 2022
1 parent 36fb0f4 commit a716fab
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions libs/tendermint/mempool/clist_mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,11 +418,13 @@ func (mem *CListMempool) addAndSortTx(memTx *mempoolTx) error {
}

mem.txs.InsertElement(elem)
mem.txsMap.Store(txKey(memTx.tx), elem)

txHash := txKey(memTx.tx)
mem.txsMap.Store(txHash, elem)
atomic.AddInt64(&mem.txsBytes, int64(len(memTx.tx)))

ele := mem.bcTxsList.PushBack(memTx)
mem.bcTxsMap.Store(txKey(memTx.tx), ele)
mem.bcTxsMap.Store(txHash, ele)

mem.metrics.TxSizeBytes.Observe(float64(len(memTx.tx)))
mem.eventBus.PublishEventPendingTx(types.EventDataTx{TxResult: types.TxResult{
Expand Down Expand Up @@ -489,8 +491,9 @@ func (mem *CListMempool) addTx(memTx *mempoolTx) error {
// - resCbRecheck (lock not held) if tx was invalidated
func (mem *CListMempool) removeTx(elem *clist.CElement, ignoreAddressRecord ...bool) {
tx := elem.Value.(*mempoolTx).tx
txHash := txKey(tx)
if mem.config.SortTxByGp {
if e, ok := mem.bcTxsMap.LoadAndDelete(txKey(tx)); ok {
if e, ok := mem.bcTxsMap.LoadAndDelete(txHash); ok {
tmpEle := e.(*clist.CElement)
mem.bcTxsList.Remove(tmpEle)
tmpEle.DetachPrev()
Expand All @@ -504,7 +507,7 @@ func (mem *CListMempool) removeTx(elem *clist.CElement, ignoreAddressRecord ...b
mem.addressRecord.DeleteItem(elem)
}

mem.txsMap.Delete(txKey(tx))
mem.txsMap.Delete(txHash)
atomic.AddInt64(&mem.txsBytes, int64(-len(tx)))
}

Expand Down Expand Up @@ -1051,11 +1054,12 @@ func (cache *mapTxCache) Reset() {
// Push adds the given tx to the cache and returns true. It returns
// false if tx is already in the cache.
func (cache *mapTxCache) Push(tx types.Tx) bool {
// Use the tx hash in the cache
txHash := txKey(tx)

cache.mtx.Lock()
defer cache.mtx.Unlock()

// Use the tx hash in the cache
txHash := txKey(tx)
if moved, exists := cache.cacheMap[txHash]; exists {
cache.list.MoveToBack(moved)
return false
Expand All @@ -1076,8 +1080,9 @@ func (cache *mapTxCache) Push(tx types.Tx) bool {

// Remove removes the given tx from the cache.
func (cache *mapTxCache) Remove(tx types.Tx) {
cache.mtx.Lock()
txHash := txKey(tx)

cache.mtx.Lock()
popped := cache.cacheMap[txHash]
delete(cache.cacheMap, txHash)
if popped != nil {
Expand Down

0 comments on commit a716fab

Please sign in to comment.