In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "what are the Solidity code patterns that can save Gas costs". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the Solidity code patterns to save Gas costs?"
On the eTaifang block chain, Gas is used to compensate miners for the computing power provided by the storage and execution of smart contracts. At present, the use of Ethernet Fong is gradually increasing, and the cost of transaction fees is also on the rise-the daily cost of gas is now as high as millions of dollars. With the expansion of the ethernet ecosystem, Solidity intelligent contract developers also need to pay attention to the optimization of gas utilization.
1. Use short-circuit mode to sort Solidity operations
Short-circuiting is a solidity contract development model that uses or / and logic to sort different cost operations. It puts low gas cost operations first and high gas cost operations behind, so that if the previous low cost operations are feasible, you can skip the high cost Ethernet Square virtual machine operations behind (short circuit).
/ / f (x) is the operation with low gas cost / / g (y) is the operation with high gas cost / / the operation f (x) with different gas costs is sorted as follows | g (y) f (x) & & g (y) 2. Delete unnecessary Solidity libraries
When developing Solidity smart contracts, the libraries we introduce usually only need to use some of these functions, which means that they may contain a lot of solidity code that is actually redundant for your smart contracts. If you can safely and effectively implement the dependent library functions in your own contract, you can achieve the goal of optimizing the gas utilization of solidity contracts.
For example, in the following solidity code, our Ethernet Square contract only uses the add method of the SafeMath library:
Import'. / SafeMath.sol' as SafeMath;contract SafeAddition {function safeAdd (uint a, uint b) public pure returns (uint) {return SafeMath.add (a, b);}}
By referring to the implementation of this part of the code in SafeMath, you can remove the dependency on this solidity library:
Contract SafeAddition {function safeAdd (uint a, uint b) public pure returns (uint) {uint c = a + b; require (c > = a, "Addition overflow"); return c;}} 3, explicitly declare the visibility of the Solidity contract function
In Solidity contract development, explicitly declaring the visibility of functions can not only improve the security of smart contracts, but also help to optimize the gas cost of contract execution. For example, by explicitly marking the function as an external function (External), you can force the storage location of the function parameters to be set to calldata, which saves the Ethernet Square gas cost each time the function is executed.
4. Use the correct Solidity data type
In Solidity, some data types are more expensive to gas than others. It is necessary to understand the gas utilization of available data types in order to choose the one that is most efficient according to your needs. Here are some rules about gas consumption of the solidity data type:
Do not use uint types in any case where string types can be used
The cost of storing uint256 is lower than the gas of storing uint8. Why? Click here to view the original text
When the bytes type is available, do not use the byte [] type in the solidity contract
If the length of bytes has a predictable upper limit, use solidity types with fixed length such as bytes1~bytes32 as far as possible
The gas cost required by bytes32 is lower than that of string type
5. Avoid dead code in Solidity intelligent contract
Dead code (Dead code) refers to Solidity code that will never be executed, such as those whose execution conditions can never be met, just like the Solidity code blocks in the following two contradictory conditional judgments, which consume ethernet gas resources but have no effect:
Function deadCode (uint x) public pure {if (x
< 1) { if(x >2) {return x;} 6. Avoid using unnecessary conditional judgment
The results of some conditional assertions can be understood without the execution of Solidity code, so such conditional judgment can be simplified. For example, in the following two-level judgment condition in the Solidity contract code, the outermost judgment is a waste of valuable Ethernet Square gas resources:
Function opaquePredicate (uint x) public pure {if (x < 1) {if (x < 0) {return x;} 7, avoid expensive gas operations in the loop
Because of the high cost of SLOAD and SSTORE opcodes, the gas cost of managing storage variables is much higher than memory variables, so avoid manipulating storage variables in loops. For example, in the following solidity code, the num variable is a storage variable, then several operations with unknown loop times are likely to cause the ethernet square gas to consume a black hole that solidity developers did not expect:
Uint num = 0 function expensiveLoop (uint x) public {for (uint I = 0; I < x; iTunes +) {num + = 1;}}
The solution to the above anti-pattern Taifang contract code is to create a solidity temporary variable to replace the above global variable to participate in the loop, and then re-assign the value of the temporary variable to the global variable at the end of the loop:
Uint num = 0 Solidity function lessExpensiveLoop (uint x) public {uint temp = num; for (uint I = 0; I < x; iTunes +) {temp + = 1;} num = temp;} 8, avoid using Solidity loops for predictable results
If the result of a loop calculation can be predicted without compiling and executing Solidity code, do not use loops, which can lead to significant gas savings. For example, the following Ethernet Fong contract code can directly set the value of the num variable:
Function constantOutcome () public pure returns (uint) {uint num = 0; for (uint I = 0; I < 100; iTunes +) {num + = 1;} return num;} 9, circular merge mode
Sometimes in a Solidity smart contract, you will find that the judgment conditions of the two loops are the same, so there is no reason not to merge them in this case. For example, the following Etay Fong contract code:
Function loopFusion (uint x, uint y) public pure returns (uint) {for (uint I = 0; I < 100; iBI +) {x + = 1;} for (uint I = 0; I < 100; iBI +) {y + = 1;} return x + y;} 10, avoid repeated calculations in the loop
If a Solidity expression in a loop produces the same result at each iteration, it can be moved out of the loop and evaluated first, saving additional gas costs in the loop. This is even more important if the variable used in the expression is the storage variable. For example, the value of the expression aqb in the smart contract code below does not need to be recalculated each iteration:
Uint a = 4 uint b = 5 X function repeatedComputations (uint x) public returns (uint) {uint sum = 0; for (uint I = 0; I)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.