Skip to content

perf(state): avoid linear validator lookups in MedianTime - #6051

Open
songgaoye wants to merge 2 commits into
cometbft:mainfrom
songgaoye:song/perf_median_time
Open

perf(state): avoid linear validator lookups in MedianTime#6051
songgaoye wants to merge 2 commits into
cometbft:mainfrom
songgaoye:song/perf_median_time

Conversation

@songgaoye

@songgaoye songgaoye commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

Summary

MedianTime performs a linear validator lookup for each commit signature. Use direct index access when the address matches, with address lookup as a fallback to preserve behavior for out-of-order signatures.

Performance

Apple M4, 300ms per benchmark, median of 5 runs:

Validators Before After Speedup
20 789 ns 451 ns 1.75×
100 9.41 µs 2.04 µs 4.62×
1,000 806 µs 19.3 µs 41.9×

These results measure MedianTime with ordered signatures and identical timestamps, not overall node throughput. Allocation counts are unchanged.

Testing

  • Added a regression test verifying that out-of-order signatures with unequal voting powers preserve the weighted median.
  • Added benchmarks for different validator set sizes.
  • Passed go test ./state -count=1.

PR checklist

  • Tests written/updated
  • Changelog entry added in CHANGELOG.md
  • Updated relevant documentation (docs/ or spec/) and code comments

@songgaoye
songgaoye requested a review from a team as a code owner September 13, 2026 11:13
@greptile-apps

greptile-apps Bot commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

PR author is not in the allowed authors list.

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.

1 participant