Skip to content

perf(storer): filter excluded batches and rogue chunks early in reserve sample - #5634

Open
gacevicljubisa wants to merge 2 commits into
masterfrom
perf/sample-early-batch-filter
Open

gacevicljubisa wants to merge 2 commits into
masterfrom
perf/sample-early-batch-filter

Conversation

@gacevicljubisa

@gacevicljubisa gacevicljubisa commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

Checklist

  • I have read the coding guide.
  • My change requires a documentation update, and I have done it.
  • I have added tests to cover my changes.
  • I have filled out the description and linked the related issues.

Description

Optimize batch filtering in ReserveSample by replacing map[string]struct{} with a stack-allocated [32]byte lookup, where string conversions and heap allocations are eliminated. Filtering is moved to Phase 1.

Benchmark Results

BenchmarkReserveSample10kWith1000ExcludedBatches (10k chunks in reserve, 1000 excluded batches):

Metric master This branch Delta
Time / op 3,294,111 ns/op (~3.29 ms) 1,412,442 ns/op (~1.41 ms) ~2.3x faster (-57%)
Allocs / op 67,081 allocs/op 66,079 allocs/op -1,002 allocs/op
Memory / op 4,299,470 B/op 4,318,118 B/op ~neutral

Open API Spec Version Changes (if applicable)

Motivation and Context (Optional)

Related Issue (Optional)

Screenshots (if appropriate):

AI Disclosure

  • This PR contains code that has been generated by an LLM.
  • I have reviewed the AI generated code thoroughly.
  • I possess the technical expertise to responsibly review the code generated in this PR.

Comment thread pkg/storer/sample.go
Comment thread pkg/storer/sample.go Outdated
isExcludedBatch, err := db.batchExclusionFilter(minBatchBalance)
if err != nil {
db.logger.Error(err, "get batches below value")
db.logger.Error(err, "get batch exclusion filter")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not introduced here (master also logs and continues, with a partial map), but since this is being touched: if this fails, the sample includes chunks from below-balance batches and the reveal will disagree with the neighborhood, which gets the node frozen. Returning the error would make the node skip the round instead:

if err != nil {
return Sample{}, fmt.Errorf("batch exclusion filter: %w", err)
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It makes sense. Great for noticing this.

Comment thread pkg/storer/sample.go
func (db *DB) batchesBelowValue(until *big.Int) (map[string]struct{}, error) {
res := make(map[string]struct{})

func (db *DB) batchExclusionFilter(until *big.Int) (func([]byte) bool, error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Every ReserveSample test passes nil for minBatchBalance, but production (Agent.minBatchBalance()) never does, so this path has no test. Could you add one: put a low-value batch in the mock batchstore (batchstore.WithBatch), store chunks from that batch and from another one, then sample with a minimum below / equal to / above the batch value, and assert that no sample items come from the low batch and that BelowBalanceIgnored is correct? (The PR checklist says tests were added, but none are in the diff.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

done

@gacevicljubisa
gacevicljubisa force-pushed the perf/sample-early-batch-filter branch from 2b1a4ee to 2eadeb8 Compare October 1, 2026 17:11
@gacevicljubisa
gacevicljubisa requested a review from acud October 3, 2026 15:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants