Commit 9bc7528
authored
Optimize _prompt_custom_directory
The optimized code achieves a **364% speedup** (101ms → 21.7ms) through two targeted optimizations:
## 1. Theme Instance Caching (Primary Optimization)
The original code recreated a `CodeflashTheme()` instance on every call to `_get_theme()`. Line profiler shows this function was called 360 times, spending **88.2% of execution time** (133.4ms) just instantiating theme objects.
The optimization introduces a module-level cache (`_cached_theme`) that stores the theme after first creation. This reduces `_get_theme()` total time from **151.2ms to 17.4ms** (88.5% reduction), as subsequent calls simply return the cached instance instead of re-instantiating.
**Test Impact**: Basic path validation tests show **900-1000% speedup** (265μs → 25μs), stress tests with 50-100 invalid retries show **281-300% speedup** (16ms → 4.2ms), and deeply nested paths improve by **300-500%**. The caching is most effective in scenarios with multiple validation attempts, which is common when users enter incorrect paths.
## 2. Faster Absolute Path Check
The original code used `Path(path).is_absolute()` which instantiates a `Path` object just to check if a path is absolute. Line profiler shows this consumed **55.4%** of `validate_relative_directory_path()` time (4.28ms).
The optimization replaces this with `os.path.isabs(path)`, a lightweight string-based check that avoids object instantiation. This reduces the absolute path check from **4.28ms to 0.68ms** (84% reduction).
## Why This Works
- **Lazy initialization + caching** is a classic pattern for expensive object creation that's needed repeatedly
- `os.path.isabs()` is functionally equivalent to `Path().is_absolute()` for validation purposes but avoids the overhead of pathlib's object model
- The `CodeflashTheme` initialization likely involves terminal capability detection and color setup, making it expensive to repeat
## Context Considerations
While function references aren't available, the fact that `_get_theme()` was called 360 times in the test suite suggests this function is in a validation loop where users may enter multiple invalid paths before succeeding. The optimization particularly benefits workflows with:
- Multiple path validation attempts (common in CLI prompts)
- Batch operations validating many paths
- Integration tests that exercise the prompt repeatedly
The test results confirm this: single-path tests show 9-10x speedup, while tests with 50-100 retries show 3-4x speedup, demonstrating scalability benefits for both quick validations and extended retry scenarios.1 parent c299d99 commit 9bc7528
2 files changed
Lines changed: 9 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
| 30 | + | |
29 | 31 | | |
30 | 32 | | |
31 | 33 | | |
| |||
57 | 59 | | |
58 | 60 | | |
59 | 61 | | |
60 | | - | |
61 | | - | |
62 | | - | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
63 | 67 | | |
64 | 68 | | |
65 | 69 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
472 | 472 | | |
473 | 473 | | |
474 | 474 | | |
475 | | - | |
| 475 | + | |
| 476 | + | |
476 | 477 | | |
477 | 478 | | |
478 | 479 | | |
| |||
0 commit comments