Skip to content

Instantly share code, notes, and snippets.

@eglaysher
Created August 26, 2010 18:57
Show Gist options
  • Save eglaysher/551976 to your computer and use it in GitHub Desktop.
Save eglaysher/551976 to your computer and use it in GitHub Desktop.
find_heaviest_weak_symbols.py: Find the most included symbols in .o files.
#!/usr/bin/python
#
# Is your C++ project made of bloat and fail? Wouldn't it be great if you got a
# nice table of the individual symbols sorted by total size (size of one
# instance * number of instances)?
#
# Use as such:
#
# $ find <builddir> -name "*.o" | xargs nm -S | c++filt | find_heaviest_weak_symbols.py
import sys
symbols = { }
for line in sys.stdin.readlines():
if line.find(" W ") != -1:
size = line[17:33]
name = line[36:].rstrip()
if name in symbols:
entry = symbols[name]
else:
entry = {'size':0, 'count':0}
symbols[name] = entry
entry['size'] += int(size, 16)
entry['count'] += 1
# Translate the symbols dictionary into an array of sortable tuples
# (total_size, count, name) for outputs.
def converter(x): return (x[1]['size'], x[1]['count'], x[0])
for_output = map(converter, symbols.items())
for_output.sort()
print " Size Count Name"
print "------ ----- ---------------------------------------------------------------"
for row in for_output:
# Don't print instances where there's only one copy of the symbol in the
# entire program, no matter how big.
if row[1] > 1:
print "{0[0]:6} {0[1]:5} {0[2]}".format(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment