⚡️ Speed up function magma by 13%
#75
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.
📄 13% (0.13x) speedup for
magmainsrc/bokeh/palettes.py⏱️ Runtime :
620 microseconds→549 microseconds(best of199runs)📝 Explanation and details
The optimization targets the expensive operation in the
linear_palettefunction that was consuming 98.8% of the execution time. The key changes are:What was optimized:
math.floor()on each float index with a two-step processnp.linspace(), and 2) batch-converting them to integers using NumPy'sastype(int)math.floor()calls by leveraging NumPy's vectorized operationsWhy it's faster:
math.floor()is expensive when called repeatedly in Python (each call has function call overhead)astype(int)performs the same floor operation but vectorized across the entire array, which is much more efficientPerformance characteristics:
The optimization shows significant gains for larger palette sizes - test cases with
n=100,n=255, andn=256see 15-35% speedups, while smaller cases (n=2,n=3) see modest 3-8% improvements. This is because the vectorization benefits scale with the number of elements being processed. The 13% overall speedup reflects the mixed workload of different palette sizes in typical usage.✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
unit/bokeh/test_palettes.py::test_cmap_generator_function🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_5f34sbte/tmpiq527na1/test_concolic_coverage.py::test_magmaTo edit these changes
git checkout codeflash/optimize-magma-mhbh4wu3and push.