⚡️ Speed up function _handle_sublists by 46%
#59
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
_handle_sublistsinsrc/bokeh/plotting/graph.py⏱️ Runtime :
987 microseconds→674 microseconds(best of261runs)📝 Explanation and details
The optimization achieves a 46% speedup by eliminating redundant iterations over the
valueslist and reducing the overhead of Python's built-inany()andall()functions.Key changes:
any(isinstance(x, (list, tuple)) for x in values)with an explicit loop that breaks early when finding the first non-scalar elementall(isinstance(...))) now runs only when non-scalars were detected, avoiding unnecessary work for all-scalar casesWhy this is faster:
test_all_scalars_ints,test_all_none) show 45-124% speedups because they avoid the expensiveany()call and skip the validation loop entirelytest_all_lists,test_all_tuples) show 47-67% speedups by replacing two separate traversals with a more efficient early-exit detection followed by targeted validationtest_large_all_none_with_listshows 111% speedup because the optimization scales linearly with input sizeThe optimization is particularly effective for common use cases where inputs are either all-scalar or consistently non-scalar, as it can short-circuit expensive validation work.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_handle_sublists-mhaznyuqand push.