openhuman icon indicating copy to clipboard operation
openhuman copied to clipboard

perf(composio): hoist per-connection registry read out of periodic loop

Open mysma-9403 opened this issue 5 months ago • 1 comments

Summary

  • Read the memory_sources registry once per periodic tick and index per-source caps by connection_id, instead of re-reading + linearly scanning it for every active connection (an N+1).
  • Inside the per-connection loop, cap lookup is now an O(1) HashMap hit.
  • First-match-wins on duplicate connection_id is preserved (matches the old .find()), and the lookup stays non-fatal (empty map ⇒ sync without caps).

Problem

run_one_tick in src/openhuman/memory_sync/composio/periodic.rs resolved per-source caps (max_items / sync_depth_days) by calling memory_sources::list_enabled_by_kind(Composio).await and then .find()-ing the matching source inside the for conn in resp.connections loop. That is a classic N+1: one process-global registry read plus an O(sources) scan per connection, repeated every tick (~20 min) for every connection the user has connected.

Solution

  • Extract a pure index_caps_by_connection(sources) -> HashMap<connection_id, (max_items, sync_depth_days)> helper.
  • Call list_enabled_by_kind(Composio) once before the loop and build the index; the loop body does an O(1) get.
  • or_insert keeps the first entry for a duplicate connection_id, preserving the old .find() "first match wins" semantics; entries without a connection_id are dropped (they can never match a Composio connection).
  • Behaviour parity on failure: an empty map means "sync without caps", exactly as the old per-connection unwrap_or_default().find()...unwrap_or((None, None)) did.

Submission Checklist

  • [x] Tests added or updated (happy path + at least one failure / edge case) — index_caps_* tests: caps mapping (happy), first-match-wins on duplicate connection_id, and dropping entries without a connection_id (edge cases).
  • [x] Diff coverage ≥ 80% — the substantive changed logic is the index_caps_by_connection helper, fully exercised by the three new tests (cargo test --lib composio::periodic::tests::index_caps, 3 passed).
  • [x] Coverage matrix updated — N/A: internal performance change, no feature added/removed/renamed.
  • [x] All affected feature IDs from the matrix are listed under ## Related — N/A: no feature IDs touched.
  • [x] No new external network dependencies introduced — N/A: removes redundant registry reads; no new deps; tests use in-memory entries.
  • [x] Manual smoke checklist updated if this touches release-cut surfaces — N/A: no user-visible surface change.
  • [x] Linked issue closed via Closes #NNN — N/A: no tracking issue.

Impact

  • Runtime: Rust core periodic Composio sync tick only. No desktop/mobile/web/CLI surface change.
  • Performance: each tick does 1 registry read + O(1) lookups instead of N reads + O(N×M) scans; cheaper recurring background work.
  • Compatibility: identical caps applied per connection (first-match-wins preserved); only the read pattern changed.

Related

  • Closes: N/A
  • Follow-up PR(s)/TODOs: N/A

AI Authored PR Metadata (required for Codex/Linear PRs)

Linear Issue

  • Key: N/A
  • URL: N/A

Commit & Branch

  • Branch: perf/composio-periodic-n1-hoist
  • Commit SHA: a0eb54c46f5f5fd2c27746a54012de5bb5a050a6

Validation Run

  • [x] pnpm --filter openhuman-app format:check — N/A: Rust-only, no app/ changes.
  • [x] pnpm typecheck — N/A: no TypeScript changes.
  • [x] Focused tests: cargo test --manifest-path Cargo.toml --lib composio::periodic::tests::index_caps → 3 passed.
  • [x] Rust fmt/check (if changed): cargo fmt clean; lib test build exit 0.
  • [x] Tauri fmt/check (if changed): N/A: no Tauri shell changes (pre-push pnpm rust:check passed).

Validation Blocked

  • command: N/A
  • error: N/A
  • impact: N/A

Behavior Changes

  • Intended behavior change: per-source caps are read once per tick and indexed, not re-read per connection.
  • User-visible effect: none — same caps applied; lower background CPU/IO per tick.

Parity Contract

  • Legacy behavior preserved: first-match-wins on duplicate connection_id; empty/failed read ⇒ no caps; per-connection caps identical.
  • Guard/fallback/dispatch parity checks: unwrap_or_default on the single read keeps the read non-fatal.

Duplicate / Superseded PR Handling

  • Duplicate PR(s): N/A
  • Canonical PR: this PR
  • Resolution: N/A

Summary by CodeRabbit

  • Performance

    • Reduced work during periodic syncs by precomputing per-connection capabilities, cutting repeated registry scans and improving lookup efficiency.
  • Tests

    • Added tests validating connection-to-capability mapping, ensuring first-match precedence for duplicates and ignoring entries lacking a connection identifier.

mysma-9403 avatar Jun 06 '26 13:06 mysma-9403

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 8a5780b9-9244-4868-b5e4-a2496222091a

📥 Commits

Reviewing files that changed from the base of the PR and between a0eb54c46f5f5fd2c27746a54012de5bb5a050a6 and a4bb7db73f8058ed6af184ef832b4cf815cd189e.

📒 Files selected for processing (1)
  • src/openhuman/memory_sync/composio/periodic.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/openhuman/memory_sync/composio/periodic.rs

📝 Walkthrough

Walkthrough

Per-tick precomputation now reads enabled Composio memory_sources once, builds a connection_id -> caps map with first-match semantics, and replaces per-connection registry scans with O(1) lookups; a helper and unit tests validate index construction and edge cases.

Changes

Composio periodic sync optimization

Layer / File(s) Summary
Tick-level index precomputation and per-connection lookup
src/openhuman/memory_sync/composio/periodic.rs
Per-tick read of enabled Composio memory_sources and construction of caps_by_connection (lines 381–405), and replacement of per-connection registry scans with direct O(1) lookups using conn.id (lines 453–458).
Index building helper function and unit tests
src/openhuman/memory_sync/composio/periodic.rs
Adds index_caps_by_connection implementation and unit tests (lines 582–664) to map connection_id -> (max_items, sync_depth_days) with first-match-wins and dropping entries without connection_id.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I hopped through code at tick of the clock,
One registry read now — no needless walk.
First match holds fast, duplicates set aside,
Caps mapped and tidy, per-conn lookups glide.
A little index, a lot less stride.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly reflects the main performance optimization: hoisting a per-connection registry read out of the periodic loop to reduce N+1 query patterns.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Jun 06 '26 13:06 coderabbitai[bot]

Closing as obsolete. The N+1 this PR removed — re-reading the memory_sources registry inside the per-connection loop — was independently eliminated on main by #2831, which goes further: instead of reading the registry once per tick (as this PR did), it reuses the already-loaded config.memory_sources snapshot and reads the registry zero extra times. #2831 also added a per-source enabled gate (decide_periodic_source Skip path) that this PR's caps_by_connection indexing predates and would regress. The optimization target no longer exists, so this is superseded.

mysma-9403 avatar Jun 12 '26 14:06 mysma-9403