⚡️ Speed up function _is_inside_unrequested_special_dir by 532%#124
Open
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Open
⚡️ Speed up function _is_inside_unrequested_special_dir by 532%#124codeflash-ai[bot] wants to merge 1 commit intomainfrom
_is_inside_unrequested_special_dir by 532%#124codeflash-ai[bot] wants to merge 1 commit intomainfrom
Conversation
The optimized code achieves a **532% speedup** (4.11ms → 650μs) by eliminating the heavyweight `PurePath` object creation overhead and replacing it with lightweight string operations.
## Key Performance Optimizations
**1. Eliminated PurePath Object Construction**
The original code creates two `PurePath` objects per function call, which involves significant parsing and validation overhead. The optimized version uses simple string operations (`rfind`, `split`) that operate directly on the input strings without any object instantiation.
**2. Replaced List Comprehensions with Manual Counting**
Instead of building intermediate lists with list comprehensions (`[part for part in ... if part.startswith("__")]`), the optimized code counts special directories in-place. This avoids memory allocation for temporary lists and the associated iteration overhead.
**3. Direct String Manipulation**
- Uses `rfind('/')` to locate the last separator (O(n) scan but no parsing)
- Uses string slicing to extract parent paths
- Uses `split('/')` only when needed, then iterates once to count
## Why This Works
The original implementation's bottleneck (as shown in line profiler) is spending 98.8% of time in just two lines creating PurePath objects. The optimized version:
- Avoids `PurePath` constructor overhead entirely
- Performs minimal string operations
- Counts directly without intermediate data structures
## Impact on Workloads
Based on `function_references`, this function is called from `resolve_pattern()` in a **hot path** - it filters every matched file path in glob results. The speedup directly benefits:
- Large codebases with many files to scan
- Patterns matching hundreds/thousands of files
- Directory structures with deep nesting (as shown in tests with 50-100 nested directories)
## Test Case Performance
The optimization excels across all test scenarios:
- **Simple paths**: 500-700% faster (e.g., `__pycache__/b.txt` cases)
- **Deep nesting**: 400-500% faster (50-100 directory levels)
- **Bulk operations**: 590-656% faster (multiple file scans)
- **Edge cases**: 900%+ faster (single-level paths with no parent)
The optimization is particularly effective for the common case of scanning many normal directories (no special dirs), where it achieves 600-1000% speedup by quickly determining both counts are zero.
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.
📄 532% (5.32x) speedup for
_is_inside_unrequested_special_dirinsrc/datasets/data_files.py⏱️ Runtime :
4.11 milliseconds→650 microseconds(best of6runs)📝 Explanation and details
The optimized code achieves a 532% speedup (4.11ms → 650μs) by eliminating the heavyweight
PurePathobject creation overhead and replacing it with lightweight string operations.Key Performance Optimizations
1. Eliminated PurePath Object Construction
The original code creates two
PurePathobjects per function call, which involves significant parsing and validation overhead. The optimized version uses simple string operations (rfind,split) that operate directly on the input strings without any object instantiation.2. Replaced List Comprehensions with Manual Counting
Instead of building intermediate lists with list comprehensions (
[part for part in ... if part.startswith("__")]), the optimized code counts special directories in-place. This avoids memory allocation for temporary lists and the associated iteration overhead.3. Direct String Manipulation
rfind('/')to locate the last separator (O(n) scan but no parsing)split('/')only when needed, then iterates once to countWhy This Works
The original implementation's bottleneck (as shown in line profiler) is spending 98.8% of time in just two lines creating PurePath objects. The optimized version:
PurePathconstructor overhead entirelyImpact on Workloads
Based on
function_references, this function is called fromresolve_pattern()in a hot path - it filters every matched file path in glob results. The speedup directly benefits:Test Case Performance
The optimization excels across all test scenarios:
__pycache__/b.txtcases)The optimization is particularly effective for the common case of scanning many normal directories (no special dirs), where it achieves 600-1000% speedup by quickly determining both counts are zero.
✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
test_data_files.py::test_is_inside_unrequested_special_dirtest_data_files.py::test_is_unrequested_hidden_file_or_is_inside_unrequested_hidden_dir🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-_is_inside_unrequested_special_dir-mlcly82uand push.