[mypyc] Add str.isdigit() primitive#20893
Conversation
|
|
||
|
|
||
| bool CPyStr_IsDigit(PyObject *str) { | ||
| Py_ssize_t len = PyUnicode_GET_LENGTH(str); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
One option would be to have an inline function which gets passed a function pointer to represent the variable functionality, on the assumption that C compilers can simplify all the overhead away (not sure if this is the case, but it might well be).
Another idea would be to add a template for all of these functions in a comment at the top of the file, and we could just ask Claude Code or Codex to create another function based on the template for a new use case. And if we update the template, we could use a coding agent to update all instances of the template in the code. The problem with this is that there would be no automatic validation against things being consistent, but we could add some comments warning against manual edits.
0fd7932 to
819be52
Compare
|
Was probably one, fixed after rebasing. |
819be52 to
4b3dddf
Compare
JukkaL
left a comment
There was a problem hiding this comment.
Thanks for the PR! Looks good, but there are now some merge conflicts. Can you fix them?
|
|
||
|
|
||
| bool CPyStr_IsDigit(PyObject *str) { | ||
| Py_ssize_t len = PyUnicode_GET_LENGTH(str); |
There was a problem hiding this comment.
One option would be to have an inline function which gets passed a function pointer to represent the variable functionality, on the assumption that C compilers can simplify all the overhead away (not sure if this is the case, but it might well be).
Another idea would be to add a template for all of these functions in a comment at the top of the file, and we could just ask Claude Code or Codex to create another function based on the template for a new use case. And if we update the template, we could use a coding agent to update all instances of the template in the code. The problem with this is that there would be no automatic validation against things being consistent, but we could add some comments warning against manual edits.
Similar issue as the str.isalnum() PR, for large enough strings the primitive introduces a perf regression but is generally faster on common cases:
'0')'1234567890')'5' * 100)٠)'a')'a' * 100)'0' * 99 + 'a')