From a012d81427a782aeab848c237963381e400b9410 Mon Sep 17 00:00:00 2001 From: Bernhard Kirchen Date: Sun, 7 Jan 2024 18:30:02 +0100 Subject: [PATCH] avoid too frequent SmartShunt data copies (#596) currently the whole SmartShunt data structure is copied to the BatteryStats instance in every loop, even though the data cannot possibly have changed. this is quite an expensive task to do in every loop. this change tracks the last update timestamp and only does the copy operation if an actual updated data structure was received from the smart shunt. --- include/VictronSmartShunt.h | 1 + src/VictronSmartShunt.cpp | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/include/VictronSmartShunt.h b/include/VictronSmartShunt.h index c532db6c..ffb91ee5 100644 --- a/include/VictronSmartShunt.h +++ b/include/VictronSmartShunt.h @@ -11,6 +11,7 @@ public: std::shared_ptr getStats() const final { return _stats; } private: + uint32_t _lastUpdate = 0; std::shared_ptr _stats = std::make_shared(); }; diff --git a/src/VictronSmartShunt.cpp b/src/VictronSmartShunt.cpp index 5524157f..7b6da145 100644 --- a/src/VictronSmartShunt.cpp +++ b/src/VictronSmartShunt.cpp @@ -28,5 +28,9 @@ bool VictronSmartShunt::init(bool verboseLogging) void VictronSmartShunt::loop() { VeDirectShunt.loop(); + + if (VeDirectShunt.getLastUpdate() <= _lastUpdate) { return; } + _stats->updateFrom(VeDirectShunt.veFrame); + _lastUpdate = VeDirectShunt.getLastUpdate(); }