8000 mempool: LRUTxCache.Reset could benefit from using the map clearing idiom to reduce CPU & RAM pressure · Issue #2243 · cometbft/cometbft · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
mempool: LRUTxCache.Reset could benefit from using the map clearing idiom to reduce CPU & RAM pressure #2243
Closed
@odeke-em

Description

@odeke-em

If we look at this code

func (c *LRUTxCache) Reset() {
c.mtx.Lock()
defer c.mtx.Unlock()
c.cacheMap = make(map[types.TxKey]*list.Element, c.size)
c.list.Init()
}

we can see that .Reset simply takes a lock then allocates a fresh map by discarding the previous one.

Suggestion

We can reduce on CPU & RAM pressure by using the map clearing idiom aka

func (c *LRUTxCache) Reset() { 
 	c.mtx.Lock() 
 	defer c.mtx.Unlock() 
  
        for key := range c.cacheMap {
             delete(c.cacheMap, key)
 	}
 	c.list.Init() 
 } 

which in performance wisdom is a known and accepted Go idiom for reducing CPU & RAM pressure by reusing the map.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0