Skip to content

Commit

Permalink
speedup for frame_by_id (#825)
Browse files Browse the repository at this point in the history
* speedup for frame_by_id

#774

* speedup for frame_by_id

#774

* speedup for frame_by_id

#774
  • Loading branch information
ebroecker authored Jan 2, 2025
1 parent 7fde135 commit d442729
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/canmatrix/canmatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand All @@ -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
"""
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit d442729

Please sign in to comment.