Skip to content

Commit 315b466

Browse files
authored
Use dict keys for order-preserving dedupes instead of set + list (#15105)
I figured this might be faster and less code.
1 parent bd6ce23 commit 315b466

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

mypyc/ir/rtypes.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -820,17 +820,11 @@ def make_simplified_union(items: list[RType]) -> RType:
820820
items = flatten_nested_unions(items)
821821
assert items
822822

823-
# Remove duplicate items using set + list to preserve item order
824-
seen = set()
825-
new_items = []
826-
for item in items:
827-
if item not in seen:
828-
new_items.append(item)
829-
seen.add(item)
830-
if len(new_items) > 1:
831-
return RUnion(new_items)
823+
unique_items = dict.fromkeys(items)
824+
if len(unique_items) > 1:
825+
return RUnion(list(unique_items))
832826
else:
833-
return new_items[0]
827+
return next(iter(unique_items))
834828

835829
def accept(self, visitor: RTypeVisitor[T]) -> T:
836830
return visitor.visit_runion(self)

0 commit comments

Comments
 (0)