⚡️ Speed up function _strip_ansi by 116% in PR #217 (proper-cleanup)
#223
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 #217
If you approve this dependent PR, these changes will be merged into the original PR branch
proper-cleanup.📄 116% (1.16x) speedup for
_strip_ansiincodeflash/code_utils/tabulate.py⏱️ Runtime :
4.98 milliseconds→2.30 milliseconds(best of164runs)📝 Explanation and details
Here is an optimized version of
_strip_ansifocusing on runtime speed. The main bottleneck is the regular expression replacement inre.sub, specifically returning the (possibly empty) group 4 (link text in hyperlinks) or, if it doesn’t match, an empty string for ANSI codes.This can be significantly sped up by avoiding the costly
r"\4"(which always triggers group resolution machinery in the regex engine), and instead using a faster replacer callback.Since every match will be either an ANSI escape code (where group 4 is
None) or a hyperlink (where group 4 contains the visible link text), we can handle both cases in one simple function.Optimized
code.Performance notes:
Comment:
No original comments were changed or removed.
No changes made to the public interface or expected output.
All logic concerning group 4 and escape sequence removal is preserved.
✅ Correctness verification report:
🌀 Generated Regression Tests Details
To edit these changes
git checkout codeflash/optimize-pr217-2025-05-19T04.39.46and push.