diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 470c11cbd..f68f95f83 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -1339,23 +1339,26 @@ bool NodeDB::saveNodeDatabaseToDisk() void NodeDB::sortMeshDB() { - std::sort(meshNodes->begin(), meshNodes->end(), [](const meshtastic_NodeInfoLite &a, const meshtastic_NodeInfoLite &b) { - if (a.num == myNodeInfo.my_node_num) { - return true; - } - if (b.num == myNodeInfo.my_node_num) { - return false; - } - bool aFav = a.is_favorite; - bool bFav = b.is_favorite; - if (aFav != bFav) - return aFav; - if (a.last_heard == 0 || a.last_heard == UINT32_MAX) - return false; - if (b.last_heard == 0 || b.last_heard == UINT32_MAX) - return true; - return a.last_heard > b.last_heard; - }); + if (!Throttle::isWithinTimespanMs(lastSort, 1000 * 5)) { + lastSort = millis(); + std::sort(meshNodes->begin(), meshNodes->end(), [](const meshtastic_NodeInfoLite &a, const meshtastic_NodeInfoLite &b) { + if (a.num == myNodeInfo.my_node_num) { + return true; + } + if (b.num == myNodeInfo.my_node_num) { + return false; + } + bool aFav = a.is_favorite; + bool bFav = b.is_favorite; + if (aFav != bFav) + return aFav; + if (a.last_heard == 0 || a.last_heard == UINT32_MAX) + return false; + if (b.last_heard == 0 || b.last_heard == UINT32_MAX) + return true; + return a.last_heard > b.last_heard; + }); + } } bool NodeDB::saveToDiskNoRetry(int saveWhat) diff --git a/src/mesh/NodeDB.h b/src/mesh/NodeDB.h index 4d4fb883d..b6e4d600b 100644 --- a/src/mesh/NodeDB.h +++ b/src/mesh/NodeDB.h @@ -282,6 +282,7 @@ class NodeDB bool duplicateWarned = false; uint32_t lastNodeDbSave = 0; // when we last saved our db to flash uint32_t lastBackupAttempt = 0; // when we last tried a backup automatically or manually + uint32_t lastSort = 0; // When last sorted the nodeDB /// Find a node in our DB, create an empty NodeInfoLite if missing meshtastic_NodeInfoLite *getOrCreateMeshNode(NodeNum n);