Skip to content

perf: fix index node cache thrashing on native + entity attr caching - #21

Open
tiensonqin wants to merge 3 commits into
mainfrom
devin/entity-lookup-cache
Open

tiensonqin wants to merge 3 commits into
mainfrom
devin/entity-lookup-cache

Conversation

@tiensonqin

@tiensonqin tiensonqin commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes the native daemon being ~2× slower than the Melange/JS daemon on read endpoints. Profiling a warm daemon showed handler CPU dominated by sqlite reloads + transit decode of storage-backed index nodes.

Root cause: persistent-sorted-set caches restored nodes behind OCaml weak refs, which are cleared on every major GC collection. Under any allocation pressure the cache thrashes, so every index slice re-reads nodes from sqlite and re-transit-decodes datoms. Upstream relies on js/WeakRef, which survives V8 minor GCs — the effective cache lifetime is much longer there.

Changes:

  • Platform.strong_index_node_cache (new platform flag): true on native, false on melange/jsoo. Storage.settings_of_root overrides the stored ref-type to Strong on native only — an in-memory cache policy; stored metadata unchanged.
  • Entity.lazy_entity caches forward-attr lookups per entity (lookup_cache on the materialized assoc list) instead of re-scanning datoms_by_entity per attribute; forward_entity_attr single-attr scan helper dropped as unused.

Measured on macOS arm64, thread-api/api-list-pages over a 3000-block graph (60 calls, p50): 12.3ms → ~4ms daemon-internal handler ~10ms → ~1.9ms. get-page-data on a 12000-block graph: 284ms vs 715ms in the JS daemon. test_entity and the rest of the suite still pass.

Link to Devin session: https://app.devin.ai/sessions/f641a5e9845f45108c4a98cb82e268d3
Open in Devin Desktop: https://app.devin.ai/desktop/session/f641a5e9845f45108c4a98cb82e268d3?variant=devin
Requested by: @tiensonqin


Devin Review

@devin-ai-integration

Copy link
Copy Markdown

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@devin-ai-integration devin-ai-integration 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.

✅ Devin resolved all 2 findings on 8be3288

Fixed by Devin (2)

  • Native restores permanently change cache policy
  • Unrelated reference blocks scalar lookups

View all findings in Devin Review

Devin Review

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