Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve rootkey cache in pruning: Reduce 4 rootkey fetches to 1 #1029

Merged
merged 1 commit into from
Dec 31, 2024

Conversation

ValarDragon
Copy link
Contributor

@ValarDragon ValarDragon commented Dec 31, 2024

Follow on to: #1026

This improves the rootkey cache used in pruning.

It stores two keys now, and its integrated into the traverse calls.

This speedsup the relevant prune logic in osmosis block sync a total of 27% (relative to prior #1026)

Summary by CodeRabbit

  • Performance Improvements
    • Enhanced root key caching mechanism to optimize database operations
    • Introduced a two-element cache for more efficient root key retrieval
    • Improved performance of version deletion and orphan traversal processes

@ValarDragon ValarDragon requested a review from a team as a code owner December 31, 2024 04:55
Copy link

coderabbitai bot commented Dec 31, 2024

Walkthrough

The pull request introduces a new rootkeyCache structure in nodedb.go to optimize root key caching. The implementation replaces a single-version cache with a two-element array, mimicking a direct-mapped cache. The changes modify methods like getRootKey, setRootKey, and deleteVersion to leverage this new caching mechanism, improving the efficiency of root key retrieval and management. The modifications aim to reduce database calls and enhance the performance of operations involving root key lookups.

Changes

File Change Summary
nodedb.go - Added new rootkeyCache struct
- Implemented newRootkeyCache() function
- Modified getRootKey method to use two-element cache
- Updated setRootKey method for cycling cache entries
- Adapted deleteVersion and traverseOrphans methods to use new cache

Sequence Diagram

sequenceDiagram
    participant Client
    participant NodeDB
    participant RootKeyCache
    participant Database

    Client->>NodeDB: Request Root Key
    NodeDB->>RootKeyCache: Check Cache
    alt Cache Hit
        RootKeyCache-->>NodeDB: Return Cached Root Key
    else Cache Miss
        NodeDB->>Database: Fetch Root Key
        Database-->>NodeDB: Return Root Key
        NodeDB->>RootKeyCache: Store in Cache
    end
    NodeDB-->>Client: Return Root Key
Loading

Possibly related PRs

Suggested reviewers

  • tac0turtle

Poem

🐰 Hop, hop, through the cache we go,
Two slots spinning, root keys in tow
Efficiency dancing, performance so bright
Our database queries now take flight!
Caching magic, a rabbit's delight 🔑✨

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
nodedb.go (1)

726-726: Reinforce code clarity via descriptive variable names.

The local variable rootkeyCache might be renamed to rkc or cache to maintain consistent naming across the file, but this is a minor style preference.

- rootkeyCache := newRootkeyCache()
+ rkc := newRootkeyCache()
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0f9b92f and 14fc304.

📒 Files selected for processing (1)
  • nodedb.go (6 hunks)
🔇 Additional comments (7)
nodedb.go (7)

419-424: Introduce concurrency considerations or confirm locking assumptions.

The rootkeyCache struct maintains a minimal two-element direct-mapped cache for version → rootKey lookups. While this design is efficient for its limited scope, ensure that concurrent access is safeguarded by the enclosing nodeDB's lock to prevent data races on versions, rootKeys, and next.

Would you like a verification script to confirm that all writes to this cache are protected by nodeDB.mtx?


428-434: Efficient direct lookups with manageable complexity.

The getRootKey method properly checks both cache positions before falling back to the DB fetch. This approach is consistent with a simple two-element cache design and should be performant for the intended usage.


444-448: Clear cycling logic, but watch for potential collisions.

Storing the version and rootKey in rkc.versions[rkc.next], then cyclically advancing rkc.next effectively mimics a two-entry FIFO. Confirm that your usage patterns only require caching of at most two versions at a time.


450-455: Initialize arrays with fewer allocations.

Returning a rootkeyCache with defaulted arrays ensures correct initial values and minimal overhead. This is straightforward and readable.


466-466: Ensure consistent usage of the new cache in prune logic.

Using the traverseOrphansWithRootkeyCache method for version deletion is aligned with the new caching mechanism. Verify that all references to root keys in deleteVersion are now consolidated to leverage cache.getRootKey.


1115-1117: Avoid recreating the cache if usage is frequent.

Instantiating a new cache immediately before traverseOrphansWithRootkeyCache may be acceptable if these calls are rare. Otherwise, confirm whether performance gains could arise from reusing a single cache instance across multiple orphan traversals.


Line range hint 1119-1130: Well-structured decoupling with explicit cache parameter.

Passing rootkeyCache as a parameter enables testability and modular design, especially when iterating over orphans in different versions. This is a clean approach that separates caching logic from traversal logic.

Copy link
Member

@tac0turtle tac0turtle left a comment

Choose a reason for hiding this comment

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

thanks for the prs

@tac0turtle tac0turtle merged commit 1f9d525 into master Dec 31, 2024
5 of 8 checks passed
@tac0turtle tac0turtle deleted the dev/test_further_rootkey_dedups branch December 31, 2024 07:42
@tac0turtle
Copy link
Member

@Mergifyio backport release/v1.2.x

@tac0turtle
Copy link
Member

@Mergifyio backport release/v1.3.x

Copy link
Contributor

mergify bot commented Dec 31, 2024

backport release/v1.2.x

✅ Backports have been created

Copy link
Contributor

mergify bot commented Dec 31, 2024

backport release/v1.3.x

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Dec 31, 2024
mergify bot pushed a commit that referenced this pull request Dec 31, 2024
tac0turtle pushed a commit that referenced this pull request Jan 2, 2025
…port #1029) (#1031)

Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com>
tac0turtle pushed a commit that referenced this pull request Jan 2, 2025
…port #1029) (#1030)

Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants