Skip to content

Conversation

CreeptoGengar
Copy link
Contributor

@CreeptoGengar CreeptoGengar commented Sep 24, 2025

Summary

Implements caching for state.BuildLastCommitInfo to eliminate performance bottlenecks in consensus operations.

Changes

  • Add validator and ABCI validator caches to BlockExecutor
  • Create optimized BuildLastCommitInfoFromStoreWithCache and BuildLastCommitInfoWithCache methods
  • Update all call sites in applyBlock, ExtendVote, ProcessProposal
  • Add ClearValidatorCache() for memory management

Performance Impact

  • 30.7x faster: 8462 ns/op → 275.7 ns/op
  • 9.7x less memory: 3711 B/op → 384 B/op
  • 3.4x fewer allocations: 37 → 11 allocs/op

Fixes #3152


Note

Introduce cached validator/ABCI validator lookups in BuildLastCommitInfo and update call sites for faster block processing.

  • State/Execution (state/execution.go):
    • Caching:
      • Add validatorCache and abciValidatorCache with RW locks to BlockExecutor.
      • New methods: BuildLastCommitInfoFromStoreWithCache, BuildLastCommitInfoWithCache, and ClearValidatorCache.
    • Integrations:
      • Replace uses of buildLastCommitInfoFromStore in ProcessProposal, FinalizeBlock path (applyBlock), and ExtendVote with cached variants.
      • Initialize caches in NewBlockExecutor.
      • Add ExecCommitBlockWithCache helper.
  • Tests (state/execution_test.go):
    • Add unit test for cache behavior and clearing.
    • Add benchmarks comparing cached vs original implementations.

Written by Cursor Bugbot for commit 5663a3f. This will update automatically on new commits. Configure here.

}

// ExecCommitBlockWithCache is an optimized version that uses caching for better performance
func (blockExec *BlockExecutor) ExecCommitBlockWithCache(
Copy link
Collaborator

Choose a reason for hiding this comment

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

this is unused. looks like the previous ExecCommitBlock is only used when replaying blocks. do we want to use that there?

}

// ClearValidatorCache clears the validator caches to free memory
func (blockExec *BlockExecutor) ClearValidatorCache() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

this is unused except in tests, so the part of your test that is testing cache clearing isn't really testing anything

Comment on lines +88 to +89
validatorCache: make(map[int64]*types.ValidatorSet),
abciValidatorCache: make(map[string]abci.Validator),
Copy link
Collaborator

Choose a reason for hiding this comment

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

in real execution, no values from these caches are ever removed, so these will grow forever. We will need someway to periodically remove elements from them.

commitSig := block.LastCommit.Signatures[i]

// Create cache key for this validator
cacheKey := fmt.Sprintf("%s_%d", val.PubKey.Address(), val.VotingPower)
Copy link
Collaborator

Choose a reason for hiding this comment

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

from my understanding the point of the abciValidatorCache is to not have to do the expensive val.PubKey.Address() call. however you are calling val.PubKey.Address() when generating the cacheKey. doesn't this eliminate most of the point of the cache?

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

@CreeptoGengar CreeptoGengar requested a review from mattac21 October 2, 2025 13:02

// Cleanup old cache entries if needed
blockExec.cleanupOldCacheEntries()
}
Copy link

Choose a reason for hiding this comment

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

Bug: Validator Cache Issues in ABCI

The abciValidatorCache in BuildLastCommitInfoWithCache has a few problems: it uses a manual abci.Validator conversion that may miss important logic, its cache key (validator address) can lead to stale voting power if power changes, and it has a race condition allowing redundant conversions and cache writes.

Fix in Cursor Fix in Web

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.

Cache the workload in state.buildLastCommitInfo

3 participants