[mdns] Reduce RAM usage by eliminating MAC address heap allocation (#12073)

This commit is contained in:
J. Nick Koston
2025-12-09 09:33:23 -05:00
committed by GitHub
parent 861ed8dd41
commit f9aa48295c
11 changed files with 86 additions and 55 deletions
+5 -5
View File
@@ -184,10 +184,8 @@ async def to_code(config):
# Calculate compile-time dynamic TXT value count
# Dynamic values are those that cannot be stored in flash at compile time
# Note: MAC address is now stored in a fixed char[13] buffer, not dynamic storage
dynamic_txt_count = 0
if "api" in CORE.config:
# Always: get_mac_address()
dynamic_txt_count += 1
# User-provided templatable TXT values (only lambdas, not static strings)
dynamic_txt_count += sum(
1
@@ -196,8 +194,10 @@ async def to_code(config):
if cg.is_template(txt_value)
)
# Ensure at least 1 to avoid zero-size array
cg.add_define("MDNS_DYNAMIC_TXT_COUNT", max(1, dynamic_txt_count))
# Only add define if we actually need dynamic storage
if dynamic_txt_count > 0:
cg.add_define("USE_MDNS_DYNAMIC_TXT")
cg.add_define("MDNS_DYNAMIC_TXT_COUNT", dynamic_txt_count)
# Enable storage if verbose logging is enabled (for dump_config)
if get_logger_level() in ("VERBOSE", "VERY_VERBOSE"):
+4 -2
View File
@@ -35,7 +35,7 @@ MDNS_STATIC_CONST_CHAR(SERVICE_TCP, "_tcp");
// Wrap build-time defines into flash storage
MDNS_STATIC_CONST_CHAR(VALUE_VERSION, ESPHOME_VERSION);
void MDNSComponent::compile_records_(StaticVector<MDNSService, MDNS_SERVICE_COUNT> &services) {
void MDNSComponent::compile_records_(StaticVector<MDNSService, MDNS_SERVICE_COUNT> &services, char *mac_address_buf) {
// IMPORTANT: The #ifdef blocks below must match COMPONENTS_WITH_MDNS_SERVICES
// in mdns/__init__.py. If you add a new service here, update both locations.
@@ -86,7 +86,9 @@ void MDNSComponent::compile_records_(StaticVector<MDNSService, MDNS_SERVICE_COUN
txt_records.push_back({MDNS_STR(TXT_FRIENDLY_NAME), MDNS_STR(friendly_name.c_str())});
}
txt_records.push_back({MDNS_STR(TXT_VERSION), MDNS_STR(VALUE_VERSION)});
txt_records.push_back({MDNS_STR(TXT_MAC), MDNS_STR(this->add_dynamic_txt_value(get_mac_address()))});
// MAC address: passed from caller (either member buffer or stack buffer depending on USE_MDNS_STORE_SERVICES)
txt_records.push_back({MDNS_STR(TXT_MAC), MDNS_STR(mac_address_buf)});
#ifdef USE_ESP8266
MDNS_STATIC_CONST_CHAR(PLATFORM_ESP8266, "ESP8266");
+39 -3
View File
@@ -60,22 +60,58 @@ class MDNSComponent : public Component {
void on_shutdown() override;
#ifdef USE_MDNS_DYNAMIC_TXT
/// Add a dynamic TXT value and return pointer to it for use in MDNSTXTRecord
const char *add_dynamic_txt_value(const std::string &value) {
this->dynamic_txt_values_.push_back(value);
return this->dynamic_txt_values_[this->dynamic_txt_values_.size() - 1].c_str();
}
#endif
/// Storage for runtime-generated TXT values (MAC address, user lambdas)
protected:
/// Helper to set up services and MAC buffers, then call platform-specific registration
using PlatformRegisterFn = void (*)(MDNSComponent *, StaticVector<MDNSService, MDNS_SERVICE_COUNT> &);
void setup_buffers_and_register_(PlatformRegisterFn platform_register) {
#ifdef USE_MDNS_STORE_SERVICES
auto &services = this->services_;
#else
StaticVector<MDNSService, MDNS_SERVICE_COUNT> services_storage;
auto &services = services_storage;
#endif
#ifdef USE_API
#ifdef USE_MDNS_STORE_SERVICES
get_mac_address_into_buffer(this->mac_address_);
char *mac_ptr = this->mac_address_;
#else
char mac_address[MAC_ADDRESS_BUFFER_SIZE];
get_mac_address_into_buffer(mac_address);
char *mac_ptr = mac_address;
#endif
#else
char *mac_ptr = nullptr;
#endif
this->compile_records_(services, mac_ptr);
platform_register(this, services);
}
#ifdef USE_MDNS_DYNAMIC_TXT
/// Storage for runtime-generated TXT values from user lambdas
/// Pre-sized at compile time via MDNS_DYNAMIC_TXT_COUNT to avoid heap allocations.
/// Static/compile-time values (version, board, etc.) are stored directly in flash and don't use this.
StaticVector<std::string, MDNS_DYNAMIC_TXT_COUNT> dynamic_txt_values_;
#endif
protected:
#if defined(USE_API) && defined(USE_MDNS_STORE_SERVICES)
/// Fixed buffer for MAC address (only needed when services are stored)
char mac_address_[MAC_ADDRESS_BUFFER_SIZE];
#endif
#ifdef USE_MDNS_STORE_SERVICES
StaticVector<MDNSService, MDNS_SERVICE_COUNT> services_{};
#endif
void compile_records_(StaticVector<MDNSService, MDNS_SERVICE_COUNT> &services);
void compile_records_(StaticVector<MDNSService, MDNS_SERVICE_COUNT> &services, char *mac_address_buf);
};
} // namespace esphome::mdns
+4 -10
View File
@@ -11,19 +11,11 @@ namespace esphome::mdns {
static const char *const TAG = "mdns";
void MDNSComponent::setup() {
#ifdef USE_MDNS_STORE_SERVICES
this->compile_records_(this->services_);
const auto &services = this->services_;
#else
StaticVector<MDNSService, MDNS_SERVICE_COUNT> services;
this->compile_records_(services);
#endif
static void register_esp32(MDNSComponent *comp, StaticVector<MDNSService, MDNS_SERVICE_COUNT> &services) {
esp_err_t err = mdns_init();
if (err != ESP_OK) {
ESP_LOGW(TAG, "Init failed: %s", esp_err_to_name(err));
this->mark_failed();
comp->mark_failed();
return;
}
@@ -50,6 +42,8 @@ void MDNSComponent::setup() {
}
}
void MDNSComponent::setup() { this->setup_buffers_and_register_(register_esp32); }
void MDNSComponent::on_shutdown() {
mdns_free();
delay(40); // Allow the mdns packets announcing service removal to be sent
+3 -9
View File
@@ -11,15 +11,7 @@
namespace esphome::mdns {
void MDNSComponent::setup() {
#ifdef USE_MDNS_STORE_SERVICES
this->compile_records_(this->services_);
const auto &services = this->services_;
#else
StaticVector<MDNSService, MDNS_SERVICE_COUNT> services;
this->compile_records_(services);
#endif
static void register_esp8266(MDNSComponent *, StaticVector<MDNSService, MDNS_SERVICE_COUNT> &services) {
MDNS.begin(App.get_name().c_str());
for (const auto &service : services) {
@@ -44,6 +36,8 @@ void MDNSComponent::setup() {
}
}
void MDNSComponent::setup() { this->setup_buffers_and_register_(register_esp8266); }
void MDNSComponent::loop() { MDNS.update(); }
void MDNSComponent::on_shutdown() {
+9
View File
@@ -9,6 +9,15 @@
namespace esphome::mdns {
void MDNSComponent::setup() {
#ifdef USE_MDNS_STORE_SERVICES
#ifdef USE_API
get_mac_address_into_buffer(this->mac_address_);
char *mac_ptr = this->mac_address_;
#else
char *mac_ptr = nullptr;
#endif
this->compile_records_(this->services_, mac_ptr);
#endif
// Host platform doesn't have actual mDNS implementation
}
+3 -9
View File
@@ -11,15 +11,7 @@
namespace esphome::mdns {
void MDNSComponent::setup() {
#ifdef USE_MDNS_STORE_SERVICES
this->compile_records_(this->services_);
const auto &services = this->services_;
#else
StaticVector<MDNSService, MDNS_SERVICE_COUNT> services;
this->compile_records_(services);
#endif
static void register_libretiny(MDNSComponent *, StaticVector<MDNSService, MDNS_SERVICE_COUNT> &services) {
MDNS.begin(App.get_name().c_str());
for (const auto &service : services) {
@@ -43,6 +35,8 @@ void MDNSComponent::setup() {
}
}
void MDNSComponent::setup() { this->setup_buffers_and_register_(register_libretiny); }
void MDNSComponent::on_shutdown() {}
} // namespace esphome::mdns
+3 -9
View File
@@ -11,15 +11,7 @@
namespace esphome::mdns {
void MDNSComponent::setup() {
#ifdef USE_MDNS_STORE_SERVICES
this->compile_records_(this->services_);
const auto &services = this->services_;
#else
StaticVector<MDNSService, MDNS_SERVICE_COUNT> services;
this->compile_records_(services);
#endif
static void register_rp2040(MDNSComponent *, StaticVector<MDNSService, MDNS_SERVICE_COUNT> &services) {
MDNS.begin(App.get_name().c_str());
for (const auto &service : services) {
@@ -43,6 +35,8 @@ void MDNSComponent::setup() {
}
}
void MDNSComponent::setup() { this->setup_buffers_and_register_(register_rp2040); }
void MDNSComponent::loop() { MDNS.update(); }
void MDNSComponent::on_shutdown() {