solidity – Why Gas Prices Vary According to State Visibility in Solidity

evmsolidityvisibility

Why gas prices vary according to state visibility(public/external/private/internal) to Function?

Best Answer

In Ethereum, transactions cost gas and hence ether. The gas consumption of a transaction depends on the opcodes that the EVM has to execute. So it's all about the opcode being used.

More opcode, more gas used (some opcode are really expensive because they are computationally heavy)

For exemple, using visibility external for the functions only accessed externally forces to use calldata as the parameter location and this saves some gas when the function executes.

You also have this great twitter thread about forge inspect @CONTRACT ir-optimized that will help you visualize what opcode are being used by your code https://twitter.com/w1nt3r_eth/status/1579486967963693057

Hope this helps

Related Topic