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

car/interface: retrieve car brand list from car.values.BRANDS #32590

Closed
wants to merge 3 commits into from

Conversation

deanlee
Copy link
Contributor

@deanlee deanlee commented Jun 2, 2024

Cache the list of car brands as CAR_BRANDS, improving performance by eliminating redundant directory scans on subsequent function calls.

@github-actions github-actions bot added the car vehicle-specific label Jun 2, 2024
Copy link
Contributor

github-actions bot commented Jun 2, 2024

Thanks for contributing to openpilot! In order for us to review your PR as quickly as possible, check the following:

  • Convert your PR to a draft unless it's ready to review
  • Read the contributing docs
  • Before marking as "ready for review", ensure:
    • the goal is clearly stated in the description
    • all the tests are passing
    • the change is something we merge
    • include a route or your device' dongle ID if relevant

@@ -33,6 +33,8 @@
TORQUE_OVERRIDE_PATH = os.path.join(BASEDIR, 'selfdrive/car/torque_data/override.toml')
TORQUE_SUBSTITUTE_PATH = os.path.join(BASEDIR, 'selfdrive/car/torque_data/substitute.toml')

CAR_BRANDS = sorted(entry.name for entry in os.scandir(BASEDIR + '/selfdrive/car') if entry.is_dir())
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we not use car.values.BRANDS?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not quit sure how to correctly retrieve the brand name from car.values.BRANDS. Would it be something like this?

import inspect
...
for brand in BRANDS:
    try:
      brand_name = inspect.getmodule(brand).__name__.split('.')[-2]
      brand_values = __import__(f'openpilot.selfdrive.car.{brand_name}.{INTERFACE_ATTR_FILE.get(attr, "values")}', fromlist=[attr])
      ...

but the inspect.getmodule is expensive, slower compared to simple I/O operations.

Copy link
Contributor

Choose a reason for hiding this comment

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

How about __module__?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahhhh, got it!

@deanlee deanlee changed the title car/interface: cache car brands list car/interface: retrieve car brand list from car.values.BRANDS Jun 6, 2024
@adeebshihadeh
Copy link
Contributor

I think we just want to get rid of the dynamic importing. Want to open a PR for that?

@deanlee deanlee deleted the cache_car_brands branch September 13, 2024 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
car vehicle-specific
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants