Skip to content
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

Merged
merged 4 commits into from Apr 15, 2022

Conversation

Copy link
Member

@sweeneyde sweeneyde commented Apr 12, 2022

No description provided.

@sweeneyde sweeneyde changed the title Use _Py_STR(empty) instead of PyUnicode_New(0, 0) gh-90699: Use _Py_STR(empty) instead of PyUnicode_New(0, 0) Apr 12, 2022
@@ -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));
Copy link
Member

@corona10 corona10 Apr 12, 2022

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)

PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar)
{
/* Optimization for empty strings */
if (size == 0) {
return unicode_new_empty();
}

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;
}

Copy link
Member

@corona10 corona10 Apr 12, 2022

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 :)

Copy link
Member Author

@sweeneyde sweeneyde Apr 14, 2022

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.

@sweeneyde sweeneyde requested review from corona10 Apr 15, 2022
Copy link
Member

@corona10 corona10 left a comment

LGTM
I am +1 with an aggressive optimization approach for opcode evaluation :)

@corona10 corona10 merged commit 7296598 into python:main Apr 15, 2022
12 checks passed
@sweeneyde sweeneyde deleted the empty branch Apr 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants