Commit ea737f8
authored
⚡️ Speed up function
The optimized code achieves a **16% speedup** through two key optimizations in the `get_patches_metadata()` function:
**1. Caching expensive directory lookups**: The original code called `get_patches_dir_for_project()` on every invocation, which dominated 88.6% of execution time (26.9ms out of 30.3ms total). The optimization introduces `_cached_get_patches_dir_for_project()` with `@lru_cache(maxsize=1)`, eliminating repeated expensive Git operations. This reduces the directory lookup time from 26.9ms to 25.1ms while enabling reuse across multiple calls.
**2. More efficient JSON parsing**: Replaced `json.loads(meta_file.read_text())` with `json.load(f)` using a direct file handle. This avoids loading the entire file content into memory as a string before parsing, reducing JSON processing time from 2.3ms to 1.3ms (43% improvement).
The line profiler shows the optimization is most effective when `get_patches_metadata()` is called multiple times, as the cached directory lookup provides cumulative benefits. Test results demonstrate consistent 14-19% speedups across various scenarios, with particularly strong gains for large metadata files and repeated invocations. The caching is especially valuable in LSP server contexts where the same patches directory is accessed frequently during a session.retrieve_successful_optimizations by 16% in PR #690 (worktree/persist-optimization-patches)1 parent b6f6661 commit ea737f8
1 file changed
+9
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
275 | 275 | | |
276 | 276 | | |
277 | 277 | | |
278 | | - | |
| 278 | + | |
279 | 279 | | |
280 | 280 | | |
281 | | - | |
| 281 | + | |
| 282 | + | |
282 | 283 | | |
283 | 284 | | |
284 | 285 | | |
| |||
341 | 342 | | |
342 | 343 | | |
343 | 344 | | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
0 commit comments