Use the existing RingBuffer for keeper executions - #817
Open
shaurya703 wants to merge 1 commit into
Open
Conversation
Fixes SO4-Markets#808. last_executions was a Vec trimmed with remove(0), which shifts every remaining element on each eviction, while the equivalent bounded-history list next to it (state.failures) already uses RingBuffer and its VecDeque-backed pop_front. Two implementations of the same idea in one file, one of them O(n). The cap stays 100 and the admin endpoint still returns the most recent 50, so behaviour is unchanged. Three things the swap needed: - Default is now hand-written. RingBuffer::default() is FAILURE_RING_CAPACITY, which is the failures list's 256 — a derived Default would have silently grown this list to 256. - RingBuffer gained a Serialize impl, because KeeperStatus derives Serialize and the field is part of it. Oldest first, so anything serializing a struct holding one sees the array it saw before. - The admin handler iterates and clones instead of consuming, since RingBuffer exposes iter() rather than into_iter(). Two tests: the eviction bound itself, which nothing covered before, and one that fails if Default ever goes back to the 256-capacity derive. 294 lib tests pass, 16 suites green, fmt and clippy -D warnings clean.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #808.
last_executionsis now theRingBufferalready used bystate.failures, so the two bounded-history lists in this file use one implementation instead of two — and eviction ispop_frontrather thanVec::remove(0)shifting every remaining element.Cap stays 100, the admin endpoint still returns the most recent 50. Behaviour unchanged.
Three things the swap needed, none of them obvious from the issue
Defaultis now hand-written.RingBuffer::default()isFAILURE_RING_CAPACITY— the failures list's 256. Keeping the derivedDefaultwould have silently grown this list from 100 to 256 with nothing to notice. There is a test that fails if it ever goes back.RingBuffergained aSerializeimpl, becauseKeeperStatusderivesSerializeand this field is part of it. Oldest-first, matching theVec, so anything serializing a struct that holds one sees the same array as before. (KeeperStatusis not serialized anywhere today —KeeperStatusResponseis, and builds its ownVec— but the derive is there and I did not want to remove it as a side effect of a performance change.)RingBufferexposesiter(), notinto_iter().Tests
Two, both in
state.rs:capacity + 25, assert exactly 100 remain, oldest evicted, newest retained, order preserved. Nothing covered this before, so a capacity change would have gone unnoticed.Defaulttrap above.Verified non-vacuous: switching
Defaultback toRingBuffer::default()fails both (292 passed; 2 failed).Checks
cargo test— 16 suites green, 294 lib tests.cargo fmt --checkclean.cargo clippy --all-targets -- -D warningsclean.