From e2360a9b35f0bc834551695ea95f85576120c042 Mon Sep 17 00:00:00 2001 From: Bernhard Kirchen Date: Sat, 6 Jan 2024 12:33:22 +0100 Subject: [PATCH] avoid too frequent SmartShunt data copies 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 c532db6c2..ffb91ee5b 100644 --- a/include/VictronSmartShunt.h +++ b/include/VictronSmartShunt.h @@ -11,6 +11,7 @@ class VictronSmartShunt : public BatteryProvider { 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 5524157f8..7b6da145a 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(); }