mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-17 01:23:33 +08:00
MavlinkOrbSubscription::update: improve performance & fix corner case
- reorders operations, such that the most expensive one (orb_copy) is done only when really needed. - corner case: when the topic was not advertised yet, orb_stat() would fail and then update() was called, which succeeds for the first advertisement. In that case the timestamp was incorrectly set to 0 and true was returned. The next call would again return true, because the timestamp was updated, but the topic data was still the same. Reduces CPU load by ~2% on a Pixracer.
This commit is contained in:
@@ -80,6 +80,10 @@ MavlinkOrbSubscription::update(uint64_t *time, void *data)
|
||||
// TODO this is NOT atomic operation, we can get data newer than time
|
||||
// if topic was published between orb_stat and orb_copy calls.
|
||||
|
||||
if (!is_published()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint64_t time_topic;
|
||||
|
||||
if (orb_stat(_fd, &time_topic)) {
|
||||
@@ -87,15 +91,11 @@ MavlinkOrbSubscription::update(uint64_t *time, void *data)
|
||||
time_topic = 0;
|
||||
}
|
||||
|
||||
if (update(data)) {
|
||||
/* data copied successfully */
|
||||
|
||||
if (time_topic == 0 || (time_topic != *time)) {
|
||||
if (time_topic == 0 || (time_topic != *time)) {
|
||||
if (orb_copy(_topic, _fd, data) == PX4_OK) {
|
||||
/* data copied successfully */
|
||||
*time = time_topic;
|
||||
return true;
|
||||
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user