⚡️ Speed up function standardize_quotes by 144%#4201
Open
KRRT7 wants to merge 8 commits intoUnstructured-IO:mainfrom
Open
⚡️ Speed up function standardize_quotes by 144%#4201KRRT7 wants to merge 8 commits intoUnstructured-IO:mainfrom
standardize_quotes by 144%#4201KRRT7 wants to merge 8 commits intoUnstructured-IO:mainfrom
Conversation
The optimized code achieves a **144% speedup** by replacing a loop-based character replacement approach with Python's built-in `str.translate()` method using a pre-computed translation table. ## Key Optimizations **1. Pre-computed Translation Table at Module Load** - The quote dictionaries and translation table are now created once at module import time (module-level constants prefixed with `_`) - Original code recreated these 40+ entry dictionaries on every function call (6.1% + 6.5% = 12.6% of runtime just for dictionary creation) - Translation table maps Unicode codepoints directly to ASCII quote codepoints, eliminating repeated string operations **2. Single-Pass O(n) Algorithm with `str.translate()`** - Original: Two loops iterating through ~40 quote types, calling `unicode_to_char()` 3,096 times (67.5% of total runtime) and performing substring searches with `in` operator (5.9% of runtime) - Optimized: Single `str.translate()` call that processes the entire string in one pass using efficient C-level implementation - Eliminates 3,096 function calls to `unicode_to_char()` and all associated string parsing/conversion overhead **3. Algorithmic Complexity Improvement** - Original: O(n × m) where n = text length, m = number of quote types (~40), with repeated `text.replace()` creating new string objects - Optimized: O(n) single pass through the text, with translation table lookups being O(1) ## Performance Context Based on `function_references`, this function is called from `calculate_edit_distance()`, which is likely in a **hot path** for text extraction metrics. The function processes strings before edit distance calculations, meaning: - Any text comparison workflow will call this repeatedly - The 144% speedup compounds when processing multiple documents or performing batch comparisons - Reduced memory allocation pressure from eliminating repeated dictionary creation and intermediate string objects ## Test Case Insights The test with input `"«'"` (containing both double and single quote variants) shows the optimization handles mixed quote types efficiently in a single pass, whereas the original code would iterate through all 40 quote types regardless of actual presence in the text.
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.
📄 144% (1.44x) speedup for
standardize_quotesinunstructured/metrics/text_extraction.py⏱️ Runtime :
128 microseconds→52.2 microseconds(best of170runs)📝 Explanation and details
The optimized code achieves a 144% speedup by replacing a loop-based character replacement approach with Python's built-in
str.translate()method using a pre-computed translation table.Key Optimizations
1. Pre-computed Translation Table at Module Load
_)2. Single-Pass O(n) Algorithm with
str.translate()unicode_to_char()3,096 times (67.5% of total runtime) and performing substring searches withinoperator (5.9% of runtime)str.translate()call that processes the entire string in one pass using efficient C-level implementationunicode_to_char()and all associated string parsing/conversion overhead3. Algorithmic Complexity Improvement
text.replace()creating new string objectsPerformance Context
Based on
function_references, this function is called fromcalculate_edit_distance(), which is likely in a hot path for text extraction metrics. The function processes strings before edit distance calculations, meaning:Test Case Insights
The test with input
"«'"(containing both double and single quote variants) shows the optimization handles mixed quote types efficiently in a single pass, whereas the original code would iterate through all 40 quote types regardless of actual presence in the text.✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
metrics/test_text_extraction.py::test_standardize_quotes🔎 Click to see Concolic Coverage Tests
codeflash_concolic_qdmvy_uv/tmpooe6tmfm/test_concolic_coverage.py::test_standardize_quotesTo edit these changes
git checkout codeflash/optimize-standardize_quotes-mklcp188and push.