⚡️ Speed up function CONTEXTUAL_TIMEDELTA_FORMATTER by 24%
#133
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.
📄 24% (0.24x) speedup for
CONTEXTUAL_TIMEDELTA_FORMATTERinsrc/bokeh/models/formatters.py⏱️ Runtime :
6.16 milliseconds→4.98 milliseconds(best of16runs)📝 Explanation and details
The optimization achieves a 23% speedup by precomputing the constant keyword argument dictionaries used to construct nested
TimedeltaTickFormatterobjects, avoiding repeated allocation overhead on each function call.Key Optimizations:
Precomputed Static Dictionaries: The original code recreated identical dictionaries and lists every time the function was called. The optimized version moves these constant values into module-level variables (
_base_kwargs,_ctx1_kwargs,_ctx2_kwargs) that are computed once at import time.Reduced Memory Allocations: Instead of constructing nested keyword arguments inline (which creates temporary dictionaries at each nesting level), the optimization uses
.copy()on pre-existing dictionaries and modifies only thecontextfield as needed.Eliminated Redundant Object Creation: The original version created three levels of nested
TimedeltaTickFormatterobjects in a single expression, causing the Python interpreter to allocate memory for intermediate keyword dictionaries multiple times. The optimized version builds these objects step-by-step, reusing pre-allocated dictionaries.Performance Impact:
The line profiler shows that the three
TimedeltaTickFormatterconstructor calls (lines with ~31-35% of total time) are now more efficient because they use pre-allocated dictionaries rather than constructing new ones from scratch. The optimization is particularly effective for this use case because the formatter configuration is completely static - the same values are used every time.Test Results Analysis:
All test cases show consistent 19-28% improvements, indicating the optimization is effective regardless of the specific formatting scenarios being tested. The performance gain is most pronounced in
test_context_nested_formatting(27.6% faster), which makes sense as it directly exercises the nested formatter construction that was optimized.This optimization is especially valuable if
CONTEXTUAL_TIMEDELTA_FORMATTER()is called frequently in data visualization workflows where tick formatters are created repeatedly.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-CONTEXTUAL_TIMEDELTA_FORMATTER-mhwgx7xwand push.