Throttle sorting just a touch

This commit is contained in:
Jonathan Bennett 2025-06-25 19:43:36 -05:00
parent 18098fb1cb
commit e1b1e35a27
2 changed files with 21 additions and 17 deletions

View File

@ -1339,23 +1339,26 @@ bool NodeDB::saveNodeDatabaseToDisk()
void NodeDB::sortMeshDB() void NodeDB::sortMeshDB()
{ {
std::sort(meshNodes->begin(), meshNodes->end(), [](const meshtastic_NodeInfoLite &a, const meshtastic_NodeInfoLite &b) { if (!Throttle::isWithinTimespanMs(lastSort, 1000 * 5)) {
if (a.num == myNodeInfo.my_node_num) { lastSort = millis();
return true; std::sort(meshNodes->begin(), meshNodes->end(), [](const meshtastic_NodeInfoLite &a, const meshtastic_NodeInfoLite &b) {
} if (a.num == myNodeInfo.my_node_num) {
if (b.num == myNodeInfo.my_node_num) { return true;
return false; }
} if (b.num == myNodeInfo.my_node_num) {
bool aFav = a.is_favorite; return false;
bool bFav = b.is_favorite; }
if (aFav != bFav) bool aFav = a.is_favorite;
return aFav; bool bFav = b.is_favorite;
if (a.last_heard == 0 || a.last_heard == UINT32_MAX) if (aFav != bFav)
return false; return aFav;
if (b.last_heard == 0 || b.last_heard == UINT32_MAX) if (a.last_heard == 0 || a.last_heard == UINT32_MAX)
return true; return false;
return a.last_heard > b.last_heard; if (b.last_heard == 0 || b.last_heard == UINT32_MAX)
}); return true;
return a.last_heard > b.last_heard;
});
}
} }
bool NodeDB::saveToDiskNoRetry(int saveWhat) bool NodeDB::saveToDiskNoRetry(int saveWhat)

View File

@ -282,6 +282,7 @@ class NodeDB
bool duplicateWarned = false; bool duplicateWarned = false;
uint32_t lastNodeDbSave = 0; // when we last saved our db to flash 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 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 /// Find a node in our DB, create an empty NodeInfoLite if missing
meshtastic_NodeInfoLite *getOrCreateMeshNode(NodeNum n); meshtastic_NodeInfoLite *getOrCreateMeshNode(NodeNum n);