⚡️ Speed up method SuccessiveHalvingPruner.prune by 10%
#156
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.
📄 10% (0.10x) speedup for
SuccessiveHalvingPruner.pruneinoptuna/pruners/_successive_halving.py⏱️ Runtime :
20.3 microseconds→18.4 microseconds(best of70runs)📝 Explanation and details
The optimized code achieves a 10% speedup through three key algorithmic and micro-optimizations:
1. Rung Counting Optimization in
_get_current_rungThe original implementation used a while loop that repeatedly called
_completed_rung_key(rung)and performed dictionary lookups, taking O(k) time where k is the number of rungs. The optimized version counts all matching keys in a single pass throughtrial.system_attrs, eliminating repeated string creation and dictionary lookups. This shows a dramatic improvement from 36.2μs to 7.0μs in the line profiler (81% faster for this function).2. Heap-based Selection in
_is_trial_promotable_to_next_rungInstead of sorting the entire
competing_valueslist (O(n log n)), the optimization usesheapq.nsmallestorheapq.nlargestto find only the k-th element needed for comparison (O(n + k log k)). For typical pruning scenarios where k << n, this provides significant savings. The function time increased slightly in the profiler due to heapq overhead, but this is outweighed by avoiding full sorts on larger datasets.3. String Formatting Micro-optimization in
_completed_rung_keyReplaced
.format()with f-string formatting, reducing function time from 14.9μs to 0.55μs (96% faster). While small individually, this function is called frequently during rung checking.Performance Impact Analysis:
Based on the annotated tests, the optimizations are particularly effective for scenarios with:
The optimizations target the core pruning loop where trials compete for promotion between rungs - a critical hot path in hyperparameter optimization workflows. The heap-based selection especially benefits studies with many concurrent trials, which is common in distributed optimization scenarios.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-SuccessiveHalvingPruner.prune-mho94056and push.