⚡️ Speed up function strip_accents_text by 42%
#11
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.
📄 42% (0.42x) speedup for
strip_accents_textinspacy/lang/yo/lex_attrs.py⏱️ Runtime :
4.86 milliseconds→3.42 milliseconds(best of250runs)📝 Explanation and details
The optimization achieves a 42% speedup through two key micro-optimizations that reduce function call overhead in the Unicode processing loop:
What was optimized:
unicodedata.normalizeandunicodedata.categorylookups outside the loop by storing them as local variablesnormalizeandcategory"".join()Why this speeds up the code:
unicodedata.normalizeandunicodedata.categorythrough global module attribute lookups. The optimized version performs these lookups once and stores them as local variables, which are faster to access in Python.join(), CPython's implementation ofjoin()can be slightly more efficient when given a list directly rather than consuming a generator.Performance impact based on test results:
The optimization shows consistent improvements across all test cases, with particularly strong gains on:
Real-world benefits:
This function appears to be part of spaCy's Yoruba language processing pipeline for lexical attribute handling. Given that text preprocessing often involves processing large volumes of text with many accented characters, these micro-optimizations compound significantly. The improvements are most pronounced on longer texts and Unicode-heavy content, making this optimization particularly valuable for natural language processing workloads where accent stripping is performed repeatedly on large corpora.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-strip_accents_text-mhmjqmvmand push.