⚡️ Speed up method JournalStorageReplayResult._apply_create_study by 22%
#28
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.
📄 22% (0.22x) speedup for
JournalStorageReplayResult._apply_create_studyinoptuna/storages/journal/_storage.py⏱️ Runtime :
43.5 microseconds→35.6 microseconds(best of12runs)📝 Explanation and details
The optimization replaces an expensive linear search with a more efficient set-based lookup when checking for duplicate study names.
Key optimization:
study_name in [s.study_name for s in self._studies.values()]- this creates a list and performs O(n) linear search every timeset(s.study_name for s in self._studies.values())and checks membership in O(1) timeif self._studies:check to avoid unnecessary work when no studies exist yetWhy this is faster:
self._studiesis non-empty, avoiding overhead for the first study creationTest case performance:
The optimization particularly benefits scenarios with multiple studies:
test_create_study_empty_directions: 26.7% faster (7.47μs → 5.90μs)test_create_study_invalid_direction: 15.2% faster (6.46μs → 5.61μs)The 22% overall speedup comes from avoiding the O(n) list creation and search operation, which becomes increasingly valuable as more studies are added to the storage.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_wou29s7s/tmp8ibnnfgl/test_concolic_coverage.py::test_JournalStorageReplayResult__apply_create_studyTo edit these changes
git checkout codeflash/optimize-JournalStorageReplayResult._apply_create_study-mhaxgbgyand push.