perf(mempool): O(k) reap with head-offset compaction#1070
Draft
perf(mempool): O(k) reap with head-offset compaction#1070
Conversation
Replace the full O(N) index rebuild after every Reap() with a head-offset design. Reap now advances a head pointer past consumed entries and only compacts the backing slice when a meaningful prefix has been consumed (threshold-based). Push operations deduplicate under a single write lock, eliminating the TOCTOU window between the old exists() and push() calls. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Summary
txIndexrebuild after everyReap()with an O(k) head-offset design that advances a pointer past consumed entrieshead >= 1024orhead*2 >= len(txs), keeping the common path allocation-freeexists()+push()into single-lockpushLocked(), eliminating a TOCTOU race on duplicate detectionMotivation
The
ReapListmaintained atxIndex map[string]intfor deduplication. After everyReap(), the entire index was rebuilt from scratch (slices.DeleteFunc+ re-index loop), even thoughReap()only consumes entries from the front. This is O(N) in the total list size, wasted work when the list is large.Benchmark Results (benchstat, 5 runs, Apple M4 Pro)
All Cosmos-path results statistically significant (p ≤ 0.032). EVM-only paths show no change (expected — ReapList is Cosmos-side only). Memory and allocs unchanged (zero-allocation design).
Files Changed
mempool/reap_list.gohead intfield; rewriteReap()to iterate fromhead; addmaybeCompactLocked(); mergeexists()+push()intopushLocked()Test Plan
go test -tags=test -race ./mempool/...— 74 tests pass with race detection🤖 Generated with Claude Code