perf: cut TUI startup from ~20s to ~0.15s (lazy-import litellm + IPv4-first) - #15
Conversation
Importing graybox.cli dropped from ~1.4s to ~0.14s. litellm and its ~900 modules are now only loaded when a command that uses the LLM runs (organize/ask/chat/dupes/merge/edit/delete/refresh). Imports moved from module level into the command functions: - graybox.ai.AIService - graybox.retrieval (ask, ConversationTurn) - graybox.organizer (organize_all) - graybox.curate (find_possible_duplicates, merge_pages, edit_page, delete_page) - graybox.summarizer (refresh_all_summaries)
litellm's own import does a blocking network fetch of its model-cost map, which hangs ~20s on networks without routable IPv6 (getaddrinfo lists IPv6 first). Two fixes, both applied before litellm is imported: - Reorder getaddrinfo results so IPv4 is tried first. - Set LITELLM_LOCAL_MODEL_COST_MAP so the bundled local cost map is used instead of a network fetch — faster for everyone. Tests cover the reordering, the patching, and that both happen before the litellm import.
|
Hi @gilsonolegario, Request changes:
|
|
Thanks for the thorough review — the points are valid and both are addressed in the latest commit.
Full suite passes (243 tests on this branch). Happy to adjust if anything else stands out. |
Summary
graybox.clistartup currently takes ~20s because importing it pulls in litellm, whose own import does a blocking network fetch of its model-cost map (and hangs ~20s on networks without routable IPv6). This PR makes importing the CLI ~0.14s — a ~150x speedup.Changes
Two commits:
perf: lazy-import LLM modules for fast startup— litellm and its ~900 modules are no longer imported at module level. They load only when a command actually needs the LLM (organize/ask/chat/dupes/merge/edit/delete/refresh).perf: prefer IPv4 and local cost map in litellm startup— two fixes applied before litellm is imported:getaddrinforesults so IPv4 is tried first (avoids the ~20s hang on broken-IPv6 networks).LITELLM_LOCAL_MODEL_COST_MAPso litellm's bundled local cost map is used instead of a network fetch.Verification
python3 -c "import time; t0=time.time(); import graybox.cli; print(time.time()-t0)"Why
The interactive TUI (
graybox.cliwith no args) is the primary way people run Gray Box. Waiting ~20s for a splash screen that doesn't even use the LLM is a poor first impression and unnecessary work.