Skip to main content

Anti Bloat

The following links have tutorials on reducing the size of your installers:

Rust Compression Features#

Add this to your src-tauri/Cargo.toml

[profile.release]
panic = "abort"
codegen-units = 1
lto = true
incremental = false
opt-level = "s"

Unstable Rust Compression Features#

The following methods involve using unstable compiler features and require having a rust nightly toolchain installed. If you don't have the nightly toolchain + rust-src nightly component added, try the following:

$ rustup toolchain install nightly
$ rustup component add rust-src --toolchain nightly

The Rust Standard Library comes precompiled. You can instead apply the optimization options used for the rest of your binary + dependencies to the std with an unstable flag. This flag requires specifying your target, so know the target triple that you are targeting.

$ cargo +nightly build --release -Z build-std --target x86_64-unknown-linux-gnu

If you are using panic = "abort" in your release profile optimizations, then you need to make sure the panic_abort crate is compiled with std. Additionally, an extra std feature can be used to further reduce the binary size. The following applies both:

$ cargo +nightly build --release -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort --target x86_64-unknown-linux-gnu

See the unstable documentation for more details about -Z build-std and -Z build-std-features.

Stripping#

Binary size can easily be reduced by stripping out debugging information from binaries that ship to end users. This is not good for debuggable builds, but means good binary size savings for end user binaries. The easiest way is to use the famous strip utility to remove this debugging information.

$ strip target/release/my_application

See your local strip manpage for more information and flags that can be used to specify what information gets stripped out from the binary.

Allowlist config#

You can also reduce the application size with the allowlist config, and only enabling what you need. Sometimes this is useful with Tauri's Bridge-Pattern or others, depending on needs.

For example in tauri.conf.json file:

{
  "tauri": {
    "allowlist": {
      "all": false,
      "fs": {
        "writeFile": true,
        "writeBinaryFile": true
      },
      "shell": {
        "execute": true
      },
      "dialog": {
        "save": true
      }
    }
  }
}

UPX#

UPX, Ultimate Packer for eXecutables, is a dinosaur amongst the binary packers. This 23-year old, well-maintained piece of kit is GPL-v2 licensed with a pretty liberal usage declaration. Our understanding of the licensing is that you can use it for any purposes (commercial or otherwise) without needing to change your license unless you modify the source code of UPX.

Basically it compresses the binary and decompresses it at runtime. It should work for pretty much any binary type out there. Read more: https://github.com/upx/upx

Usage on macOS#

$ brew install upx
$ yarn tauri build
$ upx --ultra-brute src-tauri/target/release/bundle/macos/app.app/Contents/macOS/app
                       Ultimate Packer for eXecutables
                          Copyright (C) 1996 - 2018
UPX 3.95        Markus Oberhumer, Laszlo Molnar & John Reiser   Aug 26th 2018

        File size         Ratio      Format      Name
   --------------------   ------   -----------   -----------
    963140 ->    274448   28.50%   macho/amd64   app