Skip to content

proposal: cmd/link: option to force DCE when reflect Method is present #72888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ikonst opened this issue Mar 16, 2025 · 3 comments
Closed

proposal: cmd/link: option to force DCE when reflect Method is present #72888

ikonst opened this issue Mar 16, 2025 · 3 comments
Labels
Proposal ToolProposal Issues describing a requested change to a Go tool or command-line program.
Milestone

Comments

@ikonst
Copy link
Contributor

ikonst commented Mar 16, 2025

Proposal Details

Summary

Provide an option (through GODEBUG or a linker flag?) to force exported method dead-code elimination despite presence of reflect Method or (non-constant) MethodByName in the code. When enabled, the resulting executable would have its reflection APIs return only methods that are otherwise referenced.

For edge cases, the user could force a reference e.g. by adding a dummy MethodByName

_, _ = reflect.TypeOf(0).MethodByName("MissingMethod")

Motivation

The user will trade a compatibility promise (could break packages in unpredictable ways) for faster build times and smaller binaries (can be dramatic when importing some excessively large packages).

It's not practical for a user to achieve this otherwise, e.g. through being intentional in the choice of imported packages, in anything but the smallest of codebases. While ensuring that no dependency calls those functions, the user will soon realize that:

  • even the ever-ubiquitous text/template package calls reflect's MethodByName
  • curation of all packages today could be foiled by any package changing tomorrow
  • often method reflection is not actually required (e.g. provided data has only fields but text/template trying to find a method before trying it as a field), and the linker's choice to disable DCE stems from caution rather than an educated guess

P.S.

This very proposal was implemented in https://go-review.googlesource.com/c/go/+/488575 but wasn't meant to be submitted.

@gopherbot gopherbot added this to the Proposal milestone Mar 16, 2025
@ikonst ikonst changed the title proposal: cmd/link: optionally ignore ReflectMethod proposal: cmd/link: option to force DCE when reflect Method is present Mar 16, 2025
@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Mar 16, 2025
@ianlancetaylor
Copy link
Member

In order to use such an option you would have to know that it worked for every single call to MethodByName in your program. Given the widespread use of text/template and similar packages in the Go ecosystem, that only seems possible for very restricted programs. So this seems like a configuration knob that would have to be maintained and tested, but that almost nobody would be able to use. The benefit does not seem worth the cost.

@gabyhelp gabyhelp added the ToolProposal Issues describing a requested change to a Go tool or command-line program. label Mar 16, 2025
@ikonst
Copy link
Contributor Author

ikonst commented Mar 16, 2025

Thanks for the feedback @ianlancetaylor, I agree with your rationale. Similarly to avoiding MethodByName working only for the smallest codebases, so is being certain that such flag would be safe to use.

I'm retracting this proposal. Members of the community (e.g. Uber here) do appear interested in maintaining DCE in large codebases and finding a practical approach, so I think it was good to put this up as a straw-man proposal (to capture what isn't practical).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Proposal ToolProposal Issues describing a requested change to a Go tool or command-line program.
Projects
None yet
Development

No branches or pull requests

4 participants