⚡️ Speed up function indent by 227%
#128
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.
📄 227% (2.27x) speedup for
indentinsrc/bokeh/util/strings.py⏱️ Runtime :
461 microseconds→141 microseconds(best of250runs)📝 Explanation and details
The optimized code achieves a 226% speedup by replacing the inefficient
split("\n")+ generator expression +join()pattern with a directstr.replace()operation.Key optimizations:
Empty string fast path: Added
if not text: return paddingto handle empty strings immediately, avoiding unnecessary string operations (238% faster for empty strings).Direct string replacement: Changed from
"\n".join(padding + line for line in text.split("\n"))topadding + text.replace("\n", f"\n{padding}"). This eliminates:Instead, it performs a single replace operation that inserts padding after each newline, then prepends padding to the entire text.
Why this is faster:
str.replace()is a highly optimized C-level operation in Pythonsplit()Performance characteristics:
Impact on workloads: Based on
function_references, this function is used in Bokeh's JavaScript code generation (wrap_in_onload,wrap_in_safely,wrap_in_script_tag). Since these likely run during web page rendering or visualization generation, the performance improvement will reduce latency in generating embedded Bokeh content, especially for complex visualizations with substantial JavaScript code.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_sstvtaha/tmpcy6l8blp/test_concolic_coverage.py::test_indentTo edit these changes
git checkout codeflash/optimize-indent-mhwbeqigand push.