commaai / opendbc

democratize access to car decoder rings
MIT License
1.81k stars 1.07k forks source link

CANParser: Update CAN data handling functions to accept raw data and size parameters #1040

Open deanlee opened 2 months ago

deanlee commented 2 months ago

When parsing CAN messages, CanParser creates a memory copy for each message solely to satisfy the checksum calculation function's requirement, which expects a std::vector<uint8_t> as parameter. This approach is inefficient and slows down the parsing process.

std::vector data(dat.size(), 0); memcpy(data.data(), dat.begin(), dat.size());

This PR updates all functions responsible for handling CAN data to accept raw data pointers uint8_t* data and size parameters size_t size instead of using std::vector<uint8_t>. This adjustment aims to enhance performance and reduce memory overhead by avoiding unnecessary memory allocations and copies associated with vector usage.

sshane commented 1 month ago

How much does this improve performance? I don't see much of a difference on a comma 3X

deanlee commented 1 month ago

This change won't significantly impact the performance of the master branch, as the master spends lots of time on Cython code. However, it will enhance the performance of the C++ code, particularly after https://github.com/commaai/opendbc/pull/1046. Currently, there are two performance bottlenecks in the C++ code that can be addressed: memory-aligned copying and the use of vectors. This adjustment is expected to boost the performance of the C++ code, with even greater improvements expected on devices.