⚡️ Speed up method _CachedStorage.get_trial by 8%
#150
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.
📄 8% (0.08x) speedup for
_CachedStorage.get_trialinoptuna/storages/_cached_storage.py⏱️ Runtime :
2.41 milliseconds→2.23 milliseconds(best of153runs)📝 Explanation and details
The optimized code achieves an 8% speedup through two key optimizations that reduce overhead in cache operations:
1. Eliminated double dictionary lookup in
_get_cached_trial:if trial_id not in self._trial_id_to_study_id_and_number:followed byself._trial_id_to_study_id_and_number[trial_id]performs two hash table lookupsdict.get(trial_id)lookup stores the result, eliminating redundant hash computation and lookup2. Reduced lock contention in
get_trial:3. Minor efficiency improvement in RDBStorage:
_build_frozen_trial_from_trial_modelThese optimizations are particularly effective for workloads with:
The optimizations maintain identical behavior while reducing CPU overhead from hash table operations and thread synchronization, explaining the consistent 8% overall speedup.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_CachedStorage.get_trial-mhl31e3fand push.