Skip to content

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

@ikonst

Description

@ikonst

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ProposalToolProposalIssues describing a requested change to a Go tool or command-line program.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions