⚡️ Speed up function _public_version by 29%#13
Open
codeflash-ai[bot] wants to merge 1 commit intoopt-attempt-2from
Open
⚡️ Speed up function _public_version by 29%#13codeflash-ai[bot] wants to merge 1 commit intoopt-attempt-2from
_public_version by 29%#13codeflash-ai[bot] wants to merge 1 commit intoopt-attempt-2from
Conversation
The optimization achieves a **29% speedup** by adding a **fast path for the common case** where only the `local` parameter is modified—which is exactly how `_public_version()` calls `__replace__()`. **Key optimization:** The optimized code checks `if len(kwargs) == 1 and "local" in kwargs` first, which is the dominant use case based on the function references showing `_public_version()` is called from version comparison operators (`_compare_equal`, `_compare_less_than_equal`, `_compare_greater_than_equal`). When this condition is true, the code: 1. **Validates only the `local` parameter** instead of checking all 6 parameters (`epoch`, `release`, `pre`, `post`, `dev`, `local`) 2. **Directly copies the 5 unchanged attributes** (`_epoch`, `_release`, `_pre`, `_post`, `_dev`) without any validation overhead 3. **Avoids 11 dictionary lookups** (the original code does `"key" in kwargs` for each of 6 parameters, plus the subsequent `kwargs["key"]` access for `local`) **Performance impact from profiling:** - The fast path executes for **4,493 out of 4,748 calls** (~95% hit rate) - For these calls, `__replace__` runtime drops from **31.1ms to 17.5ms** (44% faster) - The overhead of the fast-path check (200ns) is negligible compared to the savings (13.6ms) **Why this works:** The function references show `_public_version()` is called in **hot comparison paths** within the specifiers module. Version comparisons are critical operations in dependency resolution, so even microsecond improvements per call compound significantly. The test results confirm this—versions with local segments see 19-47% speedup, with the largest gains when no actual replacement occurs (47% faster when `local=None` is already true). The general path remains for edge cases where multiple parameters change, ensuring correctness while optimizing the 95% case.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📄 29% (0.29x) speedup for
_public_versioninsrc/packaging/specifiers.py⏱️ Runtime :
1.05 milliseconds→811 microseconds(best of6runs)📝 Explanation and details
The optimization achieves a 29% speedup by adding a fast path for the common case where only the
localparameter is modified—which is exactly how_public_version()calls__replace__().Key optimization:
The optimized code checks
if len(kwargs) == 1 and "local" in kwargsfirst, which is the dominant use case based on the function references showing_public_version()is called from version comparison operators (_compare_equal,_compare_less_than_equal,_compare_greater_than_equal). When this condition is true, the code:localparameter instead of checking all 6 parameters (epoch,release,pre,post,dev,local)_epoch,_release,_pre,_post,_dev) without any validation overhead"key" in kwargsfor each of 6 parameters, plus the subsequentkwargs["key"]access forlocal)Performance impact from profiling:
__replace__runtime drops from 31.1ms to 17.5ms (44% faster)Why this works:
The function references show
_public_version()is called in hot comparison paths within the specifiers module. Version comparisons are critical operations in dependency resolution, so even microsecond improvements per call compound significantly. The test results confirm this—versions with local segments see 19-47% speedup, with the largest gains when no actual replacement occurs (47% faster whenlocal=Noneis already true).The general path remains for edge cases where multiple parameters change, ensuring correctness while optimizing the 95% case.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
⏪ Click to see Replay Tests
test_benchmark_py__replay_test_0.py::test_src_packaging_specifiers__public_version🔎 Click to see Concolic Coverage Tests
codeflash_concolic_ui1l843q/tmplb9r5ewz/test_concolic_coverage.py::test__public_versionTo edit these changes
git checkout codeflash/optimize-_public_version-mjjgjbvzand push.