⚡️ Speed up function _apply_indentation by 140% in PR #1199 (omni-java)#1306
Closed
codeflash-ai[bot] wants to merge 1 commit intoomni-javafrom
Closed
⚡️ Speed up function _apply_indentation by 140% in PR #1199 (omni-java)#1306codeflash-ai[bot] wants to merge 1 commit intoomni-javafrom
_apply_indentation by 140% in PR #1199 (omni-java)#1306codeflash-ai[bot] wants to merge 1 commit intoomni-javafrom
Conversation
The optimized code achieves a **140% speedup** (from 1.86ms to 776μs) by eliminating expensive regex operations and reducing redundant function calls. ## Key Optimizations **1. Replaced Regex with Direct String Operations** The original `_get_indentation()` used `re.match(r"^(\s*)", line)` for every line, which is costly. The optimized version calculates indentation length using `len(line) - len(line.lstrip())` and returns `line[:indent_len]`. This eliminates regex overhead entirely - as shown in the line profiler, the original `_get_indentation()` consumed 83.5% of its time in regex matching (4.09ms out of 4.89ms). **2. Eliminated Redundant Function Calls** The original code called `_get_indentation()` for 1,744 lines (61.5% of total time in `_apply_indentation()`), while the optimized version inlines the calculation. This reduces function call overhead and allows the calculation to reuse `stripped_line` already computed via `line.lstrip()`. **3. Optimized Indentation Prefix Checking** Instead of calling `line_indent.startswith(existing_indent)`, the optimized code uses length comparison (`line_indent_len >= existing_indent_len`) followed by direct string slicing (`line[:existing_indent_len] == existing_indent`). This avoids creating intermediate strings and leverages the fact that we already know the indentation lengths. **4. Cached Indentation Length** The optimized version stores `existing_indent_len` alongside `existing_indent`, eliminating repeated length calculations during the prefix check for each line. ## Performance Impact The test results show consistent improvements across all scenarios: - **Small inputs** (single/few lines): 100-133% faster - **Large inputs** (500 lines): 161-200% faster - **Deep nesting** (100 levels): 121% faster - **Long content lines**: 84.7% faster The optimization is particularly effective for: - Code with many indented lines (typical Java code structure) - Deeply nested blocks where indentation calculations are repeated frequently - Large files where the regex overhead compounds Since this function likely processes Java code formatting in a hot path (based on its location in `replacement.py`), the 140% speedup significantly reduces latency for code transformation operations.
Merged
Collaborator
|
Closing stale bot PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
⚡️ This pull request contains optimizations for PR #1199
If you approve this dependent PR, these changes will be merged into the original PR branch
omni-java.📄 140% (1.40x) speedup for
_apply_indentationincodeflash/languages/java/replacement.py⏱️ Runtime :
1.86 milliseconds→776 microseconds(best of177runs)📝 Explanation and details
The optimized code achieves a 140% speedup (from 1.86ms to 776μs) by eliminating expensive regex operations and reducing redundant function calls.
Key Optimizations
1. Replaced Regex with Direct String Operations
The original
_get_indentation()usedre.match(r"^(\s*)", line)for every line, which is costly. The optimized version calculates indentation length usinglen(line) - len(line.lstrip())and returnsline[:indent_len]. This eliminates regex overhead entirely - as shown in the line profiler, the original_get_indentation()consumed 83.5% of its time in regex matching (4.09ms out of 4.89ms).2. Eliminated Redundant Function Calls
The original code called
_get_indentation()for 1,744 lines (61.5% of total time in_apply_indentation()), while the optimized version inlines the calculation. This reduces function call overhead and allows the calculation to reusestripped_linealready computed vialine.lstrip().3. Optimized Indentation Prefix Checking
Instead of calling
line_indent.startswith(existing_indent), the optimized code uses length comparison (line_indent_len >= existing_indent_len) followed by direct string slicing (line[:existing_indent_len] == existing_indent). This avoids creating intermediate strings and leverages the fact that we already know the indentation lengths.4. Cached Indentation Length
The optimized version stores
existing_indent_lenalongsideexisting_indent, eliminating repeated length calculations during the prefix check for each line.Performance Impact
The test results show consistent improvements across all scenarios:
The optimization is particularly effective for:
Since this function likely processes Java code formatting in a hot path (based on its location in
replacement.py), the 140% speedup significantly reduces latency for code transformation operations.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1199-2026-02-03T11.26.46and push.