Change information
Commit message:
internal/descfmt: replace uses of MethodByName with direct calls when possible
Update internal/descfmt/stringer.go to avoid calling MethodByName when possible.
MethodByName allows calling a method even when the signature is unknown, but it forces the linker to keep all methods with a matching name, increasing binary size with dead code. On the other hand casting to an interface only keeps methods with a matching signature.
Using MethodByName also bypasses compiler typing, so making a direct call is safer.
In this specific case, the code checks whether a type implements an interface or is a specific type before using MethodByName and FieldByName to access its methods and fields, so we can just access them directly.
I checked the size difference for the datadog-agent and kubernetes binaries (ranging from 20 to 100MiB), and the resulting gains were between 100 and 300kiB, which is small but not negligible.
This is a follow-up of https://go.dev/cl/734280.
Fixes golang/protobuf#1708
Change-Id: Ib6ca16f7f7a871c8625d934c6115b47e525a42e9
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/735401
Reviewed-by: Lasse Folger <lasse...@google.com>
LUCI-TryBot-Result: Go LUCI <golang...@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Stapelberg <stape...@google.com>
Files:
- M internal/descfmt/stringer.go
Change size: M
Delta: 1 file changed, 83 insertions(+), 96 deletions(-)
Branch: refs/heads/master
Submit Requirements:
Code-Review: +2 by Michael Stapelberg, +1 by Lasse Folger
TryBots-Pass: LUCI-TryBot-Result+1 by Go LUCI