⚡️ Speed up function _sorting_key by 22%#275
Open
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Open
Conversation
The optimized code achieves a **22% speedup** by eliminating redundant regex pattern compilation. **Key Optimization:** The original code calls `re.findall(r"(\d+)", filename)` on every invocation, which internally compiles the regex pattern `r"(\d+)"` each time. The optimized version pre-compiles this pattern once at module load time as `_DIGIT_PATTERN = re.compile(r"(\d+)")` and reuses it via `_DIGIT_PATTERN.findall(filename)`. **Why This Matters:** - Regex compilation is expensive - it involves parsing the pattern, building a state machine, and allocating internal structures - Line profiler data shows the regex line consumed **78.8%** of total runtime (13.6ms of 17.3ms) in the original code - After optimization, this dropped to **58.6%** (5.1ms of 8.7ms), cutting regex overhead by ~62% - The per-hit cost decreased from 3751ns to 1402ns - a **2.7x improvement** on the hottest line **Impact Based on Test Results:** - **Uniform benefit across all cases**: Every test shows 30-150% speedup, indicating the optimization helps regardless of input characteristics - **Best for simple inputs**: Empty strings and no-digit cases see 80-150% speedup since regex overhead dominated their total time - **Still significant for complex inputs**: Multi-number filenames (40-60% faster) and very long filenames (8-9% faster) also benefit - **Scales well**: The 500-file sorting tests confirm benefits compound when the function is called repeatedly in loops This optimization is particularly valuable since `_sorting_key` is typically used as a key function in `sorted()` operations, meaning it gets called once per item being sorted - potentially thousands of times in production workloads dealing with duplicate file management.
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.
📄 22% (0.22x) speedup for
_sorting_keyinunstructured/metrics/utils.py⏱️ Runtime :
605 microseconds→496 microseconds(best of165runs)📝 Explanation and details
The optimized code achieves a 22% speedup by eliminating redundant regex pattern compilation.
Key Optimization:
The original code calls
re.findall(r"(\d+)", filename)on every invocation, which internally compiles the regex patternr"(\d+)"each time. The optimized version pre-compiles this pattern once at module load time as_DIGIT_PATTERN = re.compile(r"(\d+)")and reuses it via_DIGIT_PATTERN.findall(filename).Why This Matters:
Impact Based on Test Results:
This optimization is particularly valuable since
_sorting_keyis typically used as a key function insorted()operations, meaning it gets called once per item being sorted - potentially thousands of times in production workloads dealing with duplicate file management.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-_sorting_key-mks4muoeand push.