Gas Cost Comparison: Stylus Rust vs Solidity
Introduction
When developing smart contracts, gas costs are a critical consideration. This article compares the gas costs of smart contracts written in Solidity and Stylus to determine which is more efficient.
You can download the sample code from the GitHub repository.
Overview
- Solidity: The most widely used programming language for writing smart contracts on the Ethereum blockchain.
- Stylus: Stylus allows you to write smart contracts in languages that compile to WASM, like Rust and C++, offering high efficiency, lower gas fees, and interoperability with Solidity on the Arbitrum network.
Testing with Computationally Intensive Functions
One common example of high computational cost in smart contracts is cryptographic operations. For this comparison, we used the keccak256
hashing function, which is known for its significant computation requirements.
This test was conducted on the Arbitrum Sepolia testnet. ( RPC: https://sepolia-rollup.arbitrum.io/rpc
Chain ID: 421614
)
Solidity Smart Contract
| Stylus Rust
|
You can build and deploy Stylus-Rust using Remix IDE with the CODE BY WELLDONE STUDIO
plugin,
and it also supports method calls.
Please refer to this document for instructions on how to use it.
Gas Consumption Results
2024-09-05 Test Result
Stylus contracts showed an average gas cost that was -49.50%
lower compared to Solidity contracts.
Solidity Contract
Stylus Contract
No | Solidity | Stylus |
---|---|---|
1 | 423,734 | 205,441 |
2 | 403,834 | 204,047 |
3 | 403,834 | 205,441 |
4 | 396,946 | 204,047 |
5 | 405,556 | 201,956 |
6 | 403,834 | 204,744 |
7 | 403,834 | 205,441 |
8 | 402,112 | 205,441 |
9 | 403,834 | 205,441 |
10 | 403,834 | 204,047 |
Avg | 405,135 | 204,605 ( -49.50 % ) |
2024-08-22 Test Result
Unexpectedly, Stylus contracts showed an average gas cost that was +25.26%
higher compared to Solidity contracts.
Solidity Contract
Stylus Contract
No | Solidity | Stylus |
---|---|---|
1 | 45,018 | 37,111 |
2 | 27,918 | 37,111 |
3 | 27,918 | 37,111 |
4 | 27,918 | 37,111 |
5 | 27,918 | 37,111 |
6 | 27,918 | 37,111 |
7 | 27,918 | 37,111 |
8 | 27,918 | 37,111 |
9 | 27,918 | 37,111 |
10 | 27,918 | 37,111 |
Avg | 29,628 | 37,111 ( +25.26% ) |
But the next day, I tested it again and found that the gas costs between the two were similar, or there were cases where Stylus had lower gas costs. Therefore, it was difficult to draw a clear conclusion.
2nd Test - Performing 1,000 keccak256 hash operations in a single call.
Assuming that the true potential of Stylus is revealed when the computational intensity reaches a certain level, I modified the code to perform keccak256
1,000 times and then tested it as follows.
Solidity Smart Contract
| Stylus Rust
|
Gas Consumption Results
2024-09-05 Test Result
That's impressive! The gas costs of Stylus have been reduced by -9.53%
compared to Solidity.
Solidity Contract
Stylus Contract
No | Solidity | Stylus |
---|---|---|
1 | 786,394 | 675,577 |
2 | 764,802 | 695,535 |
3 | 763,110 | 695,535 |
4 | 764,802 | 695,535 |
5 | 763,110 | 695,535 |
6 | 764,802 | 695,535 |
7 | 764,802 | 695,535 |
8 | 764,802 | 695,535 |
9 | 764,802 | 695,535 |
10 | 764,802 | 695,535 |
Avg | 766,623 | 693,539 ( -9.53% ) |
2024-08-22 Test Result
That's impressive! The gas costs of Stylus have been reduced by -86.3%
compared to Solidity.
Solidity Contract
Stylus Contract
No | Solidity | Stylus |
---|---|---|
1 | 412,557 | 53,311 |
2 | 396,540 | 54,397 |
3 | 396,526 | 54,403 |
4 | 396,554 | 54,385 |
5 | 396,530 | 54,381 |
6 | 396,531 | 54,379 |
7 | 396,551 | 54,402 |
8 | 396,569 | 54,440 |
9 | 396,608 | 54,450 |
10 | 396,598 | 54,458 |
Avg | 398,156 | 54,301 ( -86.4% ) |
3rd Test - Local Test Node
2024-08-22 Test Result
This test was conducted in a local node using Nitro Testnode. This was done to test in a more controlled environment and minimize the impact of external factors, which we might have encountered during testing on Arbitrum Sepolia. The test results showed that the gas savings rate was 86.6%, which was nearly identical to the results from the local test node.
No | Solidity | Stylus |
---|---|---|
1 | 412,557 | 53,343 |
2 | 395,457 | 53,343 |
3 | 395,457 | 53,343 |
4 | 395,457 | 53,343 |
5 | 395,457 | 53,343 |
6 | 395,457 | 53,343 |
7 | 395,457 | 53,343 |
8 | 395,457 | 53,343 |
9 | 395,457 | 53,343 |
10 | 395,457 | 53,343 |
Avg | 397,167 | 53,343 ( -86.6% ) |
Conclusion
The results of these tests clearly demonstrate the significant efficiency gains that can be achieved by using Stylus for smart contract development, particularly in computationally intensive scenarios. While initial tests showed that Stylus contracts might incur higher gas costs for a single keccak
operation, the true potential of Stylus becomes evident when dealing with more intensive computations, such as executing multiple keccak
operations. In this test, the fact that gas costs were reduced by up to 86.6% compared to Solidity highlights Stylus's ability to effectively optimize performance and resource utilization. This makes Stylus an attractive option for developers looking to build high-performance, cost-effective smart contracts on the Arbitrum network. As Stylus continues to evolve, it will likely become a key tool in the blockchain developer's toolkit, offering both flexibility and efficiency in contract development.
Reference
https://github.com/LimeChain/stylus-benchmark
https://github.com/OffchainLabs/stylus-hello-world
https://github.com/OffchainLabs/nitro-testnode