Home Crypto Solidity — Gas Optimization

Solidity — Gas Optimization

0
Solidity — Gas Optimization

[ad_1]

Today we’ll go through a couple different scenarios that gas savings can be applied to.

In todays age we need to make sure we’re saving on gas wherever possible!

Inefficient code

The biggest error here is that we are continuously accessing the storage variable. The SLOAD assembly function is one of the most expensive assembly functions right beside CREATE.

Lets make some changes.

By declaring the variable within our function we defined a variable in memory & then called storage to pull the value from x into y, we used y multiple times & then set x as the total.

Neat! A gas saving of 395 gas!

Okay but how can we improve this one even more?

A uint256 can hold (2²⁵⁶)-1 as the highest possible number. This guarantees that we won’t experience a overflow or underflow problem here since our counter will only be incrementing 5 times.

Because we know we won’t experience these problems we can remove the safe math wrapper to make sure we aren’t spending unnecessary gas.

Another saving of 615 gas!

More to come xD

If you found this interesting why not check out this!
https://medium.com/p/28a8bb064e86

Solidity Dev Study Group — https://discord.gg/KzbcGmrnfN

-Polygon Alliance — https://www.polygonalliance.com/

-Polygon Alliance Discord — https://discord.gg/kJKPCGQu66

Did you enjoy this article?
Feel like buying me a cup of coffee?
Polygon/Eth/Bsc — 0x4A581E0eaf6b71D05905e8E6014dc0277A1B10ad

New to trading? Try crypto trading bots or copy trading on best crypto exchanges

Join Coinmonks Telegram Channel and Youtube Channel get daily Crypto News

[ad_2]

Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here