Commit d684c41
authored
⚡️ Speed up function
Here is an optimized version of your program. The main optimizations are.
- Avoid repeated formatting of `python_version_string` by computing it once at module load time; the Python version doesn't change during runtime.
- Move the string templates out of the function, so they are created just once.
- Remove unnecessary usage of triple-quoted strings for templated outputs since there is no variable interpolation except one case.
- Inline the conditional return for a slightly reduced call stack.
- Use identity comparison `is` for Enum comparison (assuming `DependencyManager` is an `Enum`), which can be marginally faster.
**Optimized Code:**
**What changed and why:**
- Pre-calculating the version string (and Python setup string) at module load time removes a significant amount of redundant per-call formatting (was hot in profiling!).
- This also means `sys.version_info` is only accessed once.
- Enum comparison is done with `is` which is the idiomatic and fastest way for Enums.
- Templates are immediately ready, so nothing is constructed inside the function anymore; this yields maximum speedup for such a hot function; now it's a simple if/return.
This completely eliminates *all* expensive operations from the hot path of `get_dependency_manager_installation_string`.get_dependency_manager_installation_string by 139% in PR #208 (bump-gha-uv-version)1 parent df93124 commit d684c41
1 file changed
+21
-16
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
239 | 239 | | |
240 | 240 | | |
241 | 241 | | |
242 | | - | |
| 242 | + | |
243 | 243 | | |
244 | 244 | | |
245 | 245 | | |
| |||
302 | 302 | | |
303 | 303 | | |
304 | 304 | | |
305 | | - | |
| 305 | + | |
306 | 306 | | |
307 | 307 | | |
308 | 308 | | |
| |||
354 | 354 | | |
355 | 355 | | |
356 | 356 | | |
357 | | - | |
| 357 | + | |
358 | 358 | | |
359 | | - | |
| 359 | + | |
360 | 360 | | |
361 | 361 | | |
362 | 362 | | |
| |||
466 | 466 | | |
467 | 467 | | |
468 | 468 | | |
469 | | - | |
| 469 | + | |
470 | 470 | | |
471 | 471 | | |
472 | 472 | | |
| |||
619 | 619 | | |
620 | 620 | | |
621 | 621 | | |
622 | | - | |
623 | | - | |
624 | | - | |
625 | | - | |
626 | | - | |
627 | | - | |
628 | | - | |
629 | | - | |
630 | | - | |
631 | | - | |
632 | | - | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
633 | 625 | | |
634 | 626 | | |
635 | 627 | | |
| |||
966 | 958 | | |
967 | 959 | | |
968 | 960 | | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
0 commit comments