Skip to content

fix: preserve known cardinality when restoring stored sets - #2

Merged
RCmerci merged 1 commit into
mainfrom
fix/restore-known-count
Oct 1, 2026
Merged

RCmerci merged 1 commit into
mainfrom
fix/restore-known-count

Conversation

@RCmerci

@RCmerci RCmerci commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Restoring a stored set currently discards its known cardinality. A later count folds the entire tree, even when the caller has already persisted the exact count for that root snapshot.

Add a backwards-compatible ?count:int argument to restore and seed the existing immutable count cache. Existing callers that omit it retain lazy traversal-based counting. Reject negative counts without reading storage. The documented contract requires the caller to supply the exact cardinality of the addressed snapshot; the API does not scan nodes to verify a hint.

The existing successful add/remove paths already update a known count; duplicate inserts and absent removals preserve it. Tests cover those paths, empty sets, invalid counts, persistence of the original set, and the old no-count API. Melange and js_of_ocaml smoke tests exercise zero-read repeated counts too.

Validation on macOS arm64, OCaml 5.5.0, Dune 3.24.2, Node 22.21.1:

  • A test written before the change failed because counting a cold restored 4096-element tree read 1365 stored nodes; the same test now passes with zero reads for repeated counts.
  • Complete dune runtest --force --display=verbose: native, Melange/Node, js_of_ocaml/Node, Node GC and Chrome headless memory actions all passed.
  • Strict upstream test-name coverage: 15/15; browser-memory CI wiring check passed. Name coverage is not behavioral equivalence.

A companion DataScript change passes the counts from its stored root before replaying the tail and pins this exact PSS commit. In the combined before/after probe, counts remain 4096/4096/2048 while both the first and second count change from 333 index-node visits / 10240 leaf values to zero visits. DataScript snapshot depth scanning is a separate cost and remains unchanged.

Local experiments use synthetic memory storage, not a personal graph. No heap or wall-time speedup is claimed.

Companion DataScript PR: logseq/datascript-ocaml#23 . It pins this PR head exactly; suggested merge order is PSS then DataScript.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-01T11:44:02.446354Z 9c99ee9 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@RCmerci
RCmerci merged commit df53f5d into main Oct 1, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant