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 get_interface_attr speed through caching #1614

Closed
wants to merge 4 commits into from
Closed
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
49 changes: 32 additions & 17 deletions opendbc/car/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,28 +375,43 @@ def update(self, CC: structs.CarControl, CS: CarStateBase, now_nanos: int) -> tu

# interface-specific helpers

@cache
def _get_brand_names() -> list[str]:
return [folder.split('/')[-1] for folder in sorted(x[0] for x in os.walk(BASEDIR))]


@cache
def _get_brand_values_for_attr(brand_name: str, attr: str) -> Any | None:
attr_file = INTERFACE_ATTR_FILE.get(attr, "values")
try:
return __import__(f'opendbc.car.{brand_name}.{attr_file}', fromlist=[attr])
except (ImportError, OSError):
pass
return None
Comment on lines +383 to +390
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could import each values file once instead of several per brand, further reducing the import calls.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in, look up the attr_file and then cache based on that rather than the raw attr? Because if the attr_file is different, then I assume the imports aren't overlapping, right?



def get_interface_attr(attr: str, combine_brands: bool = False, ignore_none: bool = False) -> dict[str | StrEnum, Any]:
# read all the folders in opendbc/car and return a dict where:
# - keys are all the car models or brand names
# - values are attr values from all car folders
result = {}
for car_folder in sorted([x[0] for x in os.walk(BASEDIR)]):
try:
brand_name = car_folder.split('/')[-1]
brand_values = __import__(f'opendbc.car.{brand_name}.{INTERFACE_ATTR_FILE.get(attr, "values")}', fromlist=[attr])
if hasattr(brand_values, attr) or not ignore_none:
attr_data = getattr(brand_values, attr, None)
else:
continue

if combine_brands:
if isinstance(attr_data, dict):
for f, v in attr_data.items():
result[f] = v
else:
result[brand_name] = attr_data
except (ImportError, OSError):
pass

for brand_name in _get_brand_names():
brand_values = _get_brand_values_for_attr(brand_name, attr)
if brand_values is None:
# This was an ImportError or OSError, so we skip it
continue
if hasattr(brand_values, attr) or not ignore_none:
attr_data = getattr(brand_values, attr, None)
else:
continue

if combine_brands:
if isinstance(attr_data, dict):
for f, v in attr_data.items():
result[f] = v
else:
result[brand_name] = attr_data

return result

Expand Down
Loading