Skip to content

Commit

Permalink
perf[venom]: improve OrderedSet operations (#4246)
Browse files Browse the repository at this point in the history
improve time spent in venom by 25%

dict views support set operations, which allows us to loop, using
`&=`. since our intersections are typically not between a large number
of OrderedSets, this results in faster execution time.

references:
- https://docs.python.org/3/library/stdtypes.html#dict-views
  • Loading branch information
charles-cooper committed Sep 25, 2024
1 parent 48a5da4 commit 957c8ed
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions vyper/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ class OrderedSet(Generic[_T]):
"""

def __init__(self, iterable=None):
self._data = dict()
if iterable is not None:
self.update(iterable)
if iterable is None:
self._data = dict()
else:
self._data = dict.fromkeys(iterable)

def __repr__(self):
keys = ", ".join(repr(k) for k in self)
Expand Down Expand Up @@ -57,6 +58,7 @@ def pop(self):
def add(self, item: _T) -> None:
self._data[item] = None

# NOTE to refactor: duplicate of self.update()
def addmany(self, iterable):
for item in iterable:
self._data[item] = None
Expand Down Expand Up @@ -109,11 +111,11 @@ def intersection(cls, *sets):
if len(sets) == 0:
raise ValueError("undefined: intersection of no sets")

ret = sets[0].copy()
for e in sets[0]:
if any(e not in s for s in sets[1:]):
ret.remove(e)
return ret
tmp = sets[0]._data.keys()
for s in sets[1:]:
tmp &= s._data.keys()

return cls(tmp)


class StringEnum(enum.Enum):
Expand Down

0 comments on commit 957c8ed

Please sign in to comment.