From ccebe613e23b2452e245f0b84927adf8ec7a5479 Mon Sep 17 00:00:00 2001 From: David Woodhouse Date: Thu, 11 Dec 2025 23:35:54 +0900 Subject: [PATCH] Optimize buildinfo RAM usage on 32-bit platforms Use direct symbol access on 32-bit platforms to avoid 8 bytes of RAM overhead. Keep indirection workaround only on 64-bit platforms where PC-relative relocations cause linking issues. --- esphome/core/buildinfo.cpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/esphome/core/buildinfo.cpp b/esphome/core/buildinfo.cpp index 58129c43b5..dfeb073578 100644 --- a/esphome/core/buildinfo.cpp +++ b/esphome/core/buildinfo.cpp @@ -21,16 +21,21 @@ extern const char ESPHOME_BUILD_TIME[]; namespace esphome { namespace buildinfo { -// Reference the linker symbols as uintptr_t from the *data* section to -// avoid issues with pc-relative relocations on 64-bit platforms. And -// don't let the compiler know they're const or it'll optimise away the -// whole thing and emit a relocation to the ESPHOME_XXX symbols above -// directly, which defaults the whole point! -// +#if __SIZEOF_POINTER__ > 4 +// On 64-bit platforms, reference the linker symbols as uintptr_t from the *data* section to +// avoid issues with pc-relative relocations. Don't let the compiler know they're const or +// it'll optimise away the whole thing and emit a relocation to the ESPHOME_XXX symbols +// directly, which defeats the whole point! // NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables) -static uintptr_t config_hash = (uintptr_t) &ESPHOME_CONFIG_HASH; -static uintptr_t build_time = (uintptr_t) &ESPHOME_BUILD_TIME; +static uintptr_t config_hash_ptr = (uintptr_t) &ESPHOME_CONFIG_HASH; +static uintptr_t build_time_ptr = (uintptr_t) &ESPHOME_BUILD_TIME; // NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables) +#define config_hash config_hash_ptr +#define build_time build_time_ptr +#else +#define config_hash ((uintptr_t) &ESPHOME_CONFIG_HASH) +#define build_time ((uintptr_t) &ESPHOME_BUILD_TIME) +#endif const char *get_config_hash() { static char hash_str[9];