[protobuf] internal/descfmt: replace uses of MethodByName with direct calls when possible

1 view
Skip to first unread message

Michael Stapelberg (Gerrit)

unread,
Jan 16, 2026, 6:42:00 AM (2 days ago) Jan 16
to Pierre Gimalac, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Lasse Folger, Go LUCI, Nicolas Hillegeer, golang-co...@googlegroups.com

Michael Stapelberg submitted the change

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:
  • requirement satisfiedCode-Review: +2 by Michael Stapelberg, +1 by Lasse Folger
  • requirement satisfiedTryBots-Pass: LUCI-TryBot-Result+1 by Go LUCI
Open in Gerrit
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: merged
Gerrit-Project: protobuf
Gerrit-Branch: master
Gerrit-Change-Id: Ib6ca16f7f7a871c8625d934c6115b47e525a42e9
Gerrit-Change-Number: 735401
Gerrit-PatchSet: 3
Gerrit-Owner: Pierre Gimalac <pierre....@datadoghq.com>
Gerrit-Reviewer: Lasse Folger <lasse...@google.com>
Gerrit-Reviewer: Michael Stapelberg <stape...@google.com>
Gerrit-CC: Nicolas Hillegeer <ak...@google.com>
open
diffy
satisfied_requirement
Reply all
Reply to author
Forward
0 new messages