⚡️ Speed up function wcwidth by 13%
#68
Open
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.
📄 13% (0.13x) speedup for
wcwidthinsrc/_pytest/_io/wcwidth.py⏱️ Runtime :
1.34 milliseconds→1.19 milliseconds(best of114runs)📝 Explanation and details
The optimization achieves a 12% speedup by replacing runtime range checks and tuple creation with precomputed sets and constants for faster membership testing.
Key Optimizations:
Set-based lookups: The complex range comparisons
(o == 0x0000 or 0x200B <= o <= 0x200F or ...)are replaced with a single set lookupo in _Cf_Zp_Zl_SET. Python's set membership testing uses hash tables, making it O(1) vs O(n) for multiple range checks.Precomputed category sets: String tuple comparisons like
category in ("Me", "Mn")andunicodedata.east_asian_width(c) in ("F", "W")are replaced with precomputed sets_COMBINING_CATEGORIESand_EAWIDE, eliminating tuple allocation on each call.Performance Impact:
The function is called in a hot path by
wcswidth()which iterates over every character in strings for terminal width calculation. Test results show consistent improvements:Best Performance Cases:
The optimization particularly benefits workloads with:
wcswidth()on text with non-ASCII charactersThe changes maintain identical behavior while leveraging Python's optimized set operations for faster character classification.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic__lsdxkww/tmpa3lxvdfa/test_concolic_coverage.py::test_wcwidthcodeflash_concolic__lsdxkww/tmpa3lxvdfa/test_concolic_coverage.py::test_wcwidth_2codeflash_concolic__lsdxkww/tmpa3lxvdfa/test_concolic_coverage.py::test_wcwidth_3codeflash_concolic__lsdxkww/tmpa3lxvdfa/test_concolic_coverage.py::test_wcwidth_4codeflash_concolic__lsdxkww/tmpa3lxvdfa/test_concolic_coverage.py::test_wcwidth_5codeflash_concolic__lsdxkww/tmpa3lxvdfa/test_concolic_coverage.py::test_wcwidth_6To edit these changes
git checkout codeflash/optimize-wcwidth-mi9iu0mgand push.