Skip to content

Slow app startup due to heavy library imports #12

@NetZissou

Description

@NetZissou

Problem

The app has a long startup time because heavy libraries (FAISS, torch, open_clip, cuML) are being imported at module load time, even when they aren't needed immediately.

Evidence from logs

[2026-01-29 11:14:11] INFO [faiss.loader] Loading faiss with AVX512 support.
[2026-01-29 11:14:11] INFO [faiss.loader] Successfully loaded faiss with AVX512 support.

These messages appear during app startup before any user action.

Expected behavior

Heavy libraries should only be loaded when explicitly needed:

  • FAISS: only when user selects FAISS backend or auto-resolution chooses it
  • torch/open_clip: only when user runs embedding generation
  • cuML: only when user selects cuML backend

Root cause

Multiple files have module-level imports of heavy libraries:

  • shared/utils/clustering.py - imports sklearn, UMAP at module level
  • shared/utils/models.py - imports open_clip at module level
  • shared/services/embedding_service.py - imports torch and open_clip at module level
  • shared/components/clustering_controls.py - imports faiss and cuml for availability check
  • shared/utils/backend.py - availability checks aren't cached

Potential solution

Implement lazy loading pattern:

  1. Convert module-level imports to lazy-load functions
  2. Cache availability checks to avoid repeated imports
  3. Only import heavy libraries when their functionality is actually invoked

Notes

An initial lazy loading implementation was attempted but reverted as it caused other issues. A more careful approach is needed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions