⚡️ Speed up function sanitize_content_disposition by 15% in PR #10819 (s3-file-size-and-associations-to-flows)#10824
Closed
codeflash-ai[bot] wants to merge 2 commits into
Conversation
The optimized code achieves a **15% speedup** through two key performance optimizations: **What was optimized:** 1. **Precompiled regex pattern**: Moved `re.compile(r"[^\w.\- ()]")` to module scope as `_SANITIZE_FILENAME_RE`, eliminating regex compilation overhead on every function call. 2. **Faster path extraction**: Replaced `Path(filename).name` with `PurePath(filename).name`. `PurePath` is a lighter-weight class that handles path operations without filesystem access or validation, making it faster for simple string operations like extracting the filename component. **Why this leads to speedup:** - **Regex compilation cost**: The line profiler shows the original `re.sub()` call took 7.5ms (14.1% of total time). With precompilation, this drops to 1.8ms (4.1% of total time) - a **76% reduction** in regex processing time. - **Path object overhead**: `Path` objects include filesystem validation and OS-specific behavior that's unnecessary when we only need to extract the basename. `PurePath` reduces this overhead from 41.5ms to 37.8ms - an **9% improvement** in path processing. **Impact on workloads:** The optimizations are most beneficial for: - **High-frequency filename sanitization** (evident from the 1,663 test iterations) - **Batch file processing scenarios** where the same sanitization logic runs repeatedly - **Web upload handlers** processing multiple files simultaneously **Test case performance:** The annotated tests show consistent improvements across all scenarios - from simple ASCII filenames to complex Unicode cases with path traversal attempts. The optimization maintains identical behavior while reducing CPU overhead, making it particularly valuable for file upload endpoints that may process hundreds of filenames per request.
Contributor
|
Important Review skippedBot user detected. To trigger a single review, invoke the You can disable this status message by setting the Comment |
Contributor
Codecov Report✅ All modified and coverable lines are covered by tests. ❌ Your project status has failed because the head coverage (40.04%) is below the target coverage (60.00%). You can increase the head coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## s3-file-size-and-associations-to-flows #10824 +/- ##
=======================================================================
Coverage 32.39% 32.39%
=======================================================================
Files 1367 1367
Lines 63235 63225 -10
Branches 9358 9357 -1
=======================================================================
- Hits 20482 20479 -3
+ Misses 41720 41714 -6
+ Partials 1033 1032 -1
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Contributor
|
Closing automated codeflash 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 #10819
If you approve this dependent PR, these changes will be merged into the original PR branch
s3-file-size-and-associations-to-flows.📄 15% (0.15x) speedup for
sanitize_content_dispositioninsrc/backend/base/langflow/api/v2/files.py⏱️ Runtime :
8.03 milliseconds→6.95 milliseconds(best of63runs)📝 Explanation and details
The optimized code achieves a 15% speedup through two key performance optimizations:
What was optimized:
Precompiled regex pattern: Moved
re.compile(r"[^\w.\- ()]")to module scope as_SANITIZE_FILENAME_RE, eliminating regex compilation overhead on every function call.Faster path extraction: Replaced
Path(filename).namewithPurePath(filename).name.PurePathis a lighter-weight class that handles path operations without filesystem access or validation, making it faster for simple string operations like extracting the filename component.Why this leads to speedup:
Regex compilation cost: The line profiler shows the original
re.sub()call took 7.5ms (14.1% of total time). With precompilation, this drops to 1.8ms (4.1% of total time) - a 76% reduction in regex processing time.Path object overhead:
Pathobjects include filesystem validation and OS-specific behavior that's unnecessary when we only need to extract the basename.PurePathreduces this overhead from 41.5ms to 37.8ms - an 9% improvement in path processing.Impact on workloads:
The optimizations are most beneficial for:
Test case performance:
The annotated tests show consistent improvements across all scenarios - from simple ASCII filenames to complex Unicode cases with path traversal attempts. The optimization maintains identical behavior while reducing CPU overhead, making it particularly valuable for file upload endpoints that may process hundreds of filenames per request.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr10819-2025-12-01T20.28.11and push.