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

enhance CANPacker with efficient MessageData struct #1440

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

deanlee
Copy link
Contributor

@deanlee deanlee commented Nov 4, 2024

This PR introduces a more efficient MessageData struct in the CANPacker class to significantly reduce map lookup overhead during CAN message packing. The original code performed excessive lookups, including inefficient searches using std::pair<address, string> keys. For example, the following code snippet includes five separate map lookups:

auto sig_it_counter = signal_lookup.find(std::make_pair(address, "COUNTER"));
 if (!counter_set && sig_it_counter != signal_lookup.end()) {
   const auto& sig = sig_it_counter->second;

   if (counters.find(address) == counters.end()) {
     counters[address] = 0;
   }
   set_value(ret, sig, counters[address]);
   counters[address] = (counters[address] + 1) % (1 << sig.size);
 }

By optimizing the way signals, counters, and checksums are accessed, the new structure improves performance and simplifies the code.

Key improvements:

  • Introduced a MessageData struct for better organization and efficient management of signals, counters, and checksums.
  • Kept pointers to signals in MessageData instead of copies, as these pointers are always valid in the dbc() instance—avoiding unnecessary data duplication.
  • Minimized redundant lookups to significantly speed up the signal packing process.
  • Cached counter and checksum signals directly in the MessageData struct to simplify counter and checksum handling.
  • Optimized the pack method for faster processing of multiple signals, reducing overhead.

These enhancements lead to faster CAN message packing, particularly for larger datasets and frequent updates.

@github-actions github-actions bot added the can related to CAN tools, aka opendbc/can/ label Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can related to CAN tools, aka opendbc/can/
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant