diff --git a/esphome/analyze_memory/__init__.py b/esphome/analyze_memory/__init__.py index db16051b8a..15cadaf859 100644 --- a/esphome/analyze_memory/__init__.py +++ b/esphome/analyze_memory/__init__.py @@ -48,6 +48,12 @@ _READELF_SECTION_PATTERN = re.compile( r"\s*\[\s*\d+\]\s+([\.\w]+)\s+\w+\s+[\da-fA-F]+\s+[\da-fA-F]+\s+([\da-fA-F]+)" ) +# Component category prefixes +_COMPONENT_PREFIX_ESPHOME = "[esphome]" +_COMPONENT_PREFIX_EXTERNAL = "[external]" +_COMPONENT_CORE = f"{_COMPONENT_PREFIX_ESPHOME}core" +_COMPONENT_API = f"{_COMPONENT_PREFIX_ESPHOME}api" + @dataclass class MemorySection: @@ -222,7 +228,7 @@ class MemoryAnalyzer: self._uncategorized_symbols.append((symbol_name, demangled, size)) # Track ESPHome core symbols for detailed analysis - if component == "[esphome]core" and size > 0: + if component == _COMPONENT_CORE and size > 0: demangled = self._demangle_symbol(symbol_name) self._esphome_core_symbols.append((symbol_name, demangled, size)) @@ -246,7 +252,7 @@ class MemoryAnalyzer: for component_name in get_esphome_components(): patterns = get_component_class_patterns(component_name) if any(pattern in demangled for pattern in patterns): - return f"[esphome]{component_name}" + return f"{_COMPONENT_PREFIX_ESPHOME}{component_name}" # Check for ESPHome component namespaces match = ESPHOME_COMPONENT_PATTERN.search(demangled) @@ -257,17 +263,17 @@ class MemoryAnalyzer: # Check if this is an actual component in the components directory if component_name in get_esphome_components(): - return f"[esphome]{component_name}" + return f"{_COMPONENT_PREFIX_ESPHOME}{component_name}" # Check if this is a known external component from the config if component_name in self.external_components: - return f"[external]{component_name}" + return f"{_COMPONENT_PREFIX_EXTERNAL}{component_name}" # Everything else in esphome:: namespace is core - return "[esphome]core" + return _COMPONENT_CORE # Check for esphome core namespace (no component namespace) if "esphome::" in demangled: # If no component match found, it's core - return "[esphome]core" + return _COMPONENT_CORE # Check against symbol patterns for component, patterns in SYMBOL_PATTERNS.items(): @@ -460,8 +466,7 @@ class MemoryAnalyzer: Returns: Demangled name with suffix annotation """ - suffix_match = _GCC_OPTIMIZATION_SUFFIX_PATTERN.search(original) - if suffix_match: + if suffix_match := _GCC_OPTIMIZATION_SUFFIX_PATTERN.search(original): return f"{demangled} [{suffix_match.group(1)}]" return demangled diff --git a/esphome/analyze_memory/cli.py b/esphome/analyze_memory/cli.py index 5713eac94c..1695a00c19 100644 --- a/esphome/analyze_memory/cli.py +++ b/esphome/analyze_memory/cli.py @@ -3,7 +3,13 @@ from collections import defaultdict import sys -from . import MemoryAnalyzer +from . import ( + _COMPONENT_API, + _COMPONENT_CORE, + _COMPONENT_PREFIX_ESPHOME, + _COMPONENT_PREFIX_EXTERNAL, + MemoryAnalyzer, +) class MemoryAnalyzerCLI(MemoryAnalyzer): @@ -144,7 +150,9 @@ class MemoryAnalyzerCLI(MemoryAnalyzer): if self._esphome_core_symbols: lines.append("") lines.append("=" * self.TABLE_WIDTH) - lines.append("[esphome]core Detailed Analysis".center(self.TABLE_WIDTH)) + lines.append( + f"{_COMPONENT_CORE} Detailed Analysis".center(self.TABLE_WIDTH) + ) lines.append("=" * self.TABLE_WIDTH) lines.append("") @@ -185,7 +193,7 @@ class MemoryAnalyzerCLI(MemoryAnalyzer): # Top 15 largest core symbols lines.append("") - lines.append("Top 15 Largest [esphome]core Symbols:") + lines.append(f"Top 15 Largest {_COMPONENT_CORE} Symbols:") sorted_core_symbols = sorted( self._esphome_core_symbols, key=lambda x: x[2], reverse=True ) @@ -199,10 +207,12 @@ class MemoryAnalyzerCLI(MemoryAnalyzer): esphome_components = [ (name, mem) for name, mem in components - if name.startswith("[esphome]") and name != "[esphome]core" + if name.startswith(_COMPONENT_PREFIX_ESPHOME) and name != _COMPONENT_CORE ] external_components = [ - (name, mem) for name, mem in components if name.startswith("[external]") + (name, mem) + for name, mem in components + if name.startswith(_COMPONENT_PREFIX_EXTERNAL) ] top_esphome_components = sorted( @@ -217,7 +227,7 @@ class MemoryAnalyzerCLI(MemoryAnalyzer): # Check if API component exists and ensure it's included api_component = None for name, mem in components: - if name == "[esphome]api": + if name == _COMPONENT_API: api_component = (name, mem) break