Skip to content

Commit

Permalink
MavlinkOrbSubscription::update: improve performance & fix corner case
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
bkueng authored and LorenzMeier committed Jul 12, 2018
1 parent 060b130 commit e1a7472
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/modules/mavlink/mavlink_orb_subscription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,22 @@ 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)) {
/* error getting last topic publication time */
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;
}
}

Expand Down

0 comments on commit e1a7472

Please sign in to comment.