Repository navigation
docs(v2): add binary size guide and mkdocs navigation for v2 build tags #2456
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # Binary Size | ||
|
|
||
| Go removes unreachable code during compilation. The first step in size optimization | ||
| is measuring the shipped executable rather than assuming a feature introduces overhead. | ||
|
|
||
| ```sh-session | ||
| go build -trimpath -o myapp ./cmd/myapp | ||
| ls -lh myapp | ||
| ``` | ||
|
|
||
| ## Compiler and Linker Flags | ||
|
|
||
| For release builds, combine path trimming with symbol table and debug metadata removal: | ||
|
|
||
| ```sh-session | ||
| go build -trimpath -ldflags="-s -w" -o myapp ./cmd/myapp | ||
| ls -lh myapp | ||
| ``` | ||
|
|
||
| ### Flag Breakdown | ||
|
|
||
| - `-trimpath`: Strips local file system paths from the compiled executable. This removes | ||
| machine-specific directory paths from panic stack traces, produces reproducible | ||
| binaries, and reduces string data size. | ||
| - `-ldflags="-s -w"`: | ||
| - `-s`: Omits the symbol table. | ||
| - `-w`: Omits DWARF debugging information. | ||
|
|
||
| Panic stack traces retain function names and line offsets because the Go runtime | ||
| maintains internal program counter tables in a distinct section. | ||
|
|
||
| ## Inspecting Binary Contents | ||
|
|
||
| Use the Go toolchain to inspect embedded build metadata and symbol allocations on an unstripped binary: | ||
|
|
||
| ```sh-session | ||
| go version -m myapp | ||
| go tool nm -size myapp | sort -k2,2nr | head -40 | ||
| ``` | ||
|
|
||
| - `go version -m`: Displays module dependencies, Go toolchain version, build tags, and | ||
| compiler flags embedded in the binary. | ||
| - `go tool nm -size`: Lists symbols sorted by size, identifying the largest functions | ||
| and data structures. Run this command against an unstripped binary, as `-s` removes | ||
| the symbol table. | ||
|
|
||
| ## Build Tags in v2 | ||
|
|
||
| In `urfave/cli/v2`, two build tags prune optional dependencies and features at compile time: | ||
|
|
||
| | Build Tag | Target Feature | Dependency Removed | Approximate Savings | | ||
| |-----------|----------------|--------------------|---------------------| | ||
| | `urfave_cli_no_docs` | Markdown and man page generation (`ToMarkdown`, `ToMan`) | Documentation generator packages | 300 to 400 KB | | ||
| | `urfave_cli_no_suggest` | Command and flag typo suggestions ("Did you mean?") | `github.com/xrash/smetrics` | Variable | | ||
|
|
||
| ### Usage | ||
|
|
||
| To build without documentation generation methods: | ||
|
|
||
| ```sh-session | ||
| go build -tags urfave_cli_no_docs -trimpath -ldflags="-s -w" -o myapp ./cmd/myapp | ||
| ``` | ||
|
|
||
| To build without typo suggestions: | ||
|
|
||
| ```sh-session | ||
| go build -tags urfave_cli_no_suggest -trimpath -ldflags="-s -w" -o myapp ./cmd/myapp | ||
| ``` | ||
|
|
||
| To combine both build tags: | ||
|
|
||
| ```sh-session | ||
| go build -tags "urfave_cli_no_docs urfave_cli_no_suggest" -trimpath -ldflags="-s -w" -o myapp ./cmd/myapp | ||
| ``` | ||
|
|
||
| ## Transition to v3 | ||
|
|
||
| These build tags are not present in `urfave/cli/v3` because v3 decouples these features by design: | ||
|
|
||
| - Documentation generation lives in the separate module `github.com/urfave/cli-docs/v3`. | ||
| - Typo suggestions use an internal Jaro-Winkler implementation with no external dependencies. | ||
| - v3 provides the `urfave_cli_no_template` build tag to restore linker dead code elimination. | ||
|
|
||
| See the [v2 to v3 Migration Guide](migrating-to-v3.md) and [v3 Binary Size](../v3/binary-size.md) | ||
| for details. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Twenty lines of code to show how to use a build tag seems excessive to me. I would omit all this.