Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve the "ASCReader" read performance. #1717

Merged
merged 6 commits into from
Dec 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions can/io/asc.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import re
import time
from datetime import datetime
from typing import Any, Dict, Generator, List, Optional, TextIO, Union
from typing import Any, Dict, Final, Generator, List, Optional, TextIO, Union

from ..message import Message
from ..typechecking import StringPathLike
Expand All @@ -20,6 +20,14 @@
CAN_ID_MASK = 0x1FFFFFFF
BASE_HEX = 16
BASE_DEC = 10
ASC_TRIGGER_REGEX: Final = re.compile(
r"begin\s+triggerblock\s+\w+\s+(?P<datetime_string>.+)", re.IGNORECASE
)
ASC_MESSAGE_REGEX: Final = re.compile(
r"\d+\.\d+\s+(\d+\s+(\w+\s+(Tx|Rx)|ErrorFrame)|CANFD)",
re.ASCII | re.IGNORECASE,
)


logger = logging.getLogger("can.io.asc")

Expand Down Expand Up @@ -258,12 +266,7 @@ def __iter__(self) -> Generator[Message, None, None]:
for _line in self.file:
line = _line.strip()

trigger_match = re.match(
r"begin\s+triggerblock\s+\w+\s+(?P<datetime_string>.+)",
line,
re.IGNORECASE,
)
if trigger_match:
if trigger_match := ASC_TRIGGER_REGEX.match(line):
datetime_str = trigger_match.group("datetime_string")
self.start_time = (
0.0
Expand All @@ -272,11 +275,7 @@ def __iter__(self) -> Generator[Message, None, None]:
)
continue

if not re.match(
r"\d+\.\d+\s+(\d+\s+(\w+\s+(Tx|Rx)|ErrorFrame)|CANFD)",
line,
re.ASCII | re.IGNORECASE,
):
if not ASC_MESSAGE_REGEX.match(line):
# line might be a comment, chip status,
# J1939 message or some other unsupported event
continue
Expand Down