Commit 5ba76b5
authored
Optimize levenshtein_distance
The optimized version achieves an **11% speedup** through several key memory and algorithmic optimizations:
**Primary Optimizations:**
1. **Pre-allocated buffer reuse**: Instead of creating a new `newDistances` list on every iteration (16,721 allocations in the profiler), the optimized version uses two pre-allocated lists (`previous` and `current`) that are swapped via reference assignment. This eliminates ~16K list allocations per call.
2. **Eliminated tuple construction in min()**: The original code creates a 3-element tuple for `min((a, b, c))` 8+ million times. The optimized version uses inline comparisons (`a if a < b else b`), avoiding tuple overhead entirely.
3. **Direct indexing over enumerate**: Replaced `enumerate(s1)` and `enumerate(s2)` with `range(len1)` and direct indexing, eliminating tuple unpacking overhead in the inner loops.
4. **Cached string lengths**: Pre-computing `len1` and `len2` avoids repeated `len()` calls.
**Performance Impact by Test Case:**
- **Medium-length strings** (6-10 chars): 20-30% faster - best case for the optimizations
- **Large identical/similar strings** (1000+ chars): 20-25% faster for different strings, but slower for identical strings due to overhead
- **Very short strings** (1-2 chars): Often 10-20% slower due to setup overhead outweighing benefits
- **Empty string cases**: Consistently slower due to initialization costs
**Context Impact:**
The function is used in `closest_matching_file_function_name()` for fuzzy matching function names. Since this involves comparing many short-to-medium function names, the optimization should provide measurable benefits in code discovery workflows where hundreds of function name comparisons occur.
The optimization is most effective for the common case of comparing function names (typically 5-20 characters), where memory allocation savings outweigh setup costs.1 parent 626cec1 commit 5ba76b5
1 file changed
+23
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
278 | 278 | | |
279 | 279 | | |
280 | 280 | | |
| 281 | + | |
281 | 282 | | |
282 | 283 | | |
283 | 284 | | |
| |||
304 | 305 | | |
305 | 306 | | |
306 | 307 | | |
307 | | - | |
308 | | - | |
309 | | - | |
310 | | - | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
311 | 319 | | |
312 | | - | |
| 320 | + | |
313 | 321 | | |
314 | | - | |
315 | | - | |
316 | | - | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
317 | 332 | | |
318 | 333 | | |
319 | 334 | | |
| |||
0 commit comments