⚡️ Speed up function _facet_grid
by 11%
#118
Open
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.
📄 11% (0.11x) speedup for
_facet_grid
inplotly/figure_factory/_facet_grid.py
⏱️ Runtime :
1.40 seconds
→1.25 seconds
(best of7
runs)📝 Explanation and details
Here are optimized versions of the functions based on the profiling data. The functions with the highest impact on runtime are:
make_subplots
,_facet_grid
,_annotation_dict
,_make_trace_for_scatter
and, to a much lesser degree,_return_label
. The key bottleneck is callingmake_subplots
(especially if it is not cached), iterating inefficiently over DataFrames, repeatedly allocating dicts in inner loops, and unnecessary computation.Optimizations below:
unique()
, instead reuse the results).Note: Changes are only made for performance - all signatures, basic logic, and original comments are preserved.
Key details and changes:
.values
, which returns fast NumPy arrays, and Index objects for uniqueness._return_label
uses fast f-strings._annotation_dict
, all constant expressions are at the top and each branch is as small as possible.append_trace
) is unaltered but should be the only high cost left.This approach maximally preserves compatibility and interface and will be noticeably faster especially for larger datasets and higher numbers of plots. If you need even more extreme performance (such as heavy annotation/label generation for massive grids), let me know and further vectorization approaches (possibly outside of Plotly itself) can be considered.
✅ Correctness verification report:
🌀 Generated Regression Tests Details
To edit these changes
git checkout codeflash/optimize-_facet_grid-mb2f798b
and push.