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

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?

@tac0turtle tac0turtle closed this Oct 9, 2025
@github-project-automation github-project-automation bot moved this to Done in Evolve Oct 9, 2025
Copy link

codecov bot commented Oct 9, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 0.00%. Comparing base (6050506) to head (e45d7d6).
⚠️ Report is 86 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #2616       +/-   ##
==========================================
- Coverage   72.41%       0   -72.42%     
==========================================
  Files          72       0       -72     
  Lines        7394       0     -7394     
==========================================
- Hits         5354       0     -5354     
+ Misses       1600       0     -1600     
+ Partials      440       0      -440     
Flag Coverage Δ
combined ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Cache last data value

2 participants