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

wasm-opt for improving gas consumption #818

Open
Tarnadas opened this issue Aug 10, 2023 · 3 comments
Open

wasm-opt for improving gas consumption #818

Tarnadas opened this issue Aug 10, 2023 · 3 comments
Assignees

Comments

@Tarnadas
Copy link

Hey,

I looked through your build steps and haven't found usage of wasm-opt, which honestly surprised me. I basically use it for every contract I develop for a free bonus in runtime perf and code size.

I am currently developing a contract that uses Aurora SDK to do interactions from Aurora to the contract on Near. I have a couple of integration tests and I wanted to check what wasm-opt would change regarding gas consumption. Most of the gas is not used from Aurora contract, but from the cross contract calls and contracts on native Near, but you can clearly see a small improvement. I precompiled the Aurora contract for the integration tests like you're doing it in the Makefile, but for the optimized version I invoked wasm-opt -O4 res/aurora_engine.wasm -o res/aurora_engine_opt.wasm --strip-debug --vacuum.

Here are the relevant logs from my result:
Unoptimized:

    approve wNear for Solidity contract gas burnt: 7.526 TGas
    storageDeposit gas burnt: 109.358 TGas
    approve ETH for Solidity contract gas burnt: 7.757 TGas
    approve wNear for Solidity contract gas burnt: 7.522 TGas
    storageDeposit gas burnt: 73.606 TGas
    approve ETH for Solidity contract gas burnt: 7.633 TGas
    deposit gas burnt: 100.992 TGas
    createLimitOrder gas burnt: 47.737 TGas
    swap gas burnt: 126.609 TGas
    withdrawAll gas burnt: 44.345 TGas
    withdrawAll gas burnt: 63.940 TGas
    withdrawAll gas burnt: 44.189 TGas
    withdrawAll gas burnt: 44.197 TGas

Optimized:

    approve wNear for Solidity contract gas burnt: 7.503 TGas
    storageDeposit gas burnt: 108.474 TGas
    approve ETH for Solidity contract gas burnt: 7.733 TGas
    approve wNear for Solidity contract gas burnt: 7.480 TGas
    approve ETH for Solidity contract gas burnt: 7.591 TGas
    deposit gas burnt: 99.882 TGas
    createLimitOrder gas burnt: 46.957 TGas
    swap gas burnt: 125.282 TGas
    withdrawAll gas burnt: 43.621 TGas
    withdrawAll gas burnt: 63.172 TGas
    withdrawAll gas burnt: 43.464 TGas
    withdrawAll gas burnt: 43.472 TGas

Additionally it also reduces code size:

.rwxr-xr-x 1.0M marior  9 Aug 13:45 aurora_engine.wasm
.rw-r--r-- 930k marior 10 Aug 07:20 aurora_engine_opt.wasm

You can also use wasm-opt for the XCC router contract.

Is there any reason why you would not want to use wasm-opt?
Btw only up until version 110 is working. Any newer version produces an unusable binary. Idk what caused this, but maybe someone else knows more about this

@aleksuss
Copy link
Member

@Tarnadas, thank you for the information. To be honest, I tried the wasm-opt util, but I didn't get the result you got. The size of the smart contract was decreased, but at the same time, gas costs were increased. But anyway, I will take a look once again and provide feedback.

@Tarnadas
Copy link
Author

Tarnadas commented Aug 10, 2023

thx @aleksuss,
I should also show the command that I used for compiling the engine:
cargo build --target wasm32-unknown-unknown --release --no-default-features --features=mainnet,integration-test -p aurora-engine -Z avoid-dev-deps
Not sure if the integration-test feature could affect it. I used Rust 1.69.0

@aleksuss aleksuss self-assigned this Aug 10, 2023
@mrLSD
Copy link
Member

mrLSD commented Sep 6, 2023

Btw only up until version 110 is working. Any newer version produces an unusable binary.

It's near-core bug: near/nearcore#9143

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants