Skip to content

feat(#185): lazy ONNX init — CLI cold start ~3s → ~20ms - #297

Merged
ajianaz merged 1 commit into
developfrom
feat/185-lazy-embed-init
Jun 12, 2026
Merged

ajianaz merged 1 commit into
developfrom
feat/185-lazy-embed-init

Conversation

@ajianaz

@ajianaz ajianaz commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

What

ONNX embedding model loaded lazily on first use instead of eagerly at store open.

Impact

Command Before After
uteke list ~3s ~20ms
uteke stats ~3s ~17ms
uteke tags ~3s ~20ms
uteke forget ~3s ~20ms
uteke namespace list ~3s ~20ms
uteke doctor ~3s ~20ms
uteke recall ~3s ~3s (unchanged)
uteke remember ~3s ~3s (unchanged)

How

  • embedder field: Mutex<EmbeddingEngine> → Mutex<Option<EmbeddingEngine>>
  • ensure_embedder() lazy-loads ONNX on first embedding call
  • All embed() call sites updated: as_mut().expect() after ensure_embedder()

Closes

Closes #185 (Option 3: lazy init)

… commands

Embedding model is now loaded lazily on first use instead of eagerly
at store open. Commands that don't need embedding (list, get, stats,
tags, forget, namespace, aging, export, doctor, verify) start instantly.

Commands that need embedding (remember, recall, search, import,
consolidate with contradiction) still take ~3s on first use per process
invocation — daemon mode (v0.1.0+) will solve this for repeated calls.

Implementation:
- embedder field changed from Mutex<EmbeddingEngine> to Mutex<Option<EmbeddingEngine>>
- ensure_embedder() lazy-loads ONNX on first embedding call
- All embed() call sites updated to use as_mut().expect() after ensure

Closes #185 (Option 3: lazy init)
"Unknown memory type '{memory_type}'. Valid types: fact, procedure, preference, decision, context"
))
})?;
// Lazy-load embedder on first use
// Embed query outside any lock — CPU-intensive (~50ms), no shared state needed.
// Only the embedder Mutex is held here, allowing concurrent index reads.
// Lazy-load embedder on first use.
self.ensure_embedder()?;
@github-actions

Copy link
Copy Markdown

🔍 Cora AI Code Review

❌ Blocked — critical issues found.

🔴 Error (2)

  • crates/uteke-core/src/operations.rs:41 — In remember, the code locks self.embedder and immediately calls .embed(content)? on the guarded engine. The new lazy-init path makes the embedder a shared Mutex<Option<EmbeddingEngine>>, but inference now still executes while the mutex is held: self.embedder.lock()?.as_mut()?.embed(content)?. The comment in recall says embedding is CPU-intensive and should allow concurrent index reads, but this mutex also serializes all embedding requests across threads for the entire model call, which can become a significant bottleneck after lazy loading.
  • crates/uteke-core/src/operations.rs:126 — In recall, self.ensure_embedder()? is followed by self.embedder.lock()?.as_mut()?.embed(query)?. Because .embed(query) is invoked while the mutex guard is alive, every concurrent recall/remember call must wait for the previous embedding inference to finish. This is especially noticeable since the surrounding comment explicitly describes embedding as CPU-intensive. The lazy-loading change avoids startup cost, but it also centralizes all future embeddings behind a single exclusive lock.

Review powered by cora-cli · BYOK · MIT

@ajianaz
ajianaz merged commit ef36160 into develop Jun 12, 2026
8 of 9 checks passed
@ajianaz
ajianaz deleted the feat/185-lazy-embed-init branch June 12, 2026 03:03
ajianaz added a commit that referenced this pull request Aug 13, 2026
… commands (#297)

Embedding model is now loaded lazily on first use instead of eagerly
at store open. Commands that don't need embedding (list, get, stats,
tags, forget, namespace, aging, export, doctor, verify) start instantly.

Commands that need embedding (remember, recall, search, import,
consolidate with contradiction) still take ~3s on first use per process
invocation — daemon mode (v0.1.0+) will solve this for repeated calls.

Implementation:
- embedder field changed from Mutex<EmbeddingEngine> to Mutex<Option<EmbeddingEngine>>
- ensure_embedder() lazy-loads ONNX on first embedding call
- All embed() call sites updated to use as_mut().expect() after ensure

Closes #185 (Option 3: lazy init)
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.

enhancement: reduce CLI cold start time

2 participants