Skip to content

Commit f1ded84

Browse files
committed
cmd/tailscaled: add disabled debug file to force reflect for binary size experiments
This adds a file that's not compiled by default that exists just to make it easier to do binary size checks, probing what a binary would be like if it included reflect methods (as used by html/template, etc). As an example, once tailscaled uses reflect.Type.MethodByName(non-const-string) anywhere, the build jumps up by 14.5 MB: $ GOOS=linux GOARCH=amd64 ./tool/go build -tags=ts_include_cli,ts_omit_webclient,ts_omit_systray,ts_omit_debugeventbus -o before ./cmd/tailscaled $ GOOS=linux GOARCH=amd64 ./tool/go build -tags=ts_include_cli,ts_omit_webclient,ts_omit_systray,ts_omit_debugeventbus,ts_debug_forcereflect -o after ./cmd/tailscaled $ ls -l before after -rwxr-xr-x@ 1 bradfitz staff 41011861 Sep 9 07:28 before -rwxr-xr-x@ 1 bradfitz staff 55610948 Sep 9 07:29 after This is particularly pronounced with large deps like the AWS SDK. If you compare using ts_omit_aws: -rwxr-xr-x@ 1 bradfitz staff 38284771 Sep 9 07:40 no-aws-no-reflect -rwxr-xr-x@ 1 bradfitz staff 45546491 Sep 9 07:41 no-aws-with-reflect That means adding AWS to a non-reflect binary adds 2.7 MB but adding AWS to a reflect binary adds 10 MB. Updates tailscale#17063 Updates tailscale#12614 Change-Id: I18e9b77c9cf33565ce5bba65ac5584fa9433f7fb Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
1 parent f4ae81e commit f1ded84

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Tailscale Inc & AUTHORS
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
//go:build ts_debug_forcereflect
5+
6+
// This file exists for benchmarking binary sizes. When the build tag is
7+
// enabled, it forces use of part of the reflect package that makes the Go
8+
// linker go into conservative retention mode where its deadcode pass can't
9+
// eliminate exported method.
10+
11+
package main
12+
13+
import (
14+
"reflect"
15+
"time"
16+
)
17+
18+
func init() {
19+
// See Go's src/cmd/compile/internal/walk/expr.go:usemethod for
20+
// why this is isn't a const.
21+
name := []byte("Bar")
22+
if time.Now().Unix()&1 == 0 {
23+
name[0] = 'X'
24+
}
25+
_, _ = reflect.TypeOf(12).MethodByName(string(name))
26+
}

0 commit comments

Comments
 (0)