Skip to content
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

Allow linker to perform deadcode elimination for program using Cobra #1956

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Commits on Oct 15, 2024

  1. Restructure code to let linker perform deadcode elimination step

    Cobra, in its default configuration, will execute a template to generate
    help, usage and version outputs. Text/template execution calls MethodByName
    and MethodByName disables dead code elimination in the Go linker, therefore
    all programs that make use of cobra will be linked with dead code
    elimination disabled, even if they end up replacing the default usage, help
    and version formatters with a custom function and no actual text/template
    evaluations are ever made at runtime.
    
    Dead code elimination in the linker helps reduce disk space and memory
    utilization of programs. For example, for the simple example program used by
    TestDeadcodeElimination 40% of the final executable size is dead code. For a
    more realistic example, 12% of the size of Delve's executable is deadcode.
    
    This PR changes Cobra so that, in its default configuration, it does not
    automatically inhibit deadcode elimination by:
    
    1. changing Cobra's default behavior to emit output for usage and help using
       simple Go functions instead of template execution
    2. quarantining all calls to template execution into SetUsageTemplate,
       SetHelpTemplate and SetVersionTemplate so that the linker can statically
       determine if they are reachable
    aarzilli committed Oct 15, 2024
    Configuration menu
    Copy the full SHA
    4da4d97 View commit details
    Browse the repository at this point in the history
  2. addressing review comments

    aarzilli committed Oct 15, 2024
    Configuration menu
    Copy the full SHA
    84ffa30 View commit details
    Browse the repository at this point in the history