cobra icon indicating copy to clipboard operation
cobra copied to clipboard

Allow linker to perform deadcode elimination for program using Cobra

Open aarzilli opened this issue 1 year ago • 20 comments

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 avatar May 05 '23 11:05 aarzilli

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar May 05 '23 11:05 CLAassistant

This PR exceeds the recommended size of 200 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

github-actions[bot] avatar May 05 '23 11:05 github-actions[bot]

The CLA thing isn't working for some reason.

aarzilli avatar May 05 '23 14:05 aarzilli

This PR exceeds the recommended size of 200 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

github-actions[bot] avatar May 05 '23 20:05 github-actions[bot]

Thanks @aarzilli ! I like the idea of optimizing things. This will require some thought so I can understand clearly the behaviour change. Thanks for adding a test it should help with this.

marckhouzam avatar Jun 12 '23 12:06 marckhouzam

Unless I made a mistake there shouldn't be any behavior changes (as in, observable from the outside). Happy to explain anything about this if needed.

aarzilli avatar Jun 12 '23 12:06 aarzilli

Ping?

aarzilli avatar Jul 28 '23 07:07 aarzilli

Ping?

aarzilli avatar Oct 10 '23 09:10 aarzilli

This PR exceeds the recommended size of 200 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

github-actions[bot] avatar Feb 10 '24 13:02 github-actions[bot]

This PR exceeds the recommended size of 200 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

github-actions[bot] avatar Apr 10 '24 08:04 github-actions[bot]

This PR exceeds the recommended size of 200 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

github-actions[bot] avatar May 10 '24 14:05 github-actions[bot]

This PR exceeds the recommended size of 200 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

github-actions[bot] avatar Jun 13 '24 10:06 github-actions[bot]

This PR exceeds the recommended size of 200 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

github-actions[bot] avatar Aug 10 '24 11:08 github-actions[bot]