Skip to content

Commit e7890bf

Browse files
committed
guides: add guide how to optimize binary size
This is mostly intended for WebAssembly but should be useful for all targets.
1 parent 8f778bc commit e7890bf

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
title: "Optimizing binaries"
3+
weight: 11
4+
description: |
5+
How to improve speed or reduce code size for TinyGo programs.
6+
---
7+
8+
There are various ways TinyGo programs can be optimized for speed, size, or debuggability.
9+
10+
## Optimizing for code size
11+
12+
By default, binaries are already optimized for code size (`-opt=z`, equivalent to `-Oz`, is the default). However, there are various things you can do to improve code size.
13+
14+
### Do not import large packages
15+
16+
You may be tempted to import packages like `fmt` for simple formatted printing. But `fmt` is very large, so unless you absolutely need it, it is recommended to try to avoid it.
17+
18+
In many cases, you can use `println` instead:
19+
20+
```go
21+
println("2 + 2 =", 2 + 2) // prints: "2 + 2 = 4"
22+
```
23+
24+
### Remove debug symbols (WebAssembly)
25+
26+
You can omit debug symbols to reduce the size of WebAssembly binaries using the `-no-debug` flag:
27+
28+
$ tinygo build -o test.wasm examples/serial && ls -lh test.wasm
29+
-rwxrwxr-x 1 ayke ayke 93K 4 sep 17:04 test.wasm
30+
31+
$ tinygo build -o test.wasm -no-debug examples/serial && ls -lh test.wasm
32+
-rwxrwxr-x 1 ayke ayke 30K 4 sep 17:04 test.wasm
33+
34+
As you can see, debug information is ⅔ of this binary so removing it helps a lot!
35+
36+
This may also help on desktop systems (Windows, macOS, Linux) if you want small binaries, but you can also simply use the `strip` utility. It does not have an effect on microcontrollers (Arduino etc) because the debug information is not stored in the firmware image.
37+
38+
### Other options
39+
40+
You can also try the following:
41+
42+
- Disable goroutines using `-scheduler=none`. It will remove support for the `go` keyword and some related features.
43+
- Don't print panic messages using `-panic=trap`. This will make debugging harder but it can cut down on code size.
44+
- Disable the GC using `-gc=leaking` (for very short-lived programs only).
45+
- Try `-opt=s` and `-opt=2`. While these options normally increase binary size (for higher performance), in rare cases they will actually reduce binary size.
46+
47+
With all these flags applied, the binary is reduced from 93K to just 1.6K!
48+
49+
$ tinygo build -o test.wasm -no-debug -panic=trap -scheduler=none -gc=leaking examples/serial && ls -lh test.wasm
50+
-rwxrwxr-x 1 ayke ayke 1,6K 4 sep 17:30 test.wasm
51+
52+
## Optimize for speed
53+
54+
Flags you can try, roughly in order of importance:
55+
56+
- `-opt=2` to optimize for speed over code size. The default is to optimize for size (`-opt=z`).
57+
- `-gc=leaking` disables the garbage collector, which can sometimes have a large effect (especially on WebAssembly). Of course, memory will never be freed so this is only appropriate for very short-lived programs.
58+
- `-scheduler=none` disables the scheduler. This won't usually have much of an effect, but might help on WebAssembly because WebAssembly doesn't have native support for stack switching ([yet](https://github.com/WebAssembly/stack-switching)).
59+
- `-panic=trap` might help a little bit by removing some code.
60+
61+
62+
## Optimize for debugging
63+
64+
While TinyGo binaries by default include debug information, they are also heavily optimized for size. Therefore, debugging might be difficult by default. You can try the following to make it easier:
65+
66+
- `-opt=1` will disable some optimization passes which can make debugging somewhat easier.
67+
- `-opt=0` will remove most optimization passes, which should make debugging even easier than `-opt=1`. However, because so many passes are disabled, some programs don't work well anymore.
68+
- Of course, don't use `-no-debug`.

0 commit comments

Comments
 (0)