⚡️ Speed up function make_norm_from_scale by 40%
#244
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.
📄 40% (0.40x) speedup for
make_norm_from_scaleinlib/matplotlib/colors.py⏱️ Runtime :
741 microseconds→528 microseconds(best of12runs)📝 Explanation and details
The optimized code achieves a 40% speedup through several key performance improvements targeting function call overhead and attribute lookups:
What specific optimizations were applied:
Type check optimization: Replaced
isinstance(scale_cls, functools.partial)with direct type comparisontype(scale_cls) is functools.partial, eliminating the slower isinstance() call overhead.Pre-allocated variables: Moved
scale_args = ()andscale_kwargs_items = ()outside the conditional block to avoid repeated assignments in the common case.Static signature creation: For the default case (
init is None), replaced dynamic function definition with a pre-constructedinspect.Signatureobject, eliminating function creation overhead on each call.Reduced attribute lookups: In the
__call__andinversemethods, cachedself.vmin/self.vmaxin local variables (vmin/vmax) to avoid repeated attribute access.Dict construction optimization: In
__init__, moveddict(scale_kwargs_items)construction outside the functools.partial call to avoid creating the dictionary on every instance creation.Why these optimizations lead to speedup:
vminvsself.vmin)How this impacts workloads:
Based on the function_references, this function is called from
_auto_norm_from_scalein matplotlib's colormap module, which generates norm classes for color scaling. The 40% improvement will benefit any matplotlib plotting that uses automatic norm generation, particularly in tight loops or when creating many plots with different color scales.Test case performance:
The annotated tests show consistent 35-47% improvements across all scenarios, with the optimization being particularly effective for both simple linear norms and more complex log scale transforms. Large array processing also benefits significantly, making this optimization valuable for data visualization workloads.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
test_colors.py::test_make_norm_from_scale_nametest_pickle.py::test_dynamic_norm🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-make_norm_from_scale-mja3y9hdand push.