Skip to content

Commit

Permalink
Use dict keys for order-preserving dedupes instead of set + list (#15105
Browse files Browse the repository at this point in the history
)

I figured this might be faster and less code.
  • Loading branch information
adriangb authored Apr 23, 2023
1 parent bd6ce23 commit 315b466
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions mypyc/ir/rtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,17 +820,11 @@ def make_simplified_union(items: list[RType]) -> RType:
items = flatten_nested_unions(items)
assert items

# Remove duplicate items using set + list to preserve item order
seen = set()
new_items = []
for item in items:
if item not in seen:
new_items.append(item)
seen.add(item)
if len(new_items) > 1:
return RUnion(new_items)
unique_items = dict.fromkeys(items)
if len(unique_items) > 1:
return RUnion(list(unique_items))
else:
return new_items[0]
return next(iter(unique_items))

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

0 comments on commit 315b466

Please sign in to comment.