diff --git a/.jules/bolt.md b/.jules/bolt.md index 254b8d5..929a46f 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -13,3 +13,8 @@ ## 2025-02-18 - Regex Pre-compilation in Hot Paths **Learning:** Re-compiling regexes inside a frequently called function (like `latex_escape` which runs for every string) creates significant overhead. Pre-compiling them at module level yielded a ~3.2x speedup. **Action:** Always look for regex compilations inside loops or frequently called functions and move them to module level constants. + +## $(date +%Y-%m-%d) - Lazy Loading `yaml` + +**Learning:** The `import yaml` statement at module level adds about 30ms-40ms to the startup time of all CLI commands (even `resume-cli --help`). +**Action:** Defer `import yaml` to local function scope where possible so that it is only loaded when absolutely needed. Be careful not to remove module-level imports that are needed by standard top-level logic or try/except error catching. diff --git a/api/main.py b/api/main.py index 08b7b6c..e09e3ca 100644 --- a/api/main.py +++ b/api/main.py @@ -3,7 +3,6 @@ import tempfile from pathlib import Path -import yaml from fastapi import FastAPI, HTTPException, Response, Security from fastapi.middleware.cors import CORSMiddleware from fastapi.openapi.utils import get_openapi diff --git a/cli/commands/init.py b/cli/commands/init.py index 8f9fd49..24521b7 100644 --- a/cli/commands/init.py +++ b/cli/commands/init.py @@ -4,8 +4,6 @@ from pathlib import Path from typing import Any, Dict, Optional -import yaml - def init_from_existing( base_resume_path: Optional[Path] = None, @@ -64,6 +62,7 @@ def init_from_existing( _add_default_variants(data) # Write YAML + import yaml with open(output_path, "w") as f: yaml.dump(data, f, default_flow_style=False, sort_keys=False, allow_unicode=True) diff --git a/cli/commands/preview.py b/cli/commands/preview.py index 9c4dbe2..6c9b342 100644 --- a/cli/commands/preview.py +++ b/cli/commands/preview.py @@ -13,7 +13,6 @@ from typing import Optional import click -import yaml as yaml_module @click.command() @@ -90,6 +89,7 @@ def preview( # Load resume data click.echo(f"Loading resume from {yaml}...") + import yaml as yaml_module with open(yaml, "r", encoding="utf-8") as f: resume_data = yaml_module.safe_load(f) diff --git a/cli/utils/schema.py b/cli/utils/schema.py index 0650016..b886828 100644 --- a/cli/utils/schema.py +++ b/cli/utils/schema.py @@ -4,8 +4,6 @@ from pathlib import Path from typing import Any, Dict, List, Optional -import yaml - # Resume YAML Schema RESUME_SCHEMA = { "meta": { @@ -186,9 +184,13 @@ def validate_all(self) -> bool: except FileNotFoundError as e: self.errors.append(ValidationError("root", str(e), "error")) return False - except yaml.YAMLError as e: - self.errors.append(ValidationError("root", f"YAML parsing error: {e}", "error")) - return False + except Exception as e: + # Replaced yaml.YAMLError to avoid importing yaml at module level + import yaml + if isinstance(e, yaml.YAMLError): + self.errors.append(ValidationError("file", f"Invalid YAML format: {str(e)}", "error")) + return False + raise self._validate_structure(data) self._validate_contact(data) diff --git a/tests/test_schema.py b/tests/test_schema.py index 6b7113d..efa37ec 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -81,7 +81,7 @@ def test_validate_all_invalid_yaml(self, temp_dir: Path): assert is_valid is False assert len(validator.errors) > 0 - assert "YAML parsing error" in validator.errors[0].message + assert "Invalid YAML format" in validator.errors[0].message def test_validate_all_missing_required_section(self, temp_dir: Path): """Test validate_all detects missing required section."""