Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Update optimize contracts section #310

Merged
merged 4 commits into from
Feb 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 11 additions & 71 deletions docs/getting-started/hello-world.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -344,84 +344,24 @@ The following output should appear.
["Hello", "friend"]
```

[Write a Contract]: #write-a-contract
[Deploy to a Local Network]: deploy-to-a-local-network
[Build]: #build
[`soroban-cli`]: ../getting-started/setup#install-the-soroban-cli

## Optimizing Builds

Building optimized contracts to be as small as possible requires some additional tools, and requires installing the `nightly` Rust toolchain and `wasm-opt`.

:::tip

Building optimized contracts is only necessary when deploying to a network with fees or when analyzing and profiling a contract to get it as small as possible. If you're just starting out writing a contract, these steps are not necessary. See [Build] for details on how to build for development.

:::

[Build]: #build

### Install Rust `nightly`

To install the nightly Rust toolchain use [`rustup`].

[`rustup`]: ../getting-started/setup#install-rust
Use `soroban contract optimize` to further minimize the size of the `.wasm`.

```sh
rustup install nightly
rustup target add --toolchain nightly wasm32-unknown-unknown
rustup component add --toolchain nightly rust-src
soroban contract optimize \
--wasm target/wasm32-unknown-unknown/release/first_project.wasm
```

### Install `wasm-opt`

To install `wasm-opt`, install [`binaryen`]. Depending on your operating system there may be different ways to install it.

[`binaryen`]: https://github.com/WebAssembly/binaryen
This will optimize and output a new `first_project.optimized.wasm` file in the same location as the input `.wasm`.

<Tabs>
<TabItem value="macos" label="macOS" default>

```sh
brew install binaryen
```

</TabItem>
<TabItem value="linux" label="Linux">

For distributions that use `apt`:

```sh
sudo apt install binaryen
```

For other distributions see:
https://github.com/WebAssembly/binaryen/releases

</TabItem>
<TabItem value="windows" label="Windows">

For install options see: https://github.com/WebAssembly/binaryen/releases

</TabItem>
</Tabs>

### Build with `nightly` and `wasm-opt`

Build your contract using nightly. The additional options instruct the compiler to remove unnecessary information from the contract.
:::tip

```sh
cargo +nightly build \
--target wasm32-unknown-unknown \
--release \
-Z build-std=std,panic_abort \
-Z build-std-features=panic_immediate_abort
```
Building optimized contracts is only necessary when deploying to a network with fees or when analyzing and profiling a contract to get it as small as possible. If you're just starting out writing a contract, these steps are not necessary. See [Build] for details on how to build for development.

Use `wasm-opt` to further minimize the size of the `.wasm`.
:::

```sh
wasm-opt -Oz \
target/wasm32-unknown-unknown/release/first_project.wasm \
-o target/wasm32-unknown-unknown/release/first_project_optimized.wasm
```
[Write a Contract]: #write-a-contract
[Deploy to a Local Network]: deploy-to-a-local-network
[Build]: #build
[`soroban-cli`]: ../getting-started/setup#install-the-soroban-cli