Skip to content

perf(consensus)!: eliminate busy loop allocation of heap memory for round state - #4436

Merged
cason merged 16 commits into
cometbft:mainfrom
skip-mev:wesl-ee/stop-hot-polling-roundstate
Nov 15, 2024
Merged

cason merged 16 commits into
cometbft:mainfrom
skip-mev:wesl-ee/stop-hot-polling-roundstate

Conversation

@wesl-ee

@wesl-ee wesl-ee commented Nov 5, 2024

Copy link
Copy Markdown
Contributor

This goroutine runs continuously with a minimal, negligible timer (so the Go scheduler will not starve). For practical purposes the timer does not exist because it is so short (would fire 10,000x per second).

func (conR *Reactor) updateRoundStateRoutine() {
t := time.NewTicker(100 * time.Microsecond)

This routine acquires locks to both (1) the consensus state struct and subsequently (2) the round state struct. It allocates new memory for a copy of consensus state anytime it can acquire the lock, which is often, as the lock is only held when consensus state is being mutated or read. The size of this allocation is approximately 216 bytes on a 64-bit arch. The struct is about half pointers and half fixed-size fields.

// getRoundState returns a shallow copy of the internal consensus state.
// This function is not thread-safe. Use GetRoundState for the thread-safe version.
func (cs *State) getRoundState() *cstypes.RoundState {
rs := cs.RoundState // copy
return &rs
}

Proposal

This fix removes the hot loop update of the reactor's view of round state by copying the round state from the event bus. In the case this is not passed, consensus state is accessed the same as it was before in updateRoundStateNoCsLock(), i.e. without acquiring the lock.

Another change worth noting is passing and storing the struct itself instead of a pointer to the struct to more clearly indicate this is an owned clone of the round state.

These two changes should reduce garbage collector thrash.


PR checklist

  • Tests written/updated
  • Changelog entry added in .changelog (we use unclog to manage our changelog)
  • Updated relevant documentation (docs/ or spec/) and code comments

@wesl-ee
wesl-ee requested a review from a team as a code owner November 5, 2024 14:49
@wesl-ee
wesl-ee requested a review from a team November 5, 2024 14:49
@andynog andynog added this to the 2024-Q4 milestone Nov 6, 2024

@melekes melekes left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome ❤️ would be great to see the difference between heap profiles. @wesl-ee what difference did you observe?

@cason

cason commented Nov 7, 2024

Copy link
Copy Markdown

I think this follows the lines of #3007.

@cason cason left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for that.

This is something that we need to find a way to address, as described in #3007.

But we need some performance evaluation, or at least tracing to attest the benefits of this approach.

Comment thread internal/consensus/state.go Resolved
Comment thread internal/consensus/state.go Resolved
Comment thread internal/consensus/reactor.go Resolved
Comment thread internal/consensus/reactor.go Resolved
Comment thread internal/consensus/reactor.go Resolved
@cason

cason commented Nov 7, 2024

Copy link
Copy Markdown

I'd like to have the opinion of @ValarDragon regarding this change, as the author of the referred issue.

@cason cason left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for that, I think the code becomes easier to read and I've never liked that almost busy loop to update the consensus state.

The right direction to go is to transfer the state from the consensus implementation to the reactor via "message passing", as we are already doing using the synchronous bus (although, a that synchronous bus is not an ideal setup as well).

What we really miss, however, is an assessment of the impact of these changes in performance, even if restricted to heap allocations and garbage collection costs.

I will approve to unblock, but I recommend considering my comments.

Comment thread internal/consensus/state.go Resolved
Comment thread internal/consensus/reactor.go Resolved
Comment thread internal/consensus/reactor.go Resolved
Comment thread internal/consensus/reactor.go Resolved
Comment thread internal/consensus/reactor.go Resolved
Comment thread internal/consensus/state.go Resolved
Comment thread internal/consensus/reactor.go Resolved
@wesl-ee

wesl-ee commented Nov 13, 2024 •

Copy link
Copy Markdown
Contributor Author

Rebased and profiled this PR directly against main using the command go tool pprof -diff_base ./heap-theirs -http=: ./heap-mine for the heap profile. Over a 10s run on an e2e network I can see that this PR did the following:

  • Reduced the magnitude of heap space allocated by GetRoundState by 3.14MiB (% change -4.23%)
  • Reduced the number of objects allocated by GetRoundState by 15,825 (% change -3.90%)
Screenshot 2024-11-13 at 3 57 38 PM

Over a 10m run the difference was more clear:

  • Reduced the magnitude of heap space allocated by GetRoundState by 133.39MiB (% change -23.47%)
  • Reduced the number of objects allocated by GetRoundState by 672,438 (% change -12.79%)
Screenshot 2024-11-13 at 3 56 10 PM

@cason cason left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank for the updates.

Notice that we can call rs := conR.conS.GetRoundState() instead of taking the lock, using the private method, then releasing the lock. Which is exactly what the public method does.

Comment thread internal/consensus/reactor.go Resolved
Comment thread internal/consensus/reactor.go Resolved
Comment thread internal/consensus/reactor.go Resolved
Comment thread internal/consensus/reactor.go Resolved
Comment thread internal/consensus/reactor.go Resolved
Comment thread internal/consensus/reactor.go Resolved
@wesl-ee

wesl-ee commented Nov 15, 2024 •

Copy link
Copy Markdown
Contributor Author

this test failure seems transient and unrelated. I see this test pass locally but cannot rerun CI myself

@cason
cason added this pull request to the merge queue Nov 15, 2024
@cason

cason commented Nov 15, 2024

Copy link
Copy Markdown

Thank you very much for this @wesl-ee. Sorry for being picky, this part of the code is super sensitive.

Merged via the queue into cometbft:main with commit bfafe15 Nov 15, 2024

@melekes melekes left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

mattac21 pushed a commit that referenced this pull request Sep 18, 2025
…ound state (#4436)

This goroutine runs continuously with a minimal, negligible timer (so
the Go scheduler will not starve). For practical purposes the timer does
not exist because it is so short (would fire 10,000x per second).

https://github.com/cometbft/cometbft/blob/48db7ac0037db4e16eab7536a05966f928a4b70b/internal/consensus/reactor.go#L560-L561

This routine acquires locks to both (1) the consensus state struct and
subsequently (2) the round state struct. It allocates new memory for a
copy of consensus state anytime it can acquire the lock, which is often,
as the lock is only held when consensus state is being mutated or read.
The size of this allocation is approximately 216 bytes on a 64-bit arch.
The struct is about half pointers and half fixed-size fields.

https://github.com/cometbft/cometbft/blob/48db7ac0037db4e16eab7536a05966f928a4b70b/internal/consensus/state.go#L259-L264

Proposal
---------

This fix removes the hot loop update of the reactor's view of round
state by copying the round state from the event bus. In the case this is
not passed, consensus state is accessed the same as it was before in
`updateRoundStateNoCsLock()`, i.e. without acquiring the lock.

Another change worth noting is passing and storing the struct itself
instead of a pointer to the struct to more clearly indicate this is an
owned clone of the round state.

These two changes should reduce garbage collector thrash.

---

- [ ] Tests written/updated
- [ ] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [x] Updated relevant documentation (`docs/` or `spec/`) and code
comments

---------

Co-authored-by: Daniel <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants