Browse Source

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.
sbg
Beat Küng 7 years ago committed by Lorenz Meier
parent
commit
e1a7472738
  1. 14
      src/modules/mavlink/mavlink_orb_subscription.cpp

14
src/modules/mavlink/mavlink_orb_subscription.cpp

@ -80,6 +80,10 @@ MavlinkOrbSubscription::update(uint64_t *time, void *data) @@ -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) @@ -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;
}
}

Loading…
Cancel
Save