Skip to content

Commit

Permalink
ipn: remove use of reflect.MethodByName (tailscale#10652)
Browse files Browse the repository at this point in the history
Using reflect.MethodByName disables some linked deadcode optimizations
and makes our binaries much bigger.
Difference before/after this commit:
```
-rwxr-xr-x  1 awly awly  30M Dec 19 15:28 tailscaled.after*
-rwxr-xr-x  1 awly awly  43M Dec 19 15:27 tailscaled.before*
```

Fixes tailscale#10627

Signed-off-by: Andrew Lytvynov <awly@tailscale.com>
  • Loading branch information
awly committed Dec 20, 2023
1 parent 09136e5 commit 7a2eb22
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions ipn/prefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,12 +414,20 @@ func (m *MaskedPrefs) Pretty() string {
continue
}
mpf := mpv.Field(i - 1)
prettyFn := mf.MethodByName("Pretty")
if !prettyFn.IsValid() {
panic(fmt.Sprintf("MaskedPrefs field %q is missing the Pretty method", name))
// This would be much simpler with reflect.MethodByName("Pretty"),
// but using MethodByName disables some linker optimizations and
// makes our binaries much larger. See
// https://github.com/tailscale/tailscale/issues/10627#issuecomment-1861211945
//
// Instead, have this explicit switch by field name to do type
// assertions.
switch name {
case "AutoUpdateSet":
p := mf.Interface().(AutoUpdatePrefsMask).Pretty(mpf.Interface().(AutoUpdatePrefs))
fmt.Fprintf(&sb, "%s={%s}", strings.TrimSuffix(name, "Set"), p)
default:
panic(fmt.Sprintf("unexpected MaskedPrefs field %q", name))
}
res := prettyFn.Call([]reflect.Value{mpf})
fmt.Fprintf(&sb, "%s={%s}", strings.TrimSuffix(name, "Set"), res[0].String())
}
}
sb.WriteString("}")
Expand Down

0 comments on commit 7a2eb22

Please sign in to comment.