Skip to content

perf(replicated_state): Fill a loaded log memory store's first-timestamp cache on first use - #12192

Merged
alin-at-dfinity merged 2 commits into
masterfrom
alin/log-memory-store-lazy-cache
Nov 21, 2026
Merged

alin-at-dfinity merged 2 commits into
masterfrom
alin/log-memory-store-lazy-cache

Conversation

@alin-at-dfinity

@alin-at-dfinity alin-at-dfinity commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Loading a canister from a checkpoint rebuilds its log memory store's first-timestamp cache by reading the ring buffer, which maps the store's overlay file; dropping the loaded state then has to unmap it. Validating a checkpoint loads every canister only to compare and drop it: with 200k canisters, each drop took 0.57–0.65 ms, mostly in munmap, waiting for the replica's memory map lock and flushing TLBs.

This leaves the cache empty on load and fills it on the first first_timestamp call. Clones share it until mutated (every mutation sets a fresh one), so the value the background metrics observation fills in a committed state is there in the next round's state too, instead of being recomputed every round.

Full checkpoint loads (at startup, or after state sync) still fill both the header and first-timestamp caches, in the loader threads: ReplicatedState::new_from_checkpoint sequentially reads every cold canister's header (for its memory usage), and the retention metric requires every store's first timestamp. Only validation leaves them empty.

On a testnet node with 200k canisters under load, dropping a loaded canister takes 3.2 µs instead of 634 µs, and validating a checkpoint 15.0 s instead of 19.2 s; most of the rest is syncfs, waiting for the protobufs' writeback. That also shortens the shutdown wait for the latest checkpoint's verification (#12094).

🤖 Generated with Claude Code

…amp cache on first use

Loading a log memory store from a checkpoint no longer reads its ring
buffer to fill the first-timestamp cache; the first `first_timestamp` call
does. Validating a checkpoint loads every canister only to compare and drop
it, and that read mapped each store's overlay file, which dropping it then
had to unmap: 0.6 ms per canister, about 4 s of validation with 200k
canisters.

Clones share the cache until either is mutated, so the value filled when
observing a committed state's metrics carries over to the next round's
state.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

There are no active review issues for the lazy timestamp-cache change.

No open issues 🎉

What changed in this PR

This PR defers initializing a checkpoint-loaded log store’s first-timestamp cache until first use, reducing checkpoint validation overhead and the related shutdown wait.

Changes:

  • Initialize the timestamp cache lazily instead of reading the ring buffer during loading.
  • Share the cache across clones, replacing it on mutation to preserve isolation.
  • Add tests for lazy initialization, cache sharing, and clone mutation isolation.
File Description
rs/​replicated_state/​src/​canister_state/​system_state/​log_memory_store/​tests/​log_memory_store.rs Tests lazy cache initialization and isolation after clone mutation.
rs/​replicated_state/​src/​canister_state/​system_state/​log_memory_store/​mod.rs Implements the shared lazy cache and updates mutation handling and documentation.

Review effort: Balanced

@alin-at-dfinity
alin-at-dfinity marked this pull request as ready for review November 19, 2026 16:14
@alin-at-dfinity
alin-at-dfinity requested a review from a team as a code owner November 19, 2026 16:14
@zeropath-ai

zeropath-ai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

✅ No security or compliance issues detected. Reviewed everything up to 9476c81.

Security Overview
Detected Code Changes
Change Type Relevant files
Enhancement ► rs/replicated_state/src/canister_state/system_state/log_memory_store/mod.rs
       Update cache structures: header_cache and first_timestamp_cache now use Arc<OnceLock<Option>>; introduce fill_caches and lazy initialization semantics
► rs/replicated_state/src/canister_state/system_state/log_memory_store/mod.rs
       Adjust initialization to use Arc-based cache for first_timestamp_cache
► rs/replicated_state/src/canister_state/system_state/log_memory_store/mod.rs
       Refactor to maintain cache synchronization with mutations and checkpoint loads
► rs/replicated_state/src/canister_state/system_state/log_memory_store/tests/log_memory_store.rs
       Add tests for loading from checkpoint and cache filling behavior and mutation effects
► rs/state_manager/src/checkpoint.rs
    Fill log_memory_store caches in parallel during checkpoint loading

…oads

Since loading a log memory store reads nothing from its ring buffer, a
full checkpoint load (at startup, or after state sync) read every store's
header in `ReplicatedState::new_from_checkpoint`, one canister at a time,
summing the cold canisters' memory usage. The loader threads now fill each
store's header and first-timestamp caches as they load it, as loading did
before. Validating a checkpoint still reads nothing.

A store's header cache is now also set to empty after `clear` finds no
ring buffer, and after `deallocate`, like its first-timestamp cache.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 20, 2026 13:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain for the lazy log-store timestamp caching changes.

No open issues 🎉

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the log-store cache initialization and checkpoint-loading changes.

No open issues 🎉

Review effort: Balanced

@alin-at-dfinity
alin-at-dfinity added this pull request to the merge queue Nov 21, 2026
Merged via the queue into master with commit 87617d5 Nov 21, 2026
51 checks passed
@alin-at-dfinity
alin-at-dfinity deleted the alin/log-memory-store-lazy-cache branch November 21, 2026 09:05
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.

3 participants