Skip to content

Commit 6528245

Browse files
refactor
1 parent c3c04a5 commit 6528245

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

mypyc/irbuild/ll_builder.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,21 +1551,31 @@ def check_tagged_short_int(self, val: Value, line: int, negated: bool = False) -
15511551

15521552
def compare_strings(self, lhs: Value, rhs: Value, op: str, line: int) -> Value:
15531553
"""Compare two strings"""
1554+
1555+
def is_string_literal(value: Value) -> bool:
1556+
return isinstance(value, LoadLiteral) and is_str_rprimitive(value.type)
1557+
15541558
if op == "==":
1555-
if isinstance(lhs, LoadLiteral) and is_str_rprimitive(lhs.type):
1556-
literal_length = Integer(len(lhs.value), c_pyssize_t_rprimitive, line)
1559+
if is_string_literal(lhs):
1560+
if is_string_literal(rhs):
1561+
# we can optimize out the check entirely in some Final cases
1562+
return self.true() if lhs.value == rhs.value else self.false()
1563+
literal_length = Integer(len(lhs.value), c_pyssize_t_rprimitive, line) # type: ignore [arg-type]
15571564
return self.primitive_op(str_eq_literal, [rhs, lhs, literal_length], line)
1558-
elif isinstance(rhs, LoadLiteral) and is_str_rprimitive(rhs.type):
1559-
literal_length = Integer(len(rhs.value), c_pyssize_t_rprimitive, line)
1565+
elif is_string_literal(rhs):
1566+
literal_length = Integer(len(rhs.value), c_pyssize_t_rprimitive, line) # type: ignore [arg-type]
15601567
return self.primitive_op(str_eq_literal, [lhs, rhs, literal_length], line)
15611568
return self.primitive_op(str_eq, [lhs, rhs], line)
15621569
elif op == "!=":
1563-
if isinstance(lhs, LoadLiteral) and is_str_rprimitive(lhs.type):
1564-
literal_length = Integer(len(lhs.value), c_pyssize_t_rprimitive, line)
1565-
eq = self.primitive_op(str_eq_literal, [rhs, lhs, literal_length])
1566-
elif isinstance(rhs, LoadLiteral) and is_str_rprimitive(rhs.type):
1567-
literal_length = Integer(len(rhs.value), c_pyssize_t_rprimitive, line)
1568-
eq = self.primitive_op(str_eq_literal, [lhs, rhs, literal_length])
1570+
if is_string_literal(lhs):
1571+
if is_string_literal(rhs):
1572+
# we can optimize out the check entirely in some Final cases
1573+
return self.true() if lhs.value != rhs.value else self.false()
1574+
literal_length = Integer(len(lhs.value), c_pyssize_t_rprimitive, line) # type: ignore [arg-type]
1575+
eq = self.primitive_op(str_eq_literal, [rhs, lhs, literal_length], line)
1576+
elif is_string_literal(rhs):
1577+
literal_length = Integer(len(rhs.value), c_pyssize_t_rprimitive, line) # type: ignore [arg-type]
1578+
eq = self.primitive_op(str_eq_literal, [lhs, rhs, literal_length], line)
15691579
else:
15701580
eq = self.primitive_op(str_eq, [lhs, rhs], line)
15711581
return self.add(ComparisonOp(eq, self.false(), ComparisonOp.EQ, line))

0 commit comments

Comments
 (0)