Commit b5c44aa
authored
Optimize check_api_key
The optimized code achieves a **101% speedup** by eliminating two expensive operations that were being repeated on every function call:
**Key optimizations:**
1. **Import hoisting**: Moved `from codeflash.optimization.optimizer import Optimizer` from inside the function to module-level. The profiler shows this import was taking **70.5% of total execution time** (623ms out of 884ms) in the original code. By importing once at module load instead of on every call, this overhead is eliminated.
2. **Single optimizer initialization**: Added a `_optimizer_initialized` flag to prevent redundant optimizer creation. The original code called `process_args()` and created a new `Optimizer` instance on every successful API key validation, even when `server.optimizer` was already set. The optimized version only initializes once per process.
**Performance impact by test type:**
- **Single calls**: 80-90% faster for individual API key validations
- **Large scale tests**: 101% faster for repeated calls (1000 iterations), where the optimization compounds significantly
- **Mixed scenarios**: 81-97% faster across different success/error patterns
The optimization is particularly effective for LSP servers or long-running processes where `check_api_key` is called repeatedly, as the expensive import and initialization overhead is amortized across all calls rather than repeated each time.1 parent c4efb1b commit b5c44aa
1 file changed
+6
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| 34 | + | |
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
| |||
264 | 265 | | |
265 | 266 | | |
266 | 267 | | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
271 | 273 | | |
272 | 274 | | |
273 | 275 | | |
| |||
0 commit comments