Skip to content

perf: replace quadratic List patterns on transact/storage hot paths - #28

Merged
tiensonqin merged 1 commit into
mainfrom
devin/1790924166-list-perf
Oct 2, 2026
Merged

tiensonqin merged 1 commit into
mainfrom
devin/1790924166-list-perf

Conversation

@tiensonqin

Copy link
Copy Markdown
Contributor

Summary

Removes the worst quadratic List patterns found by a List-API audit of the engine hot paths (queries returning 30k–70k rows; GC ~26%, List ops ~10% in profiles), and adds a performance skill documenting the rules.

  • Db.diff — three whole-db List.exists (same_fact d) scans per datom made it O(n×m). Now builds (e, a) -> v list Hashtbl fact tables for both sides, preserving same_fact semantics exactly (value_equal treats NaN=NaN, so v is bucketed per (e,a) rather than hashed directly).
  • Db.refresh_indexes_with_tx_data — duplicate tables were filtered and re-sorted once per retracted datom. remove_facts_from_duplicate_tables now buckets all retracted facts by (e,a) and rebuilds each duplicate table once per tx. Per-datom PSet.remove calls are unchanged; Util.compare_datom includes tx, so stale lookups mid-fold only yield no-op removes.
  • Db.find_active_datom_by_fact — List.sort |> List.hd → single min-fold under compare_datom Eavt.
  • Storage.collect_garbage — List.mem address per storage node over live → Hashtbl live set.
  • Transact.dedupe_facts — assoc-list fold rebuild per datom → (entity_id * attr) Hashtbl buckets; explicit List.rev keeps the old fold's output order.

New .agents/skills/performance/SKILL.md records index-access discipline (bounded seeks vs whole-index walks), the banned hot-path List patterns, and the audited hot-path list.

Verified: dune build impl/ clean; all 18 native test exes pass (test_datascript, test_db, test_core, test_issues, test_entity, test_storage, test_upsert, test_tuples, test_serialize, test_listen, test_conn, test_pull_api, test_built_ins, test_lookup_refs, test_query_pull, test_query_engine_fixes, test_util, test_query_namespace).

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

- db.ml diff: (e,a)->values Hashtbl fact tables instead of pairwise List.exists
- db.ml refresh_indexes_with_tx_data: batch duplicate-table retract rebuild per tx
- db.ml find_active_datom_by_fact: min-fold instead of sort+hd
- storage.ml collect_garbage: Hashtbl live set instead of List.mem per node
- transact.ml dedupe_facts: (eid,attr)->datoms Hashtbl buckets, order preserved
- add .agents/skills/performance (index discipline + banned hot-path patterns)
@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

@tiensonqin
tiensonqin merged commit ca87eb1 into main Oct 2, 2026
2 checks passed
@tiensonqin
tiensonqin deleted the devin/1790924166-list-perf branch October 2, 2026 07:03
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