From 5bd12fced53ba9ab65f2e165edc2e47c5ac53f16 Mon Sep 17 00:00:00 2001 From: Niko Savola Date: Tue, 25 Jun 2024 13:49:58 +0300 Subject: [PATCH] Slightly faster `PortRef` comparisons Use Python slots for faster lookup and try-except instead of if-else --- hdl21/portref.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/hdl21/portref.py b/hdl21/portref.py index d7d0a1d..dcebbe6 100644 --- a/hdl21/portref.py +++ b/hdl21/portref.py @@ -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() @@ -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"""