New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-90699: Use _Py_STR(empty) instead of PyUnicode_New(0, 0) #91476
Conversation
Modules/_ctypes/_ctypes.c
Outdated
| @@ -4635,7 +4635,7 @@ Array_subscript(PyObject *myself, PyObject *item) | |||
| wchar_t *dest; | |||
|
|
|||
| if (slicelen <= 0) | |||
| return PyUnicode_New(0, 0); | |||
| return Py_NewRef(&_Py_STR(empty)); | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity, what is the benefit of this change except inline calling?
AFAIK, PyUnicode_New(0, 0); will finally call unicode_get_empty which is the same replacement of _Py_STR(empty)
cpython/Objects/unicodeobject.c
Lines 1372 to 1377 in f33e2c8
| PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) | |
| { | |
| /* Optimization for empty strings */ | |
| if (size == 0) { | |
| return unicode_new_empty(); | |
| } |
cpython/Objects/unicodeobject.c
Lines 259 to 271 in f33e2c8
| static inline PyObject* unicode_get_empty(void) | |
| { | |
| return &_Py_STR(empty); | |
| } | |
| // Return a strong reference to the empty string singleton. | |
| static inline PyObject* unicode_new_empty(void) | |
| { | |
| PyObject *empty = unicode_get_empty(); | |
| Py_INCREF(empty); | |
| return empty; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK, @ericsnowcurrently try to reduce private API from the stdlib module as the best practice for 3rd party users who try to write C extension module by referencing stdlib implementation..
So I don't think this is a good replacement if there are no significant improvements.
Please let me know if I know wrong Eric :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense. I had first seen that ceval.c could change, so I made similar changes elsewhere, but if the other code is better left alone, I don't mind.
I updated this PR to only have the ceval.c change, where the code can actually get smaller and have fewer branches.
LGTM
I am +1 with an aggressive optimization approach for opcode evaluation :)
No description provided.