⚡️ Speed up function cartesian_to_axial by 23%
#81
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.
📄 23% (0.23x) speedup for
cartesian_to_axialinsrc/bokeh/util/hex.py⏱️ Runtime :
1.68 milliseconds→1.37 milliseconds(best of116runs)📝 Explanation and details
The optimized code achieves a 22% speedup through several key performance improvements:
1. Eliminated Redundant Math Calculations
math.sqrt(3.0)once and reuses it in bothHEX_FLATandHEX_POINTYarrays, avoiding duplicate square root calculations on every function call2. Avoided Unnecessary Arithmetic Operations
aspect_scale=1cases to skip multiplication/division when the scale factor has no effectaspect_scale=1, uses simplerx/sizeinstead ofx/size * 1ory/size / 13. Improved NumPy Rounding Performance
np.round()withnp.rint(), which is often faster for large arrays4. Optimized Memory Usage
np.asarray(..., dtype=int)instead of.astype(int), which avoids unnecessary copying when the array is already the correct dtypeTest Results Analysis:
The optimizations show consistent performance gains across all test scenarios:
The performance gains are most pronounced for single-point calculations and cases with default aspect scaling, making this optimization particularly valuable for common usage patterns.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
unit/bokeh/util/test_hex.py::Test_cartesian_to_axial.test_default_aspect_flattopunit/bokeh/util/test_hex.py::Test_cartesian_to_axial.test_default_aspect_pointytop🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-cartesian_to_axial-mhbk4cgnand push.