Repository navigation
perf(consensus)!: eliminate busy loop allocation of heap memory for round state - #4436
Conversation
|
I think this follows the lines of #3007. |
|
I'd like to have the opinion of @ValarDragon regarding this change, as the author of the referred issue. |
cason
left a comment
There was a problem hiding this comment.
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.
cason
left a comment
There was a problem hiding this comment.
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.
Co-authored-by: Daniel <[email protected]>
|
this test failure seems transient and unrelated. I see this test pass locally but cannot rerun CI myself |
|
Thank you very much for this @wesl-ee. Sorry for being picky, this part of the code is super sensitive. |
…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]>


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).
cometbft/internal/consensus/reactor.go
Lines 560 to 561 in 48db7ac
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.
cometbft/internal/consensus/state.go
Lines 259 to 264 in 48db7ac
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
.changelog(we use unclog to manage our changelog)docs/orspec/) and code comments