Skip to content

Cache stale_counts on a store change counter (per-command cost flat in session size) - #255

Merged
chouswei merged 1 commit into
masterfrom
perf/stale-scan-cache
Nov 4, 2026
Merged

chouswei merged 1 commit into
masterfrom
perf/stale-scan-cache

Conversation

@chouswei

@chouswei chouswei commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Why

Owner goal: per-command cost must not grow with session size, since sessions are uncapped (t1014s4254). Every command goes through cli._load_session -> emit_stale_warnings -> stale_counts, which scanned every row each time (about 97% of the size-dependent cost; see findings).

What

  • mem_store.py: MemStore.change_count (MemStore._version + RecordTable.version). Every mutator bumps it: all RecordTable dict mutators and index helpers, and MemStore load, upsert, delete, restore, rename, reindex, index/unindex of tag and edge, high-water raise, mark_dirty.
  • housekeep.py: stale_counts caches its result on the MemStore (so keyed on store identity) with the counter. Reads skip the scan; the first command after a write rescans once. use_cache=False forces the scan.
  • @WRN output is unchanged. upgrade.py, serve.py, serve_pool.py and sysml-models are untouched.

Tests

tests/test_stale_scan_cache.py:

  • (a) test_warnings_identical_across_writes_deletes_edges_recycle, test_warnings_identical_through_cli_commands, test_read_skips_scan_write_rescans_once, test_cache_is_per_store
  • (b) test_every_memstore_method_is_classified, test_every_recordtable_method_is_classified (a new method must be listed as mutator or reader), test_memstore_mutator_bumps_change_count[*], test_recordtable_mutator_bumps_version[*], test_readers_leave_change_count_alone, test_state_change_always_bumps
  • (c) test_read_command_time_flat_in_session_size (find at 5k within 1.5x of 1k; fails with the cache disabled)

Full pytest passes locally; ruff check and ruff format --check clean on the CI paths.

Numbers

Same machine, ms per command (prof2.py):

size cmd before after
1k find 0.95 0.20
1k typed-walk 3 hop 0.99 0.21
5k find 4.46 0.20
5k typed-walk 3 hop 4.48 0.21

…scan

MemStore.change_count (MemStore._version + RecordTable.version) is bumped by every
mutator. housekeep.stale_counts caches its result on the MemStore keyed on it, so a
read command does no scan and the first command after a write rescans once. @wrn
output is unchanged. Tests enumerate MemStore/RecordTable methods and fail when a
new one is not classified as mutator (bumps) or reader.
@chouswei
chouswei merged commit 51c351c into master Nov 4, 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