Description
General case for investigating and reducing bloat.
Compiling using Clang with -ftime-trace
produces .json
files next to the object files (.o
), which can be loaded in Chrome to investigate compile times.
chrome://tracing/
Using nm
with --print-size
we can examine object files (.o
) to determine what symbols contribute the most space to the object file. This can be used to determine which source code elements produce the largest amount of code bloat. It may also be helpful to pass the --demangle
flag.
nm --print-size --size-sort --demangle .build/Debug_Linux_nas2d/intermediate/Configuration.o
Potentially the whole build folder can be scanned and sorted by symbol size:
find .build/Debug_Linux_nas2d/intermediate/ -name '*.o' -exec nm --print-size --size-sort --demangle {} \; | cut -d' ' -f2- | sort | uniq
It may be helpful to leave off the uniq
call, to get a better sense of how often a symbol may appear.
Edit:
It's possible to run nm
on the library file. By default, it contains sections for each internal object file. If the --size-sort
flag is passed, it only sorts within an object file section. To sort the entire file contents, it may be necessary to pipe it through sort
, and sort on the size field which is the second field (--key=2
).
nm --print-size --size-sort --demangle lib/libnas2d.a | sort --key=2
We can get much of the same information as nm
from the linker (ld
) by generating a map file. This can be done with the custom makefile
variable LDFLAGS_EXTRA
:
export LDFLAGS_EXTRA="-Wl,-Map=mapfile"
The -Wl,
is used to pass a flag to the linker. The -Map=mapfile
is the linker flag to generate the mapfile. Use %
as the filename to get the output filename with .map
added.
Related: