⚡️ Speed up function interp_palette by 46%
#74
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
interp_paletteinsrc/bokeh/palettes.py⏱️ Runtime :
9.92 milliseconds→6.81 milliseconds(best of109runs)📝 Explanation and details
The optimized code achieves a 45% speedup by eliminating object allocation overhead in the color interpolation pipeline. The key optimization replaces the expensive
RGB(*args).to_hex()pattern with direct hex string construction.Primary optimization - Object allocation elimination:
RGBobjects just to call.to_hex()(81.2% of runtime in profiler)rgba_to_hex()function that directly converts RGBA values to hex strings, bypassing object creation entirelySecondary optimization - Array access patterns:
to_rgba_arrayimprovements: Replacedenumerate()withrange(len())and direct array indexing (rgba_array[i, 0] = rgba.rvs tuple assignment)Performance characteristics by test case:
test_large_n_with_small_palette: 1.13ms → 693μs)The optimization maintains identical output and error handling while dramatically reducing memory pressure from temporary object creation - a classic Python performance pattern where avoiding object allocation provides substantial speedups.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
unit/bokeh/test_palettes.py::test_interp_palette🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_5f34sbte/tmpjqz5isp5/test_concolic_coverage.py::test_interp_palette_2To edit these changes
git checkout codeflash/optimize-interp_palette-mhbgy4edand push.