From 53d7cc01cf927ad9cd6386cbbd1b5c878b51dac8 Mon Sep 17 00:00:00 2001 From: ittacco Date: Sun, 15 Oct 2017 09:16:42 +0200 Subject: [PATCH] Optimize EEPROM::put (#2487) * EEPROM Library: Improved put function, compare data and only in case are different set _dirty flag, copy the data * It will grant that the _dirty flag is reset only at EEPROM.commit() and no changes are lost --- libraries/EEPROM/EEPROM.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libraries/EEPROM/EEPROM.h b/libraries/EEPROM/EEPROM.h index 09e582be7..3a26d02e6 100644 --- a/libraries/EEPROM/EEPROM.h +++ b/libraries/EEPROM/EEPROM.h @@ -52,9 +52,11 @@ public: const T &put(int address, const T &t) { if (address < 0 || address + sizeof(T) > _size) return t; + if (memcmp(_data + address, (const uint8_t*)&t, sizeof(T)) != 0) { + _dirty = true; + memcpy(_data + address, (const uint8_t*)&t, sizeof(T)); + } - memcpy(_data + address, (const uint8_t*) &t, sizeof(T)); - _dirty = true; return t; }