⚡️ Speed up function _completed_rung_key by 33%
#159
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.
📄 33% (0.33x) speedup for
_completed_rung_keyinoptuna/pruners/_successive_halving.py⏱️ Runtime :
723 microseconds→545 microseconds(best of233runs)📝 Explanation and details
The optimization replaces the
.format()method with an f-string for string formatting, achieving a 32% speedup (723μs → 545μs).What changed:
"completed_rung_{}".format(rung)→f"completed_rung_{rung}"Why it's faster:
F-strings are significantly more performant than
.format()because they're evaluated at compile-time rather than runtime. The.format()method involves:F-strings bypass these steps by generating optimized bytecode that directly interpolates values, reducing the per-call overhead from 352.9ns to 248.6ns (30% improvement per call).
Performance characteristics:
The optimization shows consistent gains across all test scenarios:
Impact on workloads:
This function appears to be called frequently (3,441 hits in profiling), suggesting it's in a performance-critical path. Given that it's part of Optuna's successive halving pruner, it's likely called during hyperparameter optimization loops where every microsecond matters. The 32% improvement will compound significantly in optimization workflows that make thousands of pruning decisions.
The optimization is particularly effective for workloads involving many pruning evaluations, as demonstrated by the large-scale test cases showing consistent 30%+ improvements.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_bg1jh046/tmpsmlq4p6m/test_concolic_coverage.py::test__completed_rung_keyTo edit these changes
git checkout codeflash/optimize-_completed_rung_key-mho9fiu5and push.