Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use dict keys for order-preserving dedupes instead of set + list #15105

Merged
merged 8 commits into from
Apr 23, 2023
Merged
Changes from 6 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
16 changes: 5 additions & 11 deletions mypyc/ir/rtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from __future__ import annotations

from abc import abstractmethod
from typing import TYPE_CHECKING, ClassVar, Generic, TypeVar
from typing import TYPE_CHECKING, Any, ClassVar, Generic, TypeVar
from typing_extensions import Final

from mypyc.common import IS_32_BIT_PLATFORM, PLATFORM_SIZE, JsonDict, short_name
Expand Down 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 = list(dict.fromkeys(items))
if len(unique_items) > 1:
return RUnion(unique_items)
else:
return new_items[0]
return unique_items[0]
Copy link
Member

Choose a reason for hiding this comment

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

No idea if it actually makes a difference in terms of performance, but I feel like I mildly preferred your earlier idea of only casting it to a list if it's actually necessary, i.e.:

        unique_items = dict.fromkeys(items)
        if len(unique_items) > 1:
            return RUnion(list(unique_items))
        else:
            return next(iter(unique_items))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems to indeed have a small impact:
image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So yeah reverted to that


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