# An Introduction to Ether Solidity Gas Optimization **Published by:** [Leek DEV](https://paragraph.com/@leekdev/) **Published on:** 2023-07-13 **URL:** https://paragraph.com/@leekdev/an-introduction-to-ether-solidity-gas-optimization ## Content Now there are many ways to write contracts to save Gas, here is a good case found more step-by-step, you can refer to. Case Study A method to calculate the sum of even numbers in an input array and record the result after the calculation. The inputs are constant [12, 3, 4, 5, 3, 44, 2, 12, 3, 4, 5, 21, 46, 1, 2, 12]. 1.Initial Code not optimized uint public total function sumIfEvenAndLessThan99(uint[] memory nums) external { for (uint i = 0; i < nums.length; i += 1) { bool isEven = nums[i] % 2 == 0; bool isLessThan99 = nums[i] < 99; if (isEven && isLessThan99) { total += nums[i]; } } }; 2.Read-only parameters use calldata Solidity variables memory, calldata 2 represent a very similar role, are temporary variables within the function, their biggest difference is that calldata is not modifiable, in some read-only case save Gas. uint public total; function sumIfEvenAndLessThan99(uint[] calldata nums) external { for (uint i = 0; i < nums.length; i += 1) { bool isEven = nums[i] % 2 == 0; bool isLessThan99 = nums[i] < 99; if (isEven && isLessThan99) { total += nums[i]; } } } 3.Copy high-frequency read and write parameters to the function internals Solidity function is also similar to the stack to structure, read and write variables inside the function to save Gas than read and write external variables, in some need high frequency read and write scenarios will be a good way to copy the function external variables to the function internal operation. uint public total; function sumIfEvenAndLessThan99(uint[] calldata nums) external { uint _total = total; for (uint i = 0; i < nums.length; i += 1) { bool isEven = nums[i] % 2 == 0; bool isLessThan99 = nums[i] < 99; if (isEven && isLessThan99) { _total += nums[i]; } } total = _total; } 4.Reducing the number of variable declarations This seems to be related to c++ language features uint public total; function sumIfEvenAndLessThan99(uint[] calldata nums) external { uint _total = total; for (uint i = 0; i < nums.length; i += 1) { if (nums[i] % 2 == 0 && nums[i] < 99) { _total += nums[i]; } } total = _total; } 5.Special self-increasing optimization The memory declared by Solidity is to be counted as Gas, and there are times when it is appropriate to reduce the number of internally declared variables. It is true that `isEven` and `isLessThan99` variables are declared inside the loop, and these variables are only used to make a conditional judgment, so it is obvious that they can be combined to reduce the number of internally declared variables. uint public total; function sumIfEvenAndLessThan99(uint[] calldata nums) external { uint _total = total; for (uint i = 0; i < nums.length; ++i) { if (nums[i] % 2 == 0 && nums[i] < 99) { _total += nums[i]; } } total = _total; } 6.Internal function that copies the parameters read by HF The array length `nums.length` is read every loop with the array loop variable `nums[i]`, and is read from the argument, which can be copied into memory to save more gas. uint public total; function sumIfEvenAndLessThan99(uint[] calldata nums) external { uint _total = total; uint len = nums.length; for (uint i = 0; i < len; ++i) { uint num = nums[i]; if (num % 2 == 0 && num < 99) { _total += num; } } total = _total; } 7.Cancel overflow check Solidity 8.0 and later will do a benefit check on the numbers by default, which will consume a certain amount of Gas by default, and can be unchecked by adding `Unchecked` to save a lot of Gas in some cases. function sumIfEvenAndLessThan99(uint256[] calldata nums) external { uint256 _total = total; uint256 len = nums.length; for (uint256 i = 0; i < len; ) { uint256 num = nums[i]; if (num % 2 == 0 && num < 99) { unchecked { _total += num; } } unchecked { ++i; } } unchecked { total = _total; } } Summary The gas savings after each modification compared to the beginning were calculated. References https://solidity-by-example.org/gas-golf/ Subscribe ## Publication Information - [Leek DEV](https://paragraph.com/@leekdev/): Publication homepage - [All Posts](https://paragraph.com/@leekdev/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@leekdev): Subscribe to updates - [Twitter](https://twitter.com/LeekDEV): Follow on Twitter