This can more gas optimized by adding unchecked overflow i increment. I added one more optimization in your code. // SPDX-License-Identifier: MIT pragma solidity ^0.8.10; // gas golf contract GasGolf { // start - 50908 gas // use calldata - 49163 gas // load state variables to memory - 48952 gas // short circuit - 48634 gas // loop increments - 48244 gas // cache array length - 48209 gas // load array elements to memory - 48047 gas // unchecked i increment overflow - 47645 gas uint public total; // start - not gas optimized // 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]; // } // } // } // gas optimized // [1, 2, 3, 4, 5, 100] function sumIfEvenAndLessThan99(uint[] calldata nums) external { uint _total = total; uint len = nums.length; for (uint i = 0; i < len; i=unchecked_inc(i)) { uint num = nums[i]; if (num % 2 == 0 && num < 99) { _total += num; } } total = _total; } function unchecked_inc(uint i) internal pure returns(uint){ unchecked { i++; } return i; } }
@@sirstroopwafel from the solidity 0.8.0 inbuilt checking overflow/underflow condition on arithmetic operations. In this example, there is already declaration of the variable "i" is not greater than the array length, that's why "i" will never overflow from the Uint. So we can simply bypass the overflow/underflow condition by using "unchecked" method.
Your videos are absolutely amazing. The best to do it, and there is no way I can show how much your videos have helped me grow as a Solidity dev. All the best to you and keep up the great work!!!
In C, instead of x % 2 you can do x & 1 (for odd numbers this evaluates to 1 or true), so you do a bitwise and instead of a division, but I don't know if Solidity does bit twiddling stuff though.
This can more gas optimized by adding unchecked overflow i increment. I added one more optimization in your code.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
// gas golf
contract GasGolf {
// start - 50908 gas
// use calldata - 49163 gas
// load state variables to memory - 48952 gas
// short circuit - 48634 gas
// loop increments - 48244 gas
// cache array length - 48209 gas
// load array elements to memory - 48047 gas
// unchecked i increment overflow - 47645 gas
uint public total;
// start - not gas optimized
// 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];
// }
// }
// }
// gas optimized
// [1, 2, 3, 4, 5, 100]
function sumIfEvenAndLessThan99(uint[] calldata nums) external {
uint _total = total;
uint len = nums.length;
for (uint i = 0; i < len; i=unchecked_inc(i)) {
uint num = nums[i];
if (num % 2 == 0 && num < 99) {
_total += num;
}
}
total = _total;
}
function unchecked_inc(uint i) internal pure returns(uint){
unchecked {
i++;
}
return i;
}
}
this is awesome, can you explain what does it mean to be unchecked?
@@sirstroopwafel from the solidity 0.8.0 inbuilt checking overflow/underflow condition on arithmetic operations. In this example, there is already declaration of the variable "i" is not greater than the array length, that's why "i" will never overflow from the Uint. So we can simply bypass the overflow/underflow condition by using "unchecked" method.
@@akshaytarpara5025 I see, thank you!
Awesome observation. Thanks!
Your videos are absolutely amazing. The best to do it, and there is no way I can show how much your videos have helped me grow as a Solidity dev. All the best to you and keep up the great work!!!
Hahh! I watched all 62 videos. You are the best!
this channel is definitely a public good.
You're genius at Solidity. Best courses I've ever seen so far. Thank you.
you help me growing up as a solidity dev, thanks you are the real MVP
Another technique is to disable safemath by using unchecked. For example the variable i will never overflow.
Thank you very much for all that knowledge!
Thank you for your great efforts.
A real Engineer!
Great video, thanks!
even more gas can be saved by using "continue" in the for loop
neat as always
As awesome as alwasy!!! 😍
so damn good
Thank you 💕
good trick!
thank you for your technique
hey man, great video !!
great content dude!
🤙👍
In C, instead of x % 2 you can do x & 1 (for odd numbers this evaluates to 1 or true), so you do a bitwise and instead of a division, but I don't know if Solidity does bit twiddling stuff though.
great vid! I'm another wannabe optimizoor! by the way video recommendation: writing assembly? if you don't already have one! keep up the good work!
Awasome dude (y)
awesome as always.
type of allocation into the functions are STACK, and put data in air. true?
put data in air? what that does mean?
@@smartcontractprogrammer like memory
👍 why ++i and not i++ here?
Does anyone know why it is called golf?
Golf - Lower score is better
Gas - Lower gas is better
@@tskn6547 Oh, makes sense now. Thank you!
Why did you used ++i instead of i++ in the for loop?
less gas
@@smartcontractprogrammer I still dont konw why ‘++i’ less gas than 'i+=1'
So we are sacrificing readability over gas efficiency...
That's a bit sad :(
Yeah it's a balancing act
@@smartcontractprogrammer Maybe in the future there will be compiler to automatically transform it into the most gas efficient code!