⚡️ Speed up function _filter_missing_values by 13%
#164
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.
📄 13% (0.13x) speedup for
_filter_missing_valuesinoptuna/visualization/matplotlib/_contour.py⏱️ Runtime :
447 microseconds→394 microseconds(best of250runs)📝 Explanation and details
The optimization achieves a 13% speedup by eliminating repeated attribute lookups in the inner loop through local variable binding.
Key optimization: The code caches
x_values.appendandy_values.appendas local variablesappend_xandappend_ybefore the loop. This avoids Python's costly attribute resolution on every iteration - instead of looking up theappendmethod 6,329 times each (as shown in the profiler), the lookup happens only once per list.Performance impact: The line profiler shows the append operations dropped from 20.2% + 20.8% = 41% of total time to 18.6% + 19.9% = 38.4% of total time. This 2.6 percentage point reduction in the most expensive operations drives the overall speedup.
Test case performance: The optimization shows strongest gains on large datasets (18-20% faster on 1000+ element tests) where the loop dominates execution time. Smaller datasets see modest slowdowns (2-17%) due to the overhead of creating local variables, but this is negligible in real-world usage where contour plots typically process hundreds or thousands of data points.
Why this works: Python's attribute lookup involves dictionary searches and method binding. By caching these expensive operations outside the loop, we convert O(n) attribute lookups to O(1), making the inner loop more CPU-efficient while preserving identical behavior and output.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_filter_missing_values-mhoba3yband push.