⚡️ Speed up function _split_feature_trait by 19%
#66
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.
📄 19% (0.19x) speedup for
_split_feature_traitinsrc/bokeh/plotting/_renderer.py⏱️ Runtime :
1.55 milliseconds→1.31 milliseconds(best of168runs)📝 Explanation and details
The optimized code replaces the
split()method with direct string indexing usingfind(). Instead of splitting the string into a list and then checking its length, the optimization:Uses
str.find("_")instead ofstr.split("_", 1)- This returns the index of the first underscore or -1 if not found, avoiding the overhead of creating a list object.Direct string slicing - When an underscore is found, it uses
ft[:idx]andft[idx+1:]to extract the parts directly, eliminating the intermediate list creation and tuple conversion.The key performance benefit comes from avoiding memory allocation for the list object that
split()creates. Thefind()method is a simple C-level string search operation that's faster than the more complexsplit()which must allocate memory and populate a list.Test case performance patterns:
find()returns -1find()can terminate early whilesplit()would still need to process the entire stringThe 18% overall speedup suggests the codebase has more cases without underscores or the performance gain from avoiding list allocation outweighs the slicing overhead in typical usage patterns.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_cthbg6_3/tmpi0l_qklb/test_concolic_coverage.py::test__split_feature_traitcodeflash_concolic_cthbg6_3/tmpi0l_qklb/test_concolic_coverage.py::test__split_feature_trait_2To edit these changes
git checkout codeflash/optimize-_split_feature_trait-mhb63bm2and push.