Skip to content

Comments

[mypyc] Add str.isdigit() primitive#20893

Open
VaggelisD wants to merge 1 commit intopython:masterfrom
VaggelisD:str_isdigit
Open

[mypyc] Add str.isdigit() primitive#20893
VaggelisD wants to merge 1 commit intopython:masterfrom
VaggelisD:str_isdigit

Conversation

@VaggelisD
Copy link
Contributor

Similar issue as the str.isalnum() PR, for large enough strings the primitive introduces a perf regression but is generally faster on common cases:


All-digit strings (100M calls each) Python (s) mypyc (s) Speedup
length 1 ('0') 2.089 0.656 3.2x
length 10 ('1234567890') 2.475 1.028 2.4x
length 100 ('5' * 100) 6.106 3.406 1.8x
length 1 (UCS-2: U+0660 ٠) 2.110 0.734 2.9x
length 10 (UCS-2: U+0660 * 10) 2.907 2.041 1.4x
length 100 (UCS-2: U+0660 * 100) 10.596 13.887 0.8x

Non-digit strings (100M calls each) Python (s) mypyc (s) Speedup
length 1 ('a') 2.022 0.585 3.5x
length 100 ('a' * 100) 2.068 0.587 3.5x
length 100 ('0' * 99 + 'a') 7.304 3.474 2.1x



bool CPyStr_IsDigit(PyObject *str) {
Py_ssize_t len = PyUnicode_GET_LENGTH(str);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a recurring pattern for these primitives, should we try to abstract their codegen?

Gave macros a shot for to hide the per-kind for loop, though we could go a step further and do the same for entire functions I guess.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant