From 121bdc4af8944739565d0e0aec513238fb15be47 Mon Sep 17 00:00:00 2001 From: mvglasow Date: Sun, 6 Jul 2025 20:49:55 +0300 Subject: [PATCH] [traffic] Do not announce traffic updates if nothing has changed Signed-off-by: mvglasow --- map/traffic_manager.cpp | 22 +++++++++++++++++++--- map/traffic_manager.hpp | 4 +++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/map/traffic_manager.cpp b/map/traffic_manager.cpp index 121fa0b17..796e30cd4 100644 --- a/map/traffic_manager.cpp +++ b/map/traffic_manager.cpp @@ -558,19 +558,24 @@ void TrafficManager::PurgeExpiredMessages() OnTrafficDataUpdate(); } -void TrafficManager::PurgeExpiredMessagesImpl() +bool TrafficManager::PurgeExpiredMessagesImpl() { std::lock_guard lock(m_mutex); + bool result = false; LOG(LINFO, ("before:", m_messageCache.size(), "message(s)")); traffxml::IsoTime now = traffxml::IsoTime::Now(); for (auto it = m_messageCache.begin(); it != m_messageCache.end(); ) { if (it->second.IsExpired(now)) + { it = m_messageCache.erase(it); + result = true; + } else ++it; } LOG(LINFO, ("after:", m_messageCache.size(), "message(s)")); + return result; } void TrafficManager::ConsolidateFeedQueue() @@ -692,12 +697,21 @@ void TrafficManager::ThreadRoutine() if (!IsEnabled()) continue; + /* + * Whether to call OnTrafficDataUpdate() at the end of the current round. + * The logic may fail to catch cases in which the first message in queue replaces another + * message without changing coloring. This would usually occur in a larger feed, where other + * messages would likely require an announcement, making this a minor issue. A single round + * (after a timeout) with no messages expired and an empty queue would not trigger an update. + */ + bool hasUpdates = false; + if (!IsTestMode()) { if (steady_clock::now() - lastPurged >= kPurgeInterval) { lastPurged == steady_clock::now(); - PurgeExpiredMessagesImpl(); + hasUpdates |= PurgeExpiredMessagesImpl(); } LOG(LINFO, ("active MWMs changed:", m_activeMwmsChanged, ", poll needed:", m_isPollNeeded)); @@ -720,6 +734,7 @@ void TrafficManager::ThreadRoutine() // consolidate feed queue (remove older messages in favor of newer ones) ConsolidateFeedQueue(); + hasUpdates |= !m_feedQueue.empty(); // decode one message and add it to the cache DecodeFirstMessage(); @@ -727,7 +742,8 @@ void TrafficManager::ThreadRoutine() // set new coloring for MWMs // `m_mutex` is obtained inside the method, no need to do it here // TODO drop the argument, use class member inside method - OnTrafficDataUpdate(); + if (hasUpdates) + OnTrafficDataUpdate(); // TODO no longer needed #ifdef traffic_dead_code diff --git a/map/traffic_manager.hpp b/map/traffic_manager.hpp index 50256815b..2118c2fb2 100644 --- a/map/traffic_manager.hpp +++ b/map/traffic_manager.hpp @@ -399,8 +399,10 @@ private: * This is the internal conterpart of `PurgeExpiredMessages()`. It is safe to call from any * thread. Unlike `PurgeExpiredMessages()`, it does not wake the worker thread, making it suitable * for use on the worker thread. + * + * @return true if messages were purged, false if not */ - void PurgeExpiredMessagesImpl(); + bool PurgeExpiredMessagesImpl(); /** * @brief Consolidates the feed queue.