Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 34 additions & 45 deletions mypyc/irbuild/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -1271,53 +1271,42 @@ def try_specialize_in_expr(

if items is not None:
assert left is not None
n_items = len(items)
# x in y -> x == y[0] or ... or x == y[n]
# x not in y -> x != y[0] and ... and x != y[n]
if n_items > 1:
if op == "in":
cmp_op = "=="
else:
cmp_op = "!="
out = BasicBlock()
for item in items:
cmp = transform_basic_comparison(builder, cmp_op, left, item, line)
bool_val = builder.builder.bool_value(cmp)
if not items:
# x in []/() -> False
# x not in []/() -> True
return builder.false() if op == "in" else builder.true()
# x in y -> x is y[0] or x == y[0] or ... or x is y[n] or x == y[n]
# x not in y -> not (x in y)
found = BasicBlock()
for item in items:
if item is left:
# Same value (e.g. "x in (x,)"), so it's always found
builder.goto(found)
builder.activate_block(BasicBlock())
break
Comment on lines +1282 to +1286

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

could we check this before the loop so expressions like x in (a, b, c, x) go to found immediately? i think now we would still first compare with all preceding elements.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think comparing the preceding items matches CPython semantics (equality comparison can run arbitrary code), so I'd keep it as is. This case isn't useful really, so performance improvement is theoretical.

if not (
left.type.is_unboxed
or item.type.is_unboxed
# String equality checks also check identity
or (is_str_rprimitive(left.type) and is_str_rprimitive(item.type))
):
next_block = BasicBlock()
if op == "in":
builder.add_bool_branch(bool_val, out, next_block)
else:
builder.add_bool_branch(bool_val, next_block, out)
is_same = builder.binary_op(left, item, "is", line)
builder.add_bool_branch(is_same, found, next_block)
builder.activate_block(next_block)
result_reg = Register(bool_rprimitive)
end = BasicBlock()
if op == "in":
values = builder.false(), builder.true()
else:
values = builder.true(), builder.false()
builder.assign(result_reg, values[0], line)
builder.goto(end)
builder.activate_block(out)
builder.assign(result_reg, values[1], line)
builder.goto(end)
builder.activate_block(end)
return result_reg
# x in [y]/(y) -> x == y
# x not in [y]/(y) -> x != y
elif n_items == 1:
if op == "in":
cmp_op = "=="
else:
cmp_op = "!="
right = items[0]
return transform_basic_comparison(builder, cmp_op, left, right, line)
# x in []/() -> False
# x not in []/() -> True
elif n_items == 0:
if op == "in":
return builder.false()
else:
return builder.true()
cmp = transform_basic_comparison(builder, "==", left, item, line)
next_block = BasicBlock()
builder.add_bool_branch(builder.builder.bool_value(cmp), found, next_block)
builder.activate_block(next_block)
result_reg = Register(bool_rprimitive)
end = BasicBlock()
builder.assign(result_reg, builder.false() if op == "in" else builder.true(), line)
builder.goto(end)
builder.activate_block(found)
builder.assign(result_reg, builder.true() if op == "in" else builder.false(), line)
builder.goto(end)
builder.activate_block(end)
return result_reg

# x in {...}
# x not in {...}
Expand Down
8 changes: 4 additions & 4 deletions mypyc/test-data/irbuild-tuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,11 @@ L0:
r0 = z()
r1 = x()
r2 = y()
r3 = int_ne r0, r1
if r3 goto L1 else goto L3 :: bool
r3 = int_eq r0, r1
if r3 goto L3 else goto L1 :: bool
L1:
r4 = int_ne r0, r2
if r4 goto L2 else goto L3 :: bool
r4 = int_eq r0, r2
if r4 goto L3 else goto L2 :: bool
L2:
r5 = 1
goto L4
Expand Down
29 changes: 29 additions & 0 deletions mypyc/test-data/run-tuples.test
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,35 @@ def test_tuple_in_non_specialized() -> None:
assert f_b() not in f_t()
assert log == ["f_b", "f_t"]

class NeverEqual:
def __eq__(self, other: object) -> bool:
return False

def __ne__(self, other: object) -> bool:
return False

def test_tuple_and_list_in_identity() -> None:
# Membership tests compare identity before equality
x = NeverEqual()
y = NeverEqual()
assert x in (x,)
assert x in [x]
assert x in (y, x)
assert x in [y, x]
assert y not in (x,)
assert y not in [x]
assert y not in (x, x)
assert y not in [x, x]
assert not (x not in (x,))
assert not (x not in [y, x])
a: Any = x
assert a in (a,)
assert a in (y, a)
assert not (a not in (y, a))
f = float("nan")
assert f in (f,)
assert f in [1.0, f]

def test_add() -> None:
res = (1, 2, 3, 4)
assert (1, 2) + (3, 4) == res
Expand Down
Loading