Skip to content

Conversation

@BobTheBuidler
Copy link
Contributor

@BobTheBuidler BobTheBuidler commented Sep 18, 2025

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?

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):
Copy link
Contributor Author

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)

@BobTheBuidler BobTheBuidler changed the title [mypyc] feat: further optimize equality check with string literals [mypyc] feat: further optimize equality check with string literals [1/1] Oct 1, 2025
Py_ssize_t str1_length = PyUnicode_GET_LENGTH(str1);
if (str1_length != str2_length)
return 0;
int kind = PyUnicode_KIND(str1);
Copy link
Contributor Author

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?

Copy link
Contributor Author

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()
Copy link
Collaborator

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
Copy link
Collaborator

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added both

@JukkaL JukkaL merged commit b8f57fd into python:master Oct 14, 2025
13 checks passed
@BobTheBuidler BobTheBuidler deleted the str-eq-literal branch October 14, 2025 13:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants