refactor: Dedupe wifi scan in-place without std::map (#2262)

## Summary

`std::map` heap-allocates each node. Avoid using it for wifi SSID
dedupe, and instead just dedupe in-place with the existing `networks`
vector. Removed dead `ipAddress` member in `WifiNetworkInfo` struct.

---

### AI Usage

Did you use AI tools to help write this code? _**PARTIALLY**_
This commit is contained in:
Zach Nelson
2026-06-07 08:27:20 -04:00
committed by GitHub
parent fad1a801a4
commit 3a1e9f3023
2 changed files with 13 additions and 20 deletions
@@ -6,8 +6,6 @@
#include <Logging.h>
#include <WiFi.h>
#include <map>
#include "CrossPointSettings.h"
#include "MappedInputManager.h"
#include "WifiCredentialStore.h"
@@ -118,39 +116,35 @@ void WifiSelectionActivity::processWifiScanResults() {
return;
}
// Scan complete, process results
// Use a map to deduplicate networks by SSID, keeping the strongest signal
std::map<std::string, WifiNetworkInfo> uniqueNetworks;
// Scan complete, process results — deduplicate in-place, keeping strongest signal
networks.clear();
networks.reserve(scanResult);
for (int i = 0; i < scanResult; i++) {
std::string ssid = WiFi.SSID(i).c_str();
char ssid[33];
strlcpy(ssid, WiFi.SSID(i).c_str(), sizeof(ssid));
const int32_t rssi = WiFi.RSSI(i);
// Skip hidden networks (empty SSID)
if (ssid.empty()) {
if (ssid[0] == '\0') {
continue;
}
// Check if we've already seen this SSID
auto it = uniqueNetworks.find(ssid);
if (it == uniqueNetworks.end() || rssi > it->second.rssi) {
// New network or stronger signal than existing entry
auto it =
std::find_if(networks.begin(), networks.end(), [&ssid](const WifiNetworkInfo& n) { return n.ssid == ssid; });
if (it == networks.end()) {
WifiNetworkInfo network;
network.ssid = ssid;
network.rssi = rssi;
network.isEncrypted = (WiFi.encryptionType(i) != WIFI_AUTH_OPEN);
network.hasSavedPassword = WIFI_STORE.hasSavedCredential(network.ssid);
uniqueNetworks[ssid] = network;
networks.push_back(std::move(network));
} else if (rssi > it->rssi) {
it->rssi = rssi;
it->isEncrypted = (WiFi.encryptionType(i) != WIFI_AUTH_OPEN);
}
}
// Convert map to vector
networks.clear();
for (const auto& pair : uniqueNetworks) {
// cppcheck-suppress useStlAlgorithm
networks.push_back(pair.second);
}
// Sort: saved-password networks first, then by signal strength (strongest first)
std::sort(networks.begin(), networks.end(), [](const WifiNetworkInfo& a, const WifiNetworkInfo& b) {
if (a.hasSavedPassword != b.hasSavedPassword) {
@@ -18,7 +18,6 @@ struct WifiNetworkInfo {
int32_t rssi;
bool isEncrypted;
bool hasSavedPassword; // Whether we have saved credentials for this network
std::string ipAddress; // Populated after connection for display
};
// WiFi selection states