Skip to content

perf(staleness): ask pages that share a filter once, not once per page (#6325) - #6407

Merged
nicoloboschi merged 2 commits into
mainfrom
triage/6325
Nov 6, 2026
Merged

nicoloboschi merged 2 commits into
mainfrom
triage/6325

Conversation

@nicoloboschi

Copy link
Copy Markdown
Collaborator

Fixes #6325. Thanks @harshitgupta31415 for the report, the benchmark and the patch idea.

What was wrong

The knowledge tree and the mental-model list check every page to see if it's out of date. Pages that watch the same tags and fact types each re-read the same matching memories, so 53 pages on one shared tag meant 53 reads of the same rows. Only each page's cutoff differs.

What changes

  • Pages in a batch are grouped by filter (same tags, same fact types). Each filter is asked once for its newest matching write, and every page compares its own cutoff against it.
  • The tag-index query uses max(...) FILTER (WHERE updated_at IS NOT NULL). A bare max lets Postgres walk the whole bank from newest to oldest, which made a filter that matches nothing go from 0.5 ms to 15 ms in testing. The FILTER keeps it on the tag index.
  • The time-ordered query (non-strict modes) walks once per filter, starting from the earliest cutoff in it.
  • Answers are unchanged. Oracle and compound tag_groups keep their per-scope path.

Not taken from the issue's patch: the "check the bank's newest 64 writes first" step. When the newest writes belong to another scope, the check misses and both steps run, so it was slower in testing (34 ms against 18 ms on the shared-tag case below).

Numbers

100k memories, Postgres 18, JIT off, median of 5 runs. Every case gave the same answers as the single-page check.

53 pages… Before After
one shared tag 903.7 ms 17.5 ms
four tag sets 289.1 ms 22.0 ms
rare tag 11.4 ms 0.45 ms
tag matching nothing 0.52 ms 0.25 ms
shared tag, any mode 15.8 ms 0.66 ms
8 different filters 938.5 ms 145.6 ms

Tests

  • New test_pages_sharing_a_filter_each_keep_their_own_cutoff covers both query shapes, with cutoffs before, at and after the write, tags listed in a different order, and a fact-type filter that must stay separate. It fails if the walk starts from the latest cutoff instead of the earliest.
  • test_mental_models.py and test_knowledge_base.py: 241 passed locally. 8 errored only because no LLM key was set locally, and they error the same way on main.

#6325)

Pages with the same tags and fact types were each re-reading the same
matching memories to decide if they are stale. Now each distinct filter is
asked for its newest matching write once, and every page compares its own
cutoff against it. The aggregate keeps a FILTER so Postgres stays on the tag
index instead of walking the whole bank for a filter that matches nothing.

100k memories, 53 pages on one shared tag: 904 ms -> 18 ms. Same answers as
the single-scope check in every case measured.

Reported with a patch by @harshitgupta31415.
…ted_at directly

- _ScopeFilterKey replaces a bare tuple key, matching _ScopeGroupKey next to it
- comment on the test's direct read of memory_units.updated_at
@nicoloboschi
nicoloboschi merged commit d5d2faa into main Nov 6, 2026
223 of 224 checks passed
@nicoloboschi
nicoloboschi deleted the triage/6325 branch November 6, 2026 14:30
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.

reuse freshness checks for pages with the same scope (8.66 s → 5.44 ms, 1,593× faster)

1 participant