Skip to content

Commit 14eb47e

Browse files
authored
add funcSize command (#21)
1 parent 8c7e7c0 commit 14eb47e

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

funcSize.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
"regexp"
7+
)
8+
9+
type funcSizeRunner struct {
10+
messageRE *regexp.Regexp
11+
}
12+
13+
func (r *funcSizeRunner) Init() {
14+
r.messageRE = regexp.MustCompile(`(.*) STEXT.* size=(\d+)`)
15+
}
16+
17+
func (r *funcSizeRunner) Run(pkg string) error {
18+
cmd := exec.Command("go", r.getCmd(pkg)...)
19+
out, err := cmd.CombinedOutput()
20+
if err != nil {
21+
return fmt.Errorf("%v: %s", err, out)
22+
}
23+
24+
type resultRow struct {
25+
Fn string `json:"fn"`
26+
Size string `json:"size"`
27+
}
28+
results := []resultRow{}
29+
30+
// TODO: add a CLI flag for the function name filtering?
31+
// Having to use a grep in 99% of use cases is not very convenient.
32+
for _, submatches := range r.messageRE.FindAllStringSubmatch(string(out), -1) {
33+
results = append(results, resultRow{
34+
Fn: submatches[1],
35+
Size: submatches[2],
36+
})
37+
}
38+
39+
if asJSON {
40+
marshalJSON(results)
41+
return nil
42+
}
43+
44+
for _, r := range results {
45+
fmt.Printf("%s: %s bytes\n", r.Fn, r.Size)
46+
}
47+
return nil
48+
}
49+
50+
func (r *funcSizeRunner) getCmd(pkg string) []string {
51+
return goArgs(pkg, goArgsBuild, goArgsGcFlags("-S"))
52+
}

main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ var cmds = []acmd.Command{
5656
return run(&boundCheckRunner{})
5757
},
5858
},
59+
{
60+
Name: "funcSize",
61+
Alias: "fsize",
62+
Description: "list function machine code sizes in bytes",
63+
ExecFunc: func(_ context.Context, _ []string) error {
64+
return run(&funcSizeRunner{})
65+
},
66+
},
5967
}
6068

6169
type subCommandRunner interface {

0 commit comments

Comments
 (0)