⚡️ Speed up function _facet_grid_color_numerical
by 14%
#117
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.
📄 14% (0.14x) speedup for
_facet_grid_color_numerical
inplotly/figure_factory/_facet_grid.py
⏱️ Runtime :
1.29 seconds
→1.13 seconds
(best of8
runs)📝 Explanation and details
Here’s an optimized version of your provided code, focused on.
Details.
df[color_name]
repeatedly inside loops, cache instead.get
for dict-style label lookup to avoid possible KeyErrors.make_subplots
isn’t optimized (it forwards to plotly), kept as-is for compatibility.df.groupby(fields, sort=False)
to avoid unnecessary sorting, and cachedf[color_name].values
..get()
for dict label lookup to safely fallback.marker_dict
), reusing objects._annotation_dict
.Below is the rewritten, faster version.
Summary of key changes.
_annotation_dict
is flattened for speed.If your DataFrames are large, these changes will effectively reduce memory allocation and improve runtime especially for the hot-path (_facet_grid_color_numerical).
Let me know if you would like further micro-optimization or vectorization based on the nature of your DataFrames or inputs!
✅ Correctness verification report:
🌀 Generated Regression Tests Details
To edit these changes
git checkout codeflash/optimize-_facet_grid_color_numerical-mb2f1y3j
and push.