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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
odeke-em opened this issue Feb 5, 2024 · 3 comments · Fixed by #2259
Milestone

Comments

@odeke-em
Copy link
Contributor
odeke-em commented Feb 5, 2024

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.

@melekes
Copy link
Contributor
melekes commented Feb 7, 2024

https://tip.golang.org/ref/spec#Clear

func (c *LRUTxCache) Reset() { 
...  
         clear(c.cacheMap)
         c.list.Init() 
 } 

melekes added a commit that referenced this issue Feb 7, 2024
instead of allocating a new one
Closes #2243
@odeke-em
Copy link
Contributor Author
odeke-em commented Feb 7, 2024

@melekes clear was introduced in Go1.21, is every user of this repository already upgraded to that very recent Go version?

@melekes
Copy link
Contributor
melekes commented Feb 7, 2024

https://github.com/cometbft/cometbft?tab=readme-ov-file#minimum-requirements

don't know about every user, but our current min version is 1.21 as stated in the README

github-merge-queue bot pushed a commit that referenced this issue Feb 9, 2024
instead of allocating a new one
Closes #2243

It should not be backported onto <= v0.37 because their min Go version
is 1.20, and `clear` was introduced in Go 1.21.

---

#### PR checklist

- [ ] Tests written/updated
- [ ] ~~Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)~~
- [ ] Updated relevant documentation (`docs/` or `spec/`) and code
comments
- [x] Title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec
mergify bot pushed a commit that referenced this issue Feb 9, 2024
instead of allocating a new one
Closes #2243

It should not be backported onto <= v0.37 because their min Go version
is 1.20, and `clear` was introduced in Go 1.21.

---

#### PR checklist

- [ ] Tests written/updated
- [ ] ~~Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)~~
- [ ] Updated relevant documentation (`docs/` or `spec/`) and code
comments
- [x] Title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec

(cherry picked from commit fc4e1ef)
@adizere adizere added this to the 2024-Q1 milestone Mar 1, 2024
@adizere adizere added this to CometBFT Mar 1, 2024
@github-project-automation github-project-automation bot moved this to Todo in CometBFT Mar 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: Todo
Development

Successfully merging a pull request may close this issue.

3 participants
0