-
-
Notifications
You must be signed in to change notification settings - Fork 3k
[mypyc] feat: further optimize equality check with string literals [1/1] #19883
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
base: master
Are you sure you want to change the base?
Conversation
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
} | ||
Py_ssize_t len = PyUnicode_GET_LENGTH(str1); | ||
if (PyUnicode_GET_LENGTH(str2) != len) | ||
static char _CPyStr_Equal_NoIdentCheck(PyObject *str1, PyObject *str2, Py_ssize_t str2_length) { |
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.
I'm not sure if it makes sense to re-duplicate the code and get rid of this helper
how much does an extra C call slow things down? The de-duplicated functions aren't exactly clean even with the helper so I don't really have a strong opinion either way
mypyc/irbuild/ll_builder.py
Outdated
return self.primitive_op(str_eq, [lhs, rhs], line) | ||
elif op == "!=": | ||
eq = self.primitive_op(str_eq, [lhs, rhs], line) | ||
if is_string_literal(lhs): |
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.
looking at this again, I think we can just refactor this whole block for "!=" into:
return self.add(ComparisonOp(compare_strings(lhs, rhs, line), self.false(), ComparisonOp.EQ, line)
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
This PR further optimizes string equality checks against literals by getting rid of the PyUnicode_GET_LENGTH call against the literal value, which is not necessary since the value is known at compile-time
I think this optimization will be helpful in cases where the non-literal string DOES match but is actually a subtype of string (actual strings instances that match would be caught by the identity check), or in cases where an exact string does NOT match. But we can also extend this implementation to use c-strings in certain cases where we know at compile-time that the literal value is compact ascii. Actually, maybe I should do that now? Thoughts?