-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
[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
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
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
| Py_ssize_t str1_length = PyUnicode_GET_LENGTH(str1); | ||
| if (str1_length != str2_length) | ||
| return 0; | ||
| int kind = PyUnicode_KIND(str1); |
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.
Can we deduce a literal's kind at compile time as well?
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.
Looks like there isn't a good way to reliably do this
| if is_string_literal(lhs): | ||
| if is_string_literal(rhs): | ||
| # we can optimize out the check entirely in some constant-folded cases | ||
| return self.true() if lhs.value == rhs.value else self.false() |
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.
Add a irbuild test cases for constant folding.
| return self.true() if lhs.value == rhs.value else self.false() | ||
|
|
||
| # if lhs argument is string literal, switch sides to match specializer C api | ||
| lhs, rhs = rhs, 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.
Add irbuild test case for string literal as the 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.
Added both
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?