1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-23 20:23:50 +01:00

[wifi] Optimize WiFi scan results with in-place construction

This commit is contained in:
J. Nick Koston
2025-10-17 12:39:14 -10:00
parent b61cec8e77
commit ce1d10eff0
4 changed files with 13 additions and 13 deletions

View File

@@ -706,10 +706,10 @@ void WiFiComponent::wifi_scan_done_callback_(void *arg, STATUS status) {
this->scan_result_.init(count); this->scan_result_.init(count);
for (bss_info *it = head; it != nullptr; it = STAILQ_NEXT(it, next)) { for (bss_info *it = head; it != nullptr; it = STAILQ_NEXT(it, next)) {
WiFiScanResult res({it->bssid[0], it->bssid[1], it->bssid[2], it->bssid[3], it->bssid[4], it->bssid[5]}, this->scan_result_.emplace_back(
std::string(reinterpret_cast<char *>(it->ssid), it->ssid_len), it->channel, it->rssi, bssid_t{it->bssid[0], it->bssid[1], it->bssid[2], it->bssid[3], it->bssid[4], it->bssid[5]},
it->authmode != AUTH_OPEN, it->is_hidden != 0); std::string(reinterpret_cast<char *>(it->ssid), it->ssid_len), it->channel, it->rssi, it->authmode != AUTH_OPEN,
this->scan_result_.push_back(res); it->is_hidden != 0);
} }
this->scan_done_ = true; this->scan_done_ = true;
} }

View File

@@ -790,8 +790,8 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
bssid_t bssid; bssid_t bssid;
std::copy(record.bssid, record.bssid + 6, bssid.begin()); std::copy(record.bssid, record.bssid + 6, bssid.begin());
std::string ssid(reinterpret_cast<const char *>(record.ssid)); std::string ssid(reinterpret_cast<const char *>(record.ssid));
WiFiScanResult result(bssid, ssid, record.primary, record.rssi, record.authmode != WIFI_AUTH_OPEN, ssid.empty()); scan_result_.emplace_back(bssid, ssid, record.primary, record.rssi, record.authmode != WIFI_AUTH_OPEN,
scan_result_.push_back(result); ssid.empty());
} }
} else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_START) { } else if (data->event_base == WIFI_EVENT && data->event_id == WIFI_EVENT_AP_START) {

View File

@@ -419,9 +419,9 @@ void WiFiComponent::wifi_scan_done_callback_() {
uint8_t *bssid = WiFi.BSSID(i); uint8_t *bssid = WiFi.BSSID(i);
int32_t channel = WiFi.channel(i); int32_t channel = WiFi.channel(i);
WiFiScanResult scan({bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]}, std::string(ssid.c_str()), this->scan_result_.emplace_back(bssid_t{bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]},
channel, rssi, authmode != WIFI_AUTH_OPEN, ssid.length() == 0); std::string(ssid.c_str()), channel, rssi, authmode != WIFI_AUTH_OPEN,
this->scan_result_.push_back(scan); ssid.length() == 0);
} }
WiFi.scanDelete(); WiFi.scanDelete();
this->scan_done_ = true; this->scan_done_ = true;

View File

@@ -281,13 +281,13 @@ template<typename T> class FixedVector {
} }
} }
/// Emplace element without bounds checking - constructs in-place /// Emplace element without bounds checking - constructs in-place with arguments
/// Caller must ensure sufficient capacity was allocated via init() /// Caller must ensure sufficient capacity was allocated via init()
/// Returns reference to the newly constructed element /// Returns reference to the newly constructed element
/// NOTE: Caller MUST ensure size_ < capacity_ before calling /// NOTE: Caller MUST ensure size_ < capacity_ before calling
T &emplace_back() { template<typename... Args> T &emplace_back(Args &&...args) {
// Use placement new to default-construct the object in pre-allocated memory // Use placement new to construct the object in pre-allocated memory
new (&data_[size_]) T(); new (&data_[size_]) T(std::forward<Args>(args)...);
size_++; size_++;
return data_[size_ - 1]; return data_[size_ - 1];
} }