Skip to content

Commit e4da354

Browse files
committed
tools: add sizediff tool
This tool can be very useful to compare binary sizes as output by `-size=short`. This is a tool I wrote a while ago. It's not perfect (we should probably use a geomean) but it works well enough to get a good idea on the binary size impact of a change.
1 parent 4e41e90 commit e4da354

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

tools/sizediff

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env python3
2+
3+
# Small tool to compare code size between TinyGo runs (with the -size=short flag).
4+
5+
import sys
6+
7+
class Comparison:
8+
def __init__(self, command, code0, data0, bss0, code1, data1, bss1):
9+
self.command = command
10+
self.code0 = code0
11+
self.data0 = data0
12+
self.bss0 = bss0
13+
self.code1 = code1
14+
self.data1 = data1
15+
self.bss1 = bss1
16+
17+
@property
18+
def flash0(self):
19+
return self.code0 + self.data0
20+
21+
@property
22+
def flash1(self):
23+
return self.code1 + self.data1
24+
25+
@property
26+
def codediff(self):
27+
return self.code1 - self.code0
28+
29+
@property
30+
def flashdiff(self):
31+
return self.flash1 - self.flash0
32+
33+
def readSizes(path):
34+
sizes = []
35+
lines = open(path).readlines()
36+
for i in range(len(lines)):
37+
if not lines[i].strip().startswith('code '):
38+
continue
39+
# found a size header
40+
code, data, bss = map(int, lines[i+1].split()[:3])
41+
command = lines[i-1].strip()
42+
sizes.append({
43+
'command': command,
44+
'code': code,
45+
'data': data,
46+
'bss': bss,
47+
})
48+
return sizes
49+
50+
def main():
51+
path0 = sys.argv[1]
52+
path1 = sys.argv[2]
53+
sizes0 = readSizes(path0)
54+
sizes1 = readSizes(path1)
55+
comparisons = []
56+
for i in range(len(sizes0)):
57+
if i >= len(sizes1):
58+
print('%s has more commands than %s' % (path0, path1))
59+
print(' ', sizes0[i]['command'])
60+
break
61+
if sizes0[i]['command'] != sizes1[i]['command']:
62+
print('not the same command!')
63+
print(' ', sizes0[i]['command'])
64+
print(' ', sizes1[i]['command'])
65+
comparisons.append(Comparison(sizes0[i]['command'], sizes0[i]['code'], sizes0[i]['data'], sizes0[i]['bss'], sizes1[i]['code'], sizes1[i]['data'], sizes1[i]['bss']))
66+
if len(sizes0) < len(sizes1):
67+
print('%s has more commands than %s' % (path1, path0))
68+
print(' ', sizes1[len(sizes0)]['command'])
69+
comparisons.sort(key=lambda x: x.flashdiff)
70+
totalCode0 = 0
71+
totalCode1 = 0
72+
totalDiff = 0
73+
print(' before after diff')
74+
for comparison in comparisons:
75+
print('%7d %7d %6d %6.2f%% %s' % (comparison.flash0, comparison.flash1, comparison.flashdiff, comparison.flashdiff / comparison.flash0 * 100, comparison.command))
76+
totalCode0 += comparison.flash0
77+
totalCode1 += comparison.flash1
78+
totalDiff += comparison.flashdiff
79+
print('%7d %7d %6d %6.2f%% sum' % (totalCode0, totalCode1, totalDiff, totalDiff / totalCode0 * 100))
80+
81+
82+
if __name__ == '__main__':
83+
main()

0 commit comments

Comments
 (0)