aurora-is-near / aurora-engine

⚙️ Aurora Engine implements an Ethereum Virtual Machine (EVM) on the NEAR Protocol.
https://doc.aurora.dev/develop/compat/evm
325 stars 78 forks source link

wasm-opt for improving gas consumption #818

Open Tarnadas opened 1 year ago

Tarnadas commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

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

mrLSD commented 11 months ago

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

It's near-core bug: https://github.com/near/nearcore/issues/9143