Skip to content

tapdb: keep universe root page cache warm across proof inserts#2192

Open
jtobin wants to merge 5 commits into
lightninglabs:mainfrom
jtobin:root-cache
Open

tapdb: keep universe root page cache warm across proof inserts#2192
jtobin wants to merge 5 commits into
lightninglabs:mainfrom
jtobin:root-cache

Conversation

@jtobin

@jtobin jtobin commented Jul 3, 2026

Copy link
Copy Markdown
Member

Previously, every proof leaf insert wiped the root node page cache across all universes, so any server ingesting proofs at a steady rate served AssetRoots pagination permanently cold; every page request fell through to the database.

The fix is targeted invalidation: inserting into an existing universe can't change which page its root appears on, so the insert now patches the cached root in place, and only universe creation and deletion still wipe the cache. A property test pins the invariant that the cache may miss, but never serves a page that differs from a fresh database read.

jtobin added 3 commits July 3, 2026 13:59
UpsertProofLeaf wiped the leaf keys cache for the same universe ID
twice in a row; the two call sites appear to be a merge artifact.
Both wipes are per-universe, so the second was pure dead weight.
The rootIndex map was populated in cacheRoots and cleared in
wipeCache, but no code path ever read from it. Drop it so the
upcoming targeted cache invalidation doesn't need to maintain a
structure with no consumers.
Return a universeRootStatus from universeUpsertProofLeaf that
indicates whether the upsert created the universe tree or updated an
existing one. Creation is detected by reading the tree root before
the insert: an empty root is equivalent to the tree being absent,
since deleting the last leaf of a universe removes the whole tree.

The extra root read is a single point query inside a transaction
that already fully decodes the raw proof, so the added cost is
negligible.

All callers ignore the new value for now; a follow-up uses it to
scope root node cache invalidation on the multiverse ingest path.
@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request optimizes the performance of the universe root node page cache by moving from a global cache invalidation strategy to a targeted one. By distinguishing between universe creation and simple leaf updates, the system can now maintain a warm cache for existing universes during proof ingestion, significantly reducing database load for paginated queries.

Highlights

  • Targeted Cache Invalidation: Implemented targeted invalidation for the universe root node page cache. Previously, every proof leaf insert wiped the entire cache; now, updates to existing universes patch the root in place, while only universe creation or deletion triggers a full cache wipe.
  • Performance Optimization: Improved pagination performance for AssetRoots by keeping the cache warm during steady-state proof ingestion, preventing unnecessary database fall-throughs.
  • Testing and Verification: Added a model-based property test to ensure the cache invariant holds: the cache may miss, but it never serves a page that differs from a fresh database read.
New Features

🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@jtobin jtobin added this to the v0.8.2 milestone Jul 3, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request optimizes the universe root node page cache by implementing targeted invalidation instead of wiping the entire cache on every proof leaf insertion. Now, when a proof leaf is inserted, the system checks if a new universe was created or an existing one was updated. If a new universe is created, the cache is wiped; if an existing universe is updated, the cache is patched in place. The feedback suggests optimizing the batch upsert by caching the check for whether the cache was wiped, thereby avoiding redundant patchRoot calls and mutex acquisitions.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread tapdb/multiverse.go Outdated
Comment thread tapdb/multiverse.go Outdated
jtobin added 2 commits July 3, 2026 15:24
Every proof leaf insert used to wipe the entire root node page cache
across all universes, keeping AssetRoots pagination permanently cold
on a busy server. But the page cache is keyed by pagination
parameters only, and the backing UniverseRoots query orders by the
stable universe_roots.id column, so inserting a leaf into an
existing universe never changes which page its root appears on: the
cached copies of that root can be patched in place, the same way the
adjacent syncer cache is already updated via addOrReplace.

Only two events actually change page composition and still wipe the
page cache: creating a new universe (first leaf) and deleting one.
Pages fetched with grouped asset amounts can't be patched without a
database query, so an update evicts exactly those pages that contain
the changed root.

The batch insert path applies the same logic: one wipe if any item
created a universe, otherwise an in-place patch per updated root.

A model-based rapid property test asserts the central invariant:
the cache may miss at any time, but a page it serves must always
equal a fresh database read, and handed-out pages are never mutated.
A deterministic test covers cache warmness across single and batch
inserts, creation wipes, and amounts-page eviction.
@jtobin jtobin modified the milestones: v0.8.2, v0.9 Jul 8, 2026
@lightninglabs-deploy

Copy link
Copy Markdown

@jtobin, remember to re-request review from reviewers when ready

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

Projects

Status: 🆕 New

Development

Successfully merging this pull request may close these issues.

2 participants