[mypyc] Add str.isalnum() primitive#20852
Open
VaggelisD wants to merge 1 commit intopython:masterfrom
Open
Conversation
VaggelisD
commented
Feb 20, 2026
| * ``s1.find(s2: str)`` | ||
| * ``s1.find(s2: str, start: int)`` | ||
| * ``s1.find(s2: str, start: int, end: int)`` | ||
| * ``s.isspace()`` |
Contributor
Author
There was a problem hiding this comment.
This was not documented in the str.isspace() PR, added it now
JukkaL
reviewed
Feb 20, 2026
|
|
||
| int kind = PyUnicode_KIND(str); | ||
| const void *data = PyUnicode_DATA(str); | ||
| for (Py_ssize_t i = 0; i < len; i++) { |
Collaborator
There was a problem hiding this comment.
Performance might increase if there was a separate loop for 2 byte and 4 byte kinds. This way the read operation wouldn't need to branch based on kind, which might result in better code. Can you try this out?
JukkaL
reviewed
Feb 20, 2026
| Py_ssize_t len = PyUnicode_GET_LENGTH(str); | ||
| if (len == 0) return false; | ||
|
|
||
| if (PyUnicode_IS_ASCII(str)) { |
Collaborator
There was a problem hiding this comment.
Could this be PyUnicode_KIND(obj) == PyUnicode_1BYTE_KIND instead? This would be needed if the loop below was split into dedicated 2/4 byte loops.
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.
Added
str.isalnum()similar tostr.isspace().One interesting thing to point out here is that the benchmarks decline in speed relative to the string's length:
'a')'abcde12345')'a' * 100)é)' ')'!' * 100)'a' * 99 + '!')Not entirely sure how to interpret this but could it be because the Py_UNICODE_ISALNUM calls 4 functions internally which is more optimized in CPython due to PGO & LTO (?)