⚡️ Speed up function _get_contour_plot by 26%
#161
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.
📄 26% (0.26x) speedup for
_get_contour_plotinoptuna/visualization/matplotlib/_contour.py⏱️ Runtime :
9.39 seconds→7.48 seconds(best of5runs)📝 Explanation and details
The optimized code achieves a 25% speedup by introducing intelligent caching to eliminate redundant computations in multi-parameter contour plots.
Key Optimization: Object ID-Based Caching
cached_axis_dataandcached_grid_datadictionaries that cache results of expensive functions_calculate_axis_data()and_calculate_griddata()using object IDs as keys_AxisInfoand_SubContourInfoobjects are reused across multiple subplots, leading to repeated expensive computationsPerformance Impact Analysis:
From the line profiler, the original code spent 79.7% of time in
_generate_contour_subplot()calls within the nested loops. The optimized version reduces this to 74.7%, with the time savings coming from:_calculate_axis_data()calls (lines with_filter_missing_valuesand_calculate_axis_data) are now cached when the same axis appears in multiple subplots_calculate_griddata()operation (which was 86.5% of subplot time) is cached for identical_SubContourInfoobjectsTest Case Performance:
test_large_number_of_parameters(6×6 grid) shows 379% speedup (1.94s → 404ms)test_three_parameters_contour_plotshows 248% speedup (481ms → 138ms)The optimization is particularly effective for visualization workflows where users explore relationships between many parameters simultaneously, which is common in hyperparameter optimization scenarios where this contour plotting function would be used.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_get_contour_plot-mhoaqehyand push.