⚡️ Speed up function unponc by 122%
#9
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.
📄 122% (1.22x) speedup for
unponcinspacy/lang/ga/lemmatizer.py⏱️ Runtime :
648 microseconds→292 microseconds(best of250runs)📝 Explanation and details
The optimization replaces a Python loop-based character replacement approach with Python's built-in
str.translate()method, achieving a 121% speedup.Key changes:
PONCmapping is now defined as a module-level_PONCconstant, eliminating the overhead of recreating the dictionary on every function call (11.1% of original runtime was spent on dictionary creation)._TRANSLATION_MAPconverts string keys to Unicode ordinals (ord(k)) as required bystr.translate(), created once at module load time.str.translate(): The manual character iteration, dictionary lookups, list appending, and string joining is replaced with a single call to the optimized C implementation oftranslate().Why this is faster:
str.translate()performs this work in optimized C code.Performance characteristics:
The optimization maintains identical behavior while leveraging Python's highly optimized string translation machinery.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-unponc-mhmj9x1xand push.