1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-18 09:43:47 +01:00

[network] Optimize get_use_address() to return const reference instead of copy

This commit is contained in:
J. Nick Koston
2025-10-13 14:32:17 -10:00
parent fe07c34246
commit 10724f411b
6 changed files with 14 additions and 21 deletions

View File

@@ -689,14 +689,9 @@ void EthernetComponent::add_phy_register(PHYRegister register_value) { this->phy
void EthernetComponent::set_type(EthernetType type) { this->type_ = type; } void EthernetComponent::set_type(EthernetType type) { this->type_ = type; }
void EthernetComponent::set_manual_ip(const ManualIP &manual_ip) { this->manual_ip_ = manual_ip; } void EthernetComponent::set_manual_ip(const ManualIP &manual_ip) { this->manual_ip_ = manual_ip; }
std::string EthernetComponent::get_use_address() const { // set_use_address() is guaranteed to be called during component setup by Python code generation,
if (this->use_address_.empty()) { // so use_address_ will always be valid when get_use_address() is called - no fallback needed.
// ".local" suffix length for mDNS hostnames const std::string &EthernetComponent::get_use_address() const { return this->use_address_; }
constexpr size_t mdns_local_suffix_len = 5;
return make_name_with_suffix(App.get_name(), '.', "local", mdns_local_suffix_len);
}
return this->use_address_;
}
void EthernetComponent::set_use_address(const std::string &use_address) { this->use_address_ = use_address; } void EthernetComponent::set_use_address(const std::string &use_address) { this->use_address_ = use_address; }

View File

@@ -88,7 +88,7 @@ class EthernetComponent : public Component {
network::IPAddresses get_ip_addresses(); network::IPAddresses get_ip_addresses();
network::IPAddress get_dns_address(uint8_t num); network::IPAddress get_dns_address(uint8_t num);
std::string get_use_address() const; const std::string &get_use_address() const;
void set_use_address(const std::string &use_address); void set_use_address(const std::string &use_address);
void get_eth_mac_address_raw(uint8_t *mac); void get_eth_mac_address_raw(uint8_t *mac);
std::string get_eth_mac_address_pretty(); std::string get_eth_mac_address_pretty();

View File

@@ -85,7 +85,7 @@ network::IPAddresses get_ip_addresses() {
return {}; return {};
} }
std::string get_use_address() { const std::string &get_use_address() {
#ifdef USE_ETHERNET #ifdef USE_ETHERNET
if (ethernet::global_eth_component != nullptr) if (ethernet::global_eth_component != nullptr)
return ethernet::global_eth_component->get_use_address(); return ethernet::global_eth_component->get_use_address();
@@ -100,7 +100,10 @@ std::string get_use_address() {
if (wifi::global_wifi_component != nullptr) if (wifi::global_wifi_component != nullptr)
return wifi::global_wifi_component->get_use_address(); return wifi::global_wifi_component->get_use_address();
#endif #endif
return ""; #if !defined(USE_ETHERNET) && !defined(USE_MODEM) && !defined(USE_WIFI)
static const std::string empty;
return empty;
#endif
} }
} // namespace network } // namespace network

View File

@@ -12,7 +12,7 @@ bool is_connected();
/// Return whether the network is disabled (only wifi for now) /// Return whether the network is disabled (only wifi for now)
bool is_disabled(); bool is_disabled();
/// Get the active network hostname /// Get the active network hostname
std::string get_use_address(); const std::string &get_use_address();
IPAddresses get_ip_addresses(); IPAddresses get_ip_addresses();
} // namespace network } // namespace network

View File

@@ -265,14 +265,9 @@ network::IPAddress WiFiComponent::get_dns_address(int num) {
return this->wifi_dns_ip_(num); return this->wifi_dns_ip_(num);
return {}; return {};
} }
std::string WiFiComponent::get_use_address() const { // set_use_address() is guaranteed to be called during component setup by Python code generation,
if (this->use_address_.empty()) { // so use_address_ will always be valid when get_use_address() is called - no fallback needed.
// ".local" suffix length for mDNS hostnames const std::string &WiFiComponent::get_use_address() const { return this->use_address_; }
constexpr size_t mdns_local_suffix_len = 5;
return make_name_with_suffix(App.get_name(), '.', "local", mdns_local_suffix_len);
}
return this->use_address_;
}
void WiFiComponent::set_use_address(const std::string &use_address) { this->use_address_ = use_address; } void WiFiComponent::set_use_address(const std::string &use_address) { this->use_address_ = use_address; }
#ifdef USE_WIFI_AP #ifdef USE_WIFI_AP

View File

@@ -275,7 +275,7 @@ class WiFiComponent : public Component {
network::IPAddress get_dns_address(int num); network::IPAddress get_dns_address(int num);
network::IPAddresses get_ip_addresses(); network::IPAddresses get_ip_addresses();
std::string get_use_address() const; const std::string &get_use_address() const;
void set_use_address(const std::string &use_address); void set_use_address(const std::string &use_address);
const std::vector<WiFiScanResult> &get_scan_result() const { return scan_result_; } const std::vector<WiFiScanResult> &get_scan_result() const { return scan_result_; }