mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-12-05 01:26:07 +09:00
## Summary Major performance optimization for lyrics processing pipeline. Analysis of job 36c21ece revealed the lyrics transcription stage alone took **16 minutes 28 seconds** - the target is 10 minutes for the entire pipeline. ### Key Bottlenecks Identified & Fixed | Bottleneck | Time Lost | Fix | Status | |------------|-----------|-----|--------| | Langfuse callback init | 201s | Preload at startup | **Done** | | SyllablesMatchHandler x2 | 158s | Preload NLTK + singleton | **Done** | | Model init timeouts | 300s+ | Warmup + share instance | **Done** | | Sequential anchor search | 38s | Parallelize | **Done** | ### Changes **Phase 1 (Quick Fixes):** - [x] Add NLTK preloader (`backend/services/nltk_preloader.py`) - [x] Add Langfuse preloader (`backend/services/langfuse_preloader.py`) - [x] Add `warmup()` method to LangChainBridge for eager model init - [x] Update SyllablesMatchHandler to use preloaded cmudict - [x] Update ModelFactory to use preloaded Langfuse handler **Phase 2 (Architecture):** - [x] Parallelize anchor sequence search (ThreadPoolExecutor, 4 workers) - [x] Add `_process_ngram_length_no_state()` for thread-safe processing - [x] Design document for future GCE lyrics worker ### Expected Impact - **Before**: ~30 minutes total, 16+ minutes for lyrics stage - **After**: ~18 minutes total, ~5 minutes for lyrics stage - **Savings**: ~11 minutes per job ### Configuration New environment variables: - `ANCHOR_SEARCH_WORKERS`: Number of parallel workers for anchor search (default: 4) - `ANCHOR_SEARCH_SEQUENTIAL`: Set to "1" to disable parallel processing ## Test plan - [x] Unit tests pass: `make test` (1020 passed) - [ ] Deploy to staging and run E2E test with same song - [ ] Compare job times before/after See detailed analysis: `docs/archive/2026-01-08-performance-investigation.md` @coderabbitai ignore 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
123 lines
3.5 KiB
Python
123 lines
3.5 KiB
Python
"""NLTK resource preloader for container startup.
|
|
|
|
Loads NLTK data at container startup to avoid slow downloads during request processing.
|
|
Cloud Run's ephemeral filesystem means NLTK data must be re-downloaded on each cold start,
|
|
which can take 30-100+ seconds for cmudict.
|
|
|
|
See docs/archive/2026-01-08-performance-investigation.md for background.
|
|
"""
|
|
|
|
import logging
|
|
import time
|
|
from typing import Optional, Dict, Any
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Singleton storage for preloaded resources
|
|
_preloaded_resources: Dict[str, Any] = {}
|
|
|
|
|
|
def preload_nltk_cmudict() -> None:
|
|
"""Preload NLTK's CMU Pronouncing Dictionary at startup.
|
|
|
|
The cmudict is used by SyllablesMatchHandler for syllable counting.
|
|
Without preloading, each SyllablesMatchHandler init downloads ~30MB,
|
|
which took 50-100+ seconds in Cloud Run.
|
|
"""
|
|
global _preloaded_resources
|
|
|
|
if "cmudict" in _preloaded_resources:
|
|
logger.info("NLTK cmudict already preloaded")
|
|
return
|
|
|
|
logger.info("Preloading NLTK cmudict...")
|
|
start_time = time.time()
|
|
|
|
try:
|
|
import nltk
|
|
|
|
# Ensure the data is downloaded
|
|
try:
|
|
from nltk.corpus import cmudict
|
|
|
|
# Try to access it - will raise LookupError if not downloaded
|
|
_ = cmudict.dict()
|
|
except LookupError:
|
|
logger.info("Downloading NLTK cmudict data...")
|
|
nltk.download("cmudict", quiet=True)
|
|
from nltk.corpus import cmudict
|
|
|
|
# Load into memory
|
|
cmu_dict = cmudict.dict()
|
|
_preloaded_resources["cmudict"] = cmu_dict
|
|
|
|
elapsed = time.time() - start_time
|
|
logger.info(f"NLTK cmudict preloaded in {elapsed:.2f}s ({len(cmu_dict)} entries)")
|
|
|
|
except Exception as e:
|
|
logger.error(f"Failed to preload NLTK cmudict: {e}")
|
|
raise
|
|
|
|
|
|
def get_preloaded_cmudict() -> Optional[Dict]:
|
|
"""Get the preloaded CMU dictionary if available.
|
|
|
|
Returns:
|
|
The preloaded cmudict dictionary, or None if not preloaded
|
|
"""
|
|
return _preloaded_resources.get("cmudict")
|
|
|
|
|
|
def is_cmudict_preloaded() -> bool:
|
|
"""Check if cmudict has been preloaded."""
|
|
return "cmudict" in _preloaded_resources
|
|
|
|
|
|
def preload_nltk_punkt() -> None:
|
|
"""Preload NLTK's punkt tokenizer (optional, used for sentence tokenization)."""
|
|
global _preloaded_resources
|
|
|
|
if "punkt" in _preloaded_resources:
|
|
logger.info("NLTK punkt already preloaded")
|
|
return
|
|
|
|
logger.info("Preloading NLTK punkt tokenizer...")
|
|
start_time = time.time()
|
|
|
|
try:
|
|
import nltk
|
|
|
|
try:
|
|
from nltk.tokenize import word_tokenize
|
|
|
|
# Test it works
|
|
_ = word_tokenize("test")
|
|
except LookupError:
|
|
logger.info("Downloading NLTK punkt data...")
|
|
nltk.download("punkt", quiet=True)
|
|
nltk.download("punkt_tab", quiet=True)
|
|
|
|
_preloaded_resources["punkt"] = True
|
|
|
|
elapsed = time.time() - start_time
|
|
logger.info(f"NLTK punkt preloaded in {elapsed:.2f}s")
|
|
|
|
except Exception as e:
|
|
logger.warning(f"Failed to preload NLTK punkt (non-critical): {e}")
|
|
|
|
|
|
def preload_all_nltk_resources() -> None:
|
|
"""Preload all NLTK resources used by the application."""
|
|
preload_nltk_cmudict()
|
|
# punkt is optional and less critical
|
|
try:
|
|
preload_nltk_punkt()
|
|
except Exception:
|
|
pass # Non-critical
|
|
|
|
|
|
def clear_preloaded_resources() -> None:
|
|
"""Clear all preloaded resources. Useful for testing."""
|
|
global _preloaded_resources
|
|
_preloaded_resources.clear()
|