From e1a74727389a9e3abf1021128960476fe72eea71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Thu, 12 Jul 2018 20:51:04 +0200 Subject: [PATCH] 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. --- src/modules/mavlink/mavlink_orb_subscription.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/modules/mavlink/mavlink_orb_subscription.cpp b/src/modules/mavlink/mavlink_orb_subscription.cpp index 33510e3d2a..7b13f6d1ba 100644 --- a/src/modules/mavlink/mavlink_orb_subscription.cpp +++ b/src/modules/mavlink/mavlink_orb_subscription.cpp @@ -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; } }