-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
CANParser: optimize data handling by eliminating data copy after Update() #1439
base: master
Are you sure you want to change the base?
Conversation
df2318a
to
d43298f
Compare
d43298f
to
4b72635
Compare
After this, only one bottleneck remains: the conversion related to can_capnp_to_can_list_cpp. By using a NumPy array, the existing multi-step conversion:
can be streamlined into a single step:
This would allow CanParser updates to be executed almost entirely in C++ code, with Cython serving as a simple wrapper for the C++ function, as shown below:
In my local draft, this optimization has the potential to double performance, reducing the processing time from 0.0443 ms per CAN packet to 0.0202 ms per CAN packet. It’s important to note that using a NumPy array is just one option. Other alternatives may also be worth exploring. |
opendbc/can/parser_pyx.pyx
Outdated
class ValueDict(ReadonlyDict): | ||
def __getitem__(self, key): | ||
return self.state.value(key) | ||
|
||
|
||
class NanosDict(ReadonlyDict): | ||
def __getitem__(self, key): | ||
return self.state.ts_nanos(key) | ||
|
||
|
||
class AllValueDict(ReadonlyDict): | ||
def __getitem__(self, key): | ||
return self.state.all_values(key) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any way we can get by with ReadonlyDict
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about ebf4696
This brings down Bronco card CPU usage from 28% to 21%! |
by the way, I've also eliminate the multiple conversions from capnp can data to vector[CanFrame] while call [update]: submitted: #1452 |
0c5341b
to
68313dd
Compare
68313dd
to
50a660f
Compare
This pull request removes unnecessary data copying from MessageState to dictionaries following update() calls. By encapsulating MessageState within a Cython object and implementing a read-only dictionary for direct access to the signal data, we eliminate the need to clear
vl_all
and copy updated data tovl
,vl_all
andts_nanos
. This optimization yields approximately a 1x performance improvement on the 3X device.With this refactor, the impact on dictionary read performance is negligible, as the read-only dictionary is a minimal wrapper around the C++ MessageState, allowing direct access to data from the C++
unordered_map
without additional overhead.Passes tests on the device and OpenPilot branch #33926.
Key Changes:
update()
.__getitem__
to retrieve data from C++ MessageState, ensuring read-only access.test_checksum.py
: Removed deep copy requirements as the new implementation is read-only.Below is the benchmark comparison on the 3X device:
Benchmark (PR):
Benchmark (Master):
This PR is an improved version of #1046