Skip to content

perf: optimize cold-start journal loading and add startup stage metrics - #191

Merged
RCmerci merged 14 commits into
mainfrom
devin/1795764519-optimize-journal-cold-start
Nov 27, 2026
Merged

RCmerci merged 14 commits into
mainfrom
devin/1795764519-optimize-journal-cold-start

Conversation

@tiensonqin

@tiensonqin tiensonqin commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Optimize cold-start journal loading across SQLite storage, overlay DB open, feed planning, eager Swift runtime bootstrap, SwiftUI host mounting, and JournalList first-frame layout, and add structured stage-level startup timing logs (OSLog com.logseq.journal:startup and journal-startup: stderr JSON).

Key Changes

  • SQLite open/restore & shared overlay connection (logseq_db_storage/lib/logseq_sqlite_storage.ml, mutation_receipt_store.ml, logseq_overlay_db/lib/database.ml):
    • Replace WHERE typeof(addr) <> 'integer' OR addr < 0 LIMIT 1 full-table scan with primary-key index probe (WHERE addr < 0 LIMIT 1) and reuse prepared kvs statements with integer binding.
    • Reuse a single temporary SQLite connection in open_owned across outbox read, receipt initialization, and legacy outbox migration instead of opening 3 separate connections.
    • Skip redundant CREATE TABLE IF NOT EXISTS in mutation_receipt_store when tables already exist and skip redundant outbox migration when already migrated.
    • Collapse authoritative_tree_candidate to a single EAVT scan while preserving single-valued attribute rejection.
  • Bounded concurrent root previews (app/journal_graph_feed_plan.ml, test/journal_graph_runtime_locality_test.ml):
    • Dispatch initial Preview_children requests concurrently using a bounded queue (max_concurrent_root_previews = 8) instead of strictly serial root-by-root round-trips through Worker → MainActor → LUI, staying within dispatcher_capacity across concurrent resident graphs.
  • Eager runtime bootstrap & deferred root view mount (swift/App.swift, swift/JournalRuntimeHost.swift, swift/JournalRuntime.swift, apple-tests/warm-start/JournalWarmStartAcceptance.swift):
    • Start JournalRuntime eagerly in App.init() (and the warm-start probe) so OCaml/SQLite/feed work overlaps iOS window creation, handing the prestarted runtime to JournalRuntimeHost and observing initial account state (initial: true) on JournalHost.
    • Add bounded waitForStartupContent(timeoutMilliseconds: 200) so LUISwiftUIRoot waits for journal-list (or a ready non-timeline interactive screen) instead of mounting the loading UI early and starving the worker pump on the MainActor.
  • Reduce JournalList first-frame overhead (swift/JournalList.swift):
    • Resolve the hosting UIScrollView synchronously in resolveIfNeeded() and invoke presentationLayout synchronously in TopScroll.didLayout() to remove an extra async turn before initial cover dismissal.
    • Avoid wrapping non-disclosure rows in AnyView and skip attaching .contextMenu modifiers when a row has no context menu actions.
  • Startup stage instrumentation (swift/JournalRuntime*.swift, app/application.ml, logseq_db_worker, logseq_sync, logseq_overlay_db):
    • Emit structured stage timings across Swift host startup, platform requests, mirror inspection, key unlock, database open/restore, feed loading, and timelinePresented (ignoring Sys_error when stderr is unwritable).

iOS Simulator Measurements (iPhone 17 / iOS 26.5, 500 roots / 35 children / 7 days)

  • 50k blocks SQLite reopen: 25.0 ms → 5.47 ms (median)
  • overlay.open_owned: 11.85 ms → 5.81–7.38 ms (median 6.56 ms)
  • Load_feed (30 initial roots): 107.2 ms → 44.5 ms
  • End-to-end app.timeline_presented (in-app): 664 ms → 240–268 ms (median 260 ms, ~2.55× faster)
  • 60fps visual cold-start (full-screen app frame → Timeline rendered): 883 ms → 633 ms (-250 ms)

Before vs After Cold-Start Comparison

Journal cold-start before vs after PR #191

Validation

  • dune exec test/source_boundary_test.exe -- . (passed)
  • dune exec test/application_view_test.exe -- -q (232/232 passed)
  • dune exec test/journal_graph_runtime_locality_test.exe -- -q (229/229 passed)
  • dune exec logseq_overlay_db/test/test_overlay_storage.exe -- -q (106/106 passed)
  • dune exec test/logseq_db_worker_application_integration_test.exe -- -q (10/10 passed)
  • Rebuilt and measured iOS Simulator probe app on isolated simulator.

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


🟢 Devin Review found nothing to fix · View findings · Follow Devin's work

Devin Review

@devin-ai-integration

Copy link
Copy Markdown
Contributor

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
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Newer findings are available below. Devin Review posted a newer report on this PR, in addition to the findings presented here.

Devin Review found 3 findings. Devin automatically fixed all 3 findings.

Fixed by Devin (3)
  • Host formatting breaks the boundary test — Restored single-line JournalRuntimeHost(platform: platform, ...) call in swift/JournalRuntimeHost.swift and verified test/source_boundary_test.exe passes.
  • Early account lookup risks skipping sign-in — Added initial: true to both platform.authenticationRequired and platform.localAccountAvailable onChange modifiers in JournalHost so an early account lookup resolving before mount still opens the automatic sign-in sheet.
  • Extra queries delay the startup timeline — Reverted the 12-root initial-slice split in start_feed_scan so initial 30-root loads complete in a single verified batch without an extra Timeline.Roots round-trip before the startup feed_loaded gate opens.

View findings in Devin Review · Follow Devin's work

Devin Review

@devin-ai-integration devin-ai-integration Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Newer findings are available below. Devin Review posted a newer report on this PR, in addition to the findings presented here.

Devin Review found 1 finding. Devin did not fix them (the PR is muted); it is left for you to triage below.

View findings in Devin Review · Follow Devin's work

Devin Review

Comment on lines +191 to +197
private var visible: Bool {
#if os(iOS)
active && (initial ? clock?.elapsed == true : loading)
#else
active && (loading || (initial && clock?.elapsed == true))
#endif
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Missing progress during iOS startup

When the startup reducer signals loading during a cold graph restore, visible ignores it. The loading surface shows no progress until the five-second launch deadline, even if the restore finishes before then.

Learn more

The startup reducer owns the loading state once the application is running. The startup timer triggers that state after 100 ms of waiting, and manager_props passes it to the startup screen. The native startup renderer forwards it as loading. The five-second native deadline also applies before that state exists, but it need not override an explicit loading signal once the application has sent one.

Example: Open the app on an iPhone with a saved graph that takes two seconds to restore. The startup reducer requests a loading indicator while the graph opens, but the iOS startup surface never displays it before the timeline replaces it.

Recommended fix: Keep the native deadline as a fallback for initial surfaces without an explicit loading signal. Honor loading whenever the surface is active, including during the first attempt.

Suggested change
private var visible: Bool {
#if os(iOS)
active && (initial ? clock?.elapsed == true : loading)
#else
active && (loading || (initial && clock?.elapsed == true))
#endif
}
private var visible: Bool { active && (loading || (initial && clock?.elapsed == true)) }

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

@devin-ai-integration devin-ai-integration Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Newer findings are available below. Devin Review posted a newer report on this PR, in addition to the findings presented here.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

devin-ai-integration[bot]

This comment was marked as resolved.

@devin-ai-integration devin-ai-integration Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Newer findings are available below. Devin Review posted a newer report on this PR, in addition to the findings presented here.

Devin Review found 2 findings. Devin did not fix them (the PR is muted); all 2 findings are left for you to triage below.

View findings in Devin Review · Follow Devin's work

Devin Review

Comment thread swift/JournalChrome.swift
Comment on lines +1735 to +1738
.onChange(of: properties.canChoose == true && properties.active == true && context.isUserInteractionEnabled && startupClock?.phase == .pickerRequested, initial: true) { _, eligible in
if eligible { startupClock?.deliverManagerPicker {
JournalExtensions.emit(context: context, payload: Data("choose-graph".utf8))
} }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Automatic picker handoff lacks iOS coverage

The clock test calls deliverPicker directly. The manager test injects the native event, bypassing the SwiftUI handoff. Neither verifies that a pending request opens the picker and clears when it appears.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +191 to +192
if let action { action }
else { Text("Opening graph picker…") }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Pending picker loses its status message

While a picker request is pending, JournalStartupLoadingSurface treats the manager’s empty action column as a visible control. The manager supplies that column before graph selection is available, leaving neither a status message nor an action.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

@RCmerci
RCmerci merged commit c87ef4c into main Nov 27, 2026
2 checks passed

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

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.

2 participants