⚡️ Speed up function annotation_params_for_rect by 46%
#85
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.
📄 46% (0.46x) speedup for
annotation_params_for_rectinplotly/shapeannotation.py⏱️ Runtime :
2.15 milliseconds→1.47 milliseconds(best of154runs)📝 Explanation and details
The optimized code achieves a 46% speedup through several key optimizations:
1. Precomputed frozenset constants for position matching
set(["inside", "top", "left"])constructions with precomputedfrozensetconstants like_INSIDE_TOP_LEFT2. Optimized
_meanfunction for the common two-element casereturn (x[0] + x[1]) / 2 if n == 2 else float(sum(x)) / nsum()andfloat()conversion overhead for the most common case3. Inlined position preprocessing logic
_add_inside_to_position()function call with direct inline logic in_prepare_position4. Streamlined min/max operations
min([x0, x1])tomin(x0, x1)andmax([y0, y1])tomax(y0, y1)Performance characteristics:
_meanThese optimizations are particularly effective for plotting libraries where annotation positioning is called frequently with predictable position patterns.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-annotation_params_for_rect-mhge3a6fand push.