Solidity Gas Efficiency Unleashed: Revert with Error vs Revert with String — A Deep Dive
Revert with finesse: Maximize your gas savings with this simple trick!
Hi there, fellow DeFi enthusiasts! Buckle up for another exciting article that will have you rethinking your Solidity code for maximum gas efficiency.
This time, we’ll explore the fascinating world of revert with error messages and dive into the specifics of why it’s better to use revert with error instead of revert with a string.
My experience at Frak, a DeFi protocol, has given me a front-row seat to the challenges and opportunities that come with optimizing smart contracts for efficiency.
So, what’s in store for you? We’ll take a closer look at revert with error in assembly, and even discuss the low EVM implication, contract size implication, and why it’s more efficient even if the transaction doesn’t revert. And, of course, we’ll provide you with some juicy code snippets to compare and contrast the different techniques.
Ready to make your smart contracts leaner and meaner? Let’s dive in!
Revert with String
In the early days of Solidity, developers used the
revert function with a string parameter to handle errors. This provided a human-readable error message when things went haywire. Feast your eyes on this code snippet of a simple function that reverts with a string:
As nifty as descriptive error messages can be, this method is a gas glutton. But fear not, for there is a better way!
Revert with Error
Enter Solidity 0.8.4, armed with custom errors and a thirst for gas efficiency.
By opting for
revert with an error instead of a string, we can keep the advantages of expressive error messages without draining our gas reserves. Check out the same function as before, but now with a revert error:
Revert with Error in assembly
Finally, we have the king of gas efficiency, revert with an error code in assembly. This method is the most gas-efficient, but it’s also the most complex. Use it wisely!
Why the Gas Differences?
Now that we’ve seen the magic tricks, let’s dive into the “why.” There are three primary reasons for the gas differences between the three methods:
- Low EVM Implication: When you revert with a string, the EVM has to store the string in memory, which consumes more gas than simply using an error code. In contrast, error codes are more efficient for the EVM, resulting in lower gas consumption.
- Contract Size Implication: Contracts with larger strings take up more space on the Ethereum blockchain, resulting in a larger contract size. A larger contract size means more gas is consumed for deployment and execution. Using error codes instead of strings keeps your contract size smaller and more efficient.
- Efficiency Even Without Revert: Even if your transaction doesn’t revert, using error codes over strings is still more efficient. The EVM has to parse the error string, regardless of whether the transaction reverts or not. By using error codes, you’re saving gas on every transaction!
To showcase the gas efficiency of revert with error over revert with string, let’s examine a benchmark comparison of the three code snippets:
In this benchmark, you can clearly see the differences in contract size and gas consumption between the three methods. The gas savings are substantial, especially when using revert with an error in assembly.
You can find the snippet and the gas usage test here, test done with foundry.
Kudos! You’ve now unlocked the secret to optimizing gas efficiency in Solidity by choosing revert with error over revert with string. Small changes like these can have a significant impact on your DeFi protocols, just like we did at Frak.
If this article fueled your passion for gas efficiency, don’t hesitate to give it a clap, share it, and follow me for more witty and engaging articles on Solidity and the DeFi universe.
Our journey into the world of DeFi is far from over! Stay tuned, and together, we’ll continue to explore the fascinating realm of decentralized finance. Let the adventure continue!