From e85d444acbaa561291d9af99e19deebde5c36324 Mon Sep 17 00:00:00 2001 From: Philip Mueller Date: Fri, 13 Dec 2024 11:08:04 +0100 Subject: [PATCH] Performance improvement in the `add_{in, out}_connector()` functions of the node. Before every time the function was (shallow) copying the `dict` upon a change. This was done to make use of the implicit casting functionality of the `DictProperty`, however, it is not very efficient. This commit changes the implementation of the function to perform the casting explicitly, thus avoiding the need to copy the `dict` every time. --- dace/sdfg/nodes.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/dace/sdfg/nodes.py b/dace/sdfg/nodes.py index 2b75a73ffa..d717c0a85f 100644 --- a/dace/sdfg/nodes.py +++ b/dace/sdfg/nodes.py @@ -108,40 +108,40 @@ def to_json(self, parent): def __repr__(self): return type(self).__name__ + ' (' + self.__str__() + ')' - def add_in_connector(self, connector_name: str, dtype: dtypes.typeclass = None, force: bool = False): + def add_in_connector(self, connector_name: str, dtype: Any = None, force: bool = False): """ Adds a new input connector to the node. The operation will fail if a connector (either input or output) with the same name already exists in the node. :param connector_name: The name of the new connector. :param dtype: The type of the connector, or None for auto-detect. - :param force: Add connector even if output connector already exists. + :param force: Add connector even if input or output connector of that name already exists. :return: True if the operation is successful, otherwise False. """ if (not force and (connector_name in self.in_connectors or connector_name in self.out_connectors)): return False - connectors = self.in_connectors - connectors[connector_name] = dtype - self.in_connectors = connectors + if not isinstance(dtype, dace.typeclass): + dtype = dace.typeclass(dtype) + self.in_connectors[connector_name] = dtype return True - def add_out_connector(self, connector_name: str, dtype: dtypes.typeclass = None, force: bool = False): + def add_out_connector(self, connector_name: str, dtype: Any = None, force: bool = False,) -> bool: """ Adds a new output connector to the node. The operation will fail if a connector (either input or output) with the same name already exists in the node. :param connector_name: The name of the new connector. :param dtype: The type of the connector, or None for auto-detect. - :param force: Add connector even if input connector already exists. + :param force: Add connector even if input or output connector of that name already exists. :return: True if the operation is successful, otherwise False. """ if (not force and (connector_name in self.in_connectors or connector_name in self.out_connectors)): return False - connectors = self.out_connectors - connectors[connector_name] = dtype - self.out_connectors = connectors + if not isinstance(dtype, dace.typeclass): + dtype = dace.typeclass(dtype) + self.out_connectors[connector_name] = dtype return True def remove_in_connector(self, connector_name: str):