From d4427294c7e35f8e2ff264a4d2a71cf840876c12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduard=20Br=C3=B6cker?= Date: Thu, 2 Jan 2025 10:10:18 +0100 Subject: [PATCH] speedup for frame_by_id (#825) * speedup for frame_by_id #774 * speedup for frame_by_id #774 * speedup for frame_by_id #774 --- src/canmatrix/canmatrix.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/canmatrix/canmatrix.py b/src/canmatrix/canmatrix.py index f88f5a53..e09a1a93 100644 --- a/src/canmatrix/canmatrix.py +++ b/src/canmatrix/canmatrix.py @@ -1793,7 +1793,7 @@ class CanMatrix(object): frames_dict_name = attr.ib(factory=dict) # type: typing.MutableSequence[Frame] frames_dict_id = attr.ib(factory=dict) # type: typing.MutableSequence[Frame] - + _frames_dict_id_extend = {} signal_defines = attr.ib(factory=dict) # type: typing.MutableMapping[str, Define] frame_defines = attr.ib(factory=dict) # type: typing.MutableMapping[str, Define] global_defines = attr.ib(factory=dict) # type: typing.MutableMapping[str, Define] @@ -1985,10 +1985,16 @@ def frame_by_id(self, arbitration_id): # type: (ArbitrationId) -> typing.Union[ :param ArbitrationId arbitration_id: Frame id as canmatrix.ArbitrationId :rtype: Frame or None """ - for test in self.frames: - if test.arbitration_id == arbitration_id: + hash_name = f"{arbitration_id.id}_{arbitration_id.extended}" + + frame = self._frames_dict_id_extend.get(hash_name, None) + if frame is not None: + return frame + for frame in self.frames: + if frame.arbitration_id == arbitration_id: # found ID while ignoring extended or standard - return test + self._frames_dict_id_extend[hash_name] = frame + return frame return None def frame_by_header_id(self, header_id): # type: (HeaderId) -> typing.Union[Frame, None] @@ -2090,7 +2096,7 @@ def add_frame(self, frame): # type: (Frame) -> Frame :return: the inserted Frame """ self.frames.append(frame) - + self._frames_dict_id_extend = {} self.frames_dict_name[frame.name] = frame if frame.header_id: self.frames_dict_id[frame.header_id] = frame @@ -2105,6 +2111,7 @@ def remove_frame(self, frame): # type: (Frame) -> None :param Frame frame: frame to remove from CAN Matrix """ self.frames.remove(frame) + self._frames_dict_id_extend = {} def add_signal(self, signal): # type: (Signal) -> Signal """ @@ -2199,6 +2206,8 @@ def add_ecu(self, ecu): # type(Ecu) -> None # todo return Ecu? if bu.name.strip() == ecu.name: return self.ecus.append(ecu) + self._frames_dict_id_extend = {} + def del_ecu(self, ecu_or_glob): # type: (typing.Union[Ecu, str]) -> None """Remove ECU from Matrix and all Frames. @@ -2369,6 +2378,7 @@ def merge(self, mergeArray): # type: (typing.Sequence[CanMatrix]) -> None else: logger.error( "Name Conflict, could not copy/merge EnvVar " + envVar) + self._frames_dict_id_extend = {} def set_fd_type(self) -> None: """Try to guess and set the CAN type for every frame.