Display per-function code sizes from the final binary (after tree-shaking). Args: symbols: List of symbol information from the ELF analysis board_name: Name of the board being analyzed top_n: Number of top functions to display
(
symbols: list[SymbolInfo], board_name: str, top_n: int = 50
)
| 105 | |
| 106 | |
| 107 | def display_function_sizes( |
| 108 | symbols: list[SymbolInfo], board_name: str, top_n: int = 50 |
| 109 | ) -> None: |
| 110 | """ |
| 111 | Display per-function code sizes from the final binary (after tree-shaking). |
| 112 | |
| 113 | Args: |
| 114 | symbols: List of symbol information from the ELF analysis |
| 115 | board_name: Name of the board being analyzed |
| 116 | top_n: Number of top functions to display |
| 117 | """ |
| 118 | # Filter for symbols with size > 0 and sort by size descending |
| 119 | sized_symbols = [s for s in symbols if s.size > 0] |
| 120 | sized_symbols.sort(key=lambda x: x.size, reverse=True) |
| 121 | |
| 122 | # Calculate statistics |
| 123 | total_size = sum(s.size for s in sized_symbols) |
| 124 | total_symbols = len(sized_symbols) |
| 125 | |
| 126 | # Type breakdown |
| 127 | type_counts: dict[str, int] = {} |
| 128 | type_sizes: dict[str, int] = {} |
| 129 | for sym in sized_symbols: |
| 130 | sym_type = sym.type |
| 131 | type_counts[sym_type] = type_counts.get(sym_type, 0) + 1 |
| 132 | type_sizes[sym_type] = type_sizes.get(sym_type, 0) + sym.size |
| 133 | |
| 134 | print("\n" + "=" * 80) |
| 135 | print(f"PER-FUNCTION CODE SIZE ANALYSIS - {board_name.upper()}") |
| 136 | print("(Functions from final binary after tree-shaking)") |
| 137 | print("=" * 80) |
| 138 | print(f"\nTotal symbols with size: {total_symbols}") |
| 139 | print(f"Total code size: {total_size:,} bytes ({total_size / 1024:.1f} KB)") |
| 140 | |
| 141 | print("\nSymbol Type Breakdown:") |
| 142 | for sym_type in sorted( |
| 143 | type_sizes.keys(), key=lambda t: type_sizes[t], reverse=True |
| 144 | ): |
| 145 | count = type_counts[sym_type] |
| 146 | size = type_sizes[sym_type] |
| 147 | percentage = (size / total_size * 100) if total_size > 0 else 0 |
| 148 | print( |
| 149 | f" {sym_type}: {count:4d} symbols, {size:8,} bytes ({size / 1024:6.1f} KB, {percentage:5.1f}%)" |
| 150 | ) |
| 151 | |
| 152 | print(f"\nTop {min(top_n, len(sized_symbols))} Largest Functions/Symbols:") |
| 153 | print("-" * 80) |
| 154 | |
| 155 | for i, sym in enumerate(sized_symbols[:top_n], 1): |
| 156 | # Calculate percentage of total |
| 157 | percentage = (sym.size / total_size * 100) if total_size > 0 else 0 |
| 158 | |
| 159 | # Display demangled name (truncate if too long) |
| 160 | display_name = sym.demangled_name |
| 161 | if len(display_name) > 70: |
| 162 | display_name = display_name[:67] + "..." |
| 163 | |
| 164 | print(f" {i:2d}. {sym.size:6,} bytes ({percentage:4.1f}%) - {display_name}") |