Skip to content

Commit

Permalink
Slightly faster PortRef comparisons
Browse files Browse the repository at this point in the history
Use Python slots for faster lookup and try-except instead of if-else
  • Loading branch information
nikosavola committed Jun 25, 2024
1 parent 4f9a709 commit 5bd12fc
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions hdl21/portref.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ class PortRef:
inst: _Instance
portname: str

__slots__ = (
"inst",
"portname",
"_connected_ports",
"resolved",
"_slices",
"_concats",
"_width",
)

def __post_init__(self):
# Inner management data
self._connected_ports: Set[PortRef] = set()
Expand All @@ -35,9 +45,10 @@ def __post_init__(self):
def __eq__(self, other) -> bool:
"""Port-reference equality requires *identity* between instances
(and of course equality of port-name)."""
if not isinstance(other, PortRef):
try:
return self.inst is other.inst and self.portname == other.portname
except AttributeError:
return False
return self.inst is other.inst and self.portname == other.portname

def __hash__(self):
"""Hash references as the tuple of their instance-address and name"""
Expand Down

0 comments on commit 5bd12fc

Please sign in to comment.