Skip to content
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

Feat: added wasm optimization #835

Draft
wants to merge 15 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ jobs:
etc/tests/uniswap
- name: Install dependencies
run: cargo +stable make -V || cargo +stable install cargo-make
- name: Install wasm-opt
run: scripts/ci/install-wasm-opt.sh
- name: Build main contract
run: |
case ${{ matrix.profile }} in
Expand Down Expand Up @@ -81,6 +83,8 @@ jobs:
key: ${{ matrix.profile }}-cargo-modexp-test
- name: Install dependencies
run: cargo +stable make -V || cargo +stable install cargo-make
- name: Install wasm-opt
run: scripts/ci/install-wasm-opt.sh
- name: Test ${{ matrix.profile }} bench-modexp
run: cargo make --profile ${{ matrix.profile }} bench-modexp

Expand Down
24 changes: 12 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ exclude = [
]

[profile.release]
opt-level = 3
opt-level = 3 # "z" level decreases contract size, but at the same time, increases NEAR gas consumption in some cases
debug = false
debug-assertions = false
overflow-checks = true
Expand Down
12 changes: 12 additions & 0 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ dependencies = [
"build-engine",
"make-bin-directory",
"copy-engine-build",
"wasm-opt",
"post-engine-build-env",
]

Expand All @@ -269,6 +270,7 @@ dependencies = [
"build-engine",
"make-bin-directory",
"copy-engine-build",
"wasm-opt",
"post-engine-build-env",
]

Expand All @@ -287,6 +289,16 @@ env = { "RUSTFLAGS" = "${RUSTC_FLAGS_BUILD}", "CARGO_FEATURES" = "${CARGO_FEATUR
category = "Build"
run_task = "build-engine-flow"

[tasks.wasm-opt]
condition = { profiles = ["mainnet", "testnet", "mainnet-silo", "testnet-silo", "custom"] }
category = "Build"
script = '''
filesize_before=$(wc -c bin/${WASM_FILE} | awk '{print $1}')
wasm-opt -O4 bin/${WASM_FILE} -o bin/${WASM_FILE} --strip-debug --vacuum
filesize_after=$(wc -c bin/${WASM_FILE} | awk '{print $1}')
echo "File size before: [$filesize_before] and after: [$filesize_after]"
'''

[tasks.build]
condition = { profiles = ["mainnet", "mainnet-silo", "testnet", "testnet-silo", "localnet", "development", "custom"] }
env = { "RUSTFLAGS" = "-C strip=symbols --remap-path-prefix ${HOME}=/path/to/home/ --remap-path-prefix ${PWD}=/path/to/source/", "CARGO_FEATURES" = "${CARGO_FEATURES_BUILD}", "RELEASE" = "--release", "TARGET_DIR" = "release" }
Expand Down
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ documentation.

- Node.js (v18+)
- cargo-make
- wasm-opt

```sh
cargo install --force cargo-make
Expand All @@ -37,6 +38,25 @@ cargo install --force cargo-make
- Node.js (v18+)
- Docker
- cargo-make
- wasm-opt

### Prerequisite wasm-opt

For WebAssembly optimization we use `wasm-opt` from the [Binaryen toolchain for WebAssembly](https://github.com/WebAssembly/binaryen).

We recommend installing the release:
https://github.com/WebAssembly/binaryen/releases/tag/version_121

`wasm-opt` command should be available for the build process.

Verify version:

```bash
$ wasm-opt --version
wasm-opt version 121 (version_121)
```

Please keep in mind, that `wasm-opt` runs automatically when you run `cargo make build-*` and `cargo make test` commands.

## Development

Expand Down Expand Up @@ -133,6 +153,20 @@ here:
cargo make check
```

#### Running WebAssembly optimization

In common cases, you don't need to run `wasm-opt` manually, because
it's part of builds and tests.

But for development reasons only you can run:
- `wasm-opt` , runs WebAssembly optimization for pre-build wasm files for specific profile. Requires a
`--profile` argument.

For example, the following will run wasm-opt for pre-build mainnet binary:
```sh
cargo make --profile mainnet wasm-opt
```

#### Cleanup

The following tasks are available to clean up the workspace:
Expand Down
12 changes: 6 additions & 6 deletions engine-tests/src/tests/repro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn repro_GdASJ3KESs() {
block_timestamp: 1_645_717_564_644_206_730,
input_path: "src/tests/res/input_GdASJ3KESs.hex",
evm_gas_used: 706_713,
near_gas_used: 113,
near_gas_used: 110,
});
}

Expand All @@ -51,7 +51,7 @@ fn repro_8ru7VEA() {
block_timestamp: 1_648_829_935_343_349_589,
input_path: "src/tests/res/input_8ru7VEA.hex",
evm_gas_used: 1_732_181,
near_gas_used: 202,
near_gas_used: 198,
});
}

Expand All @@ -71,7 +71,7 @@ fn repro_FRcorNv() {
block_timestamp: 1_650_960_438_774_745_116,
input_path: "src/tests/res/input_FRcorNv.hex",
evm_gas_used: 1_239_721,
near_gas_used: 163,
near_gas_used: 159,
});
}

Expand All @@ -88,7 +88,7 @@ fn repro_5bEgfRQ() {
block_timestamp: 1_651_073_772_931_594_646,
input_path: "src/tests/res/input_5bEgfRQ.hex",
evm_gas_used: 6_414_105,
near_gas_used: 508,
near_gas_used: 475,
});
}

Expand All @@ -106,7 +106,7 @@ fn repro_D98vwmi() {
block_timestamp: 1_651_753_443_421_003_245,
input_path: "src/tests/res/input_D98vwmi.hex",
evm_gas_used: 1_035_348,
near_gas_used: 164,
near_gas_used: 160,
});
}

Expand All @@ -125,7 +125,7 @@ fn repro_Emufid2() {
block_timestamp: 1_662_118_048_636_713_538,
input_path: "src/tests/res/input_Emufid2.hex",
evm_gas_used: 1_156_364,
near_gas_used: 294,
near_gas_used: 291,
});
}

Expand Down
4 changes: 2 additions & 2 deletions engine-tests/src/tests/sanity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,8 @@ fn test_solidity_pure_bench() {
);
let near_gas = profile.all_gas();
assert!(
near_gas > 1400 * 1_000_000_000_000,
"Expected 1500 NEAR Tgas to be used, but only consumed {}",
near_gas > 1390 * 1_000_000_000_000,
"Expected 1400 NEAR Tgas to be used, but only consumed {}",
near_gas / 1_000_000_000_000,
);

Expand Down
10 changes: 5 additions & 5 deletions engine-tests/src/tests/uniswap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn test_uniswap_input_multihop() {

let (_amount_out, _evm_gas, profile) = context.exact_input(&tokens, INPUT_AMOUNT.into());

assert_eq!(107, profile.all_gas() / 1_000_000_000_000);
assert_eq!(105, profile.all_gas() / 1_000_000_000_000);
}

#[test]
Expand All @@ -53,17 +53,17 @@ fn test_uniswap_exact_output() {
utils::assert_gas_bound(profile.all_gas(), 32);
let wasm_fraction = 100 * profile.wasm_gas() / profile.all_gas();
assert!(
(38..=48).contains(&wasm_fraction),
"{wasm_fraction}% is not between 38% and 48%",
(30..=40).contains(&wasm_fraction),
"{wasm_fraction}% is not between 30% and 40%",
);

let (_amount_in, profile) =
context.exact_output_single(&token_a, &token_b, OUTPUT_AMOUNT.into());
utils::assert_gas_bound(profile.all_gas(), 17);
let wasm_fraction = 100 * profile.wasm_gas() / profile.all_gas();
assert!(
(38..=48).contains(&wasm_fraction),
"{wasm_fraction}% is not between 38% and 48%",
(30..=40).contains(&wasm_fraction),
"{wasm_fraction}% is not between 30% and 40%",
);
}

Expand Down
10 changes: 10 additions & 0 deletions scripts/ci/install-wasm-opt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

VERSION=121

if [[ ! -f wasm-opt ]]; then
mkdir binaryen
curl -sL https://github.com/WebAssembly/binaryen/releases/download/version_$VERSION/binaryen-version_$VERSION-x86_64-linux.tar.gz | tar -xz -C binaryen
cp binaryen/binaryen-version_$VERSION/bin/* /usr/local/bin
rm -rf binaryen
fi
Loading