Skip to content

Conversation

tac0turtle
Copy link
Contributor

This optimization addresses the performance issue where hash calculation for empty blocks becomes costly when called repeatedly.

Changes

  • Adds private cachedHash field to Data struct
  • Uses thread-safe caching with RWMutex for concurrent access
  • Automatically invalidates cache when data changes via FromProto
  • Maintains backward compatibility with existing API

Performance Impact

Eliminates expensive marshaling + SHA256 operations for repeated hash calls on the same data, particularly beneficial for empty blocks.

Closes #2612

Generated with Claude Code

This optimization addresses the performance issue where hash calculation
for empty blocks becomes costly when called repeatedly. The solution:

- Adds private cachedHash field to Data struct
- Uses thread-safe caching with RWMutex for concurrent access
- Automatically invalidates cache when data changes via FromProto
- Maintains backward compatibility with existing API

Co-authored-by: Marko <[email protected]>
Copy link
Contributor

github-actions bot commented Sep 1, 2025

The latest Buf updates on your PR. Results from workflow CI and Release / buf-check (pull_request).

BuildFormatLintBreakingUpdated (UTC)
✅ passed⏩ skipped✅ passed✅ passedSep 1, 2025, 12:19 PM

@@ -93,6 +93,7 @@ message Metadata {
message Data {
Metadata metadata = 1;
repeated bytes txs = 2;
bytes cached_hash = 3; // Optional cached hash for performance
Copy link
Member

Choose a reason for hiding this comment

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

Do we really need to add this? Just caching the last one in the manager is sufficient right? Sure it is less optimized that cache it for all, but the logic that omits the cache for the serialization can be confusing if you use the api directly .

Copy link
Contributor Author

Choose a reason for hiding this comment

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

in the flow, every time we get we would rehash since we rarely to almost never keep the data struct alive for multiple checks on the hash after we get from the DB. This would make the hash cache nullified, evaluating if this cache is worth it if we remove it from the proto

Copy link
Member

Choose a reason for hiding this comment

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

don't we only care about height-1 anyway?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

Cache last data value
2 participants