I have been trying to save some energy cost in a smart contract by declaring all uint's in the structs with just the maximal size they could possibly need but that makes it more expensive to use the contract than if I declare them all as uint256, does anyone have any idea why? I read everywhere that it should make it cheaper to use the contract but in reality it seems to be the other way

Nov 19, 2020, 11:40 AM
uint256 is the cheapest solution
in fact uint8 is expensive than 256
Nov 19, 2020, 12:00 PM
Thx, then it's not just me
Nov 19, 2020, 12:05 PM
Why would this be? It takes more space to store. The difference comes with tight variable packing when smaller types become cheaper
Nov 19, 2020, 12:21 PM
I read this here:
https://ethereum.stackexchange.com/questions/3067/why-does-uint8-cost-more-gas-than-uint256
Nov 19, 2020, 12:22 PM
Well that answer is 4 years old, which is an eternity in Ethereum. And other answers state that this is no longer the case, and furthermore variable packing basically makes it the opposite (if used correctly)
and typically the deployment cost is not as important as usage cost
Nov 19, 2020, 12:32 PM
Good to know it
Nov 19, 2020, 12:34 PM
It looks like it is more important with the order the global variables is declared in. I changed the order of some of the variables and there is a tiny difference in the cost to use the contract
Any way to tell in what order it is best to declare the global variables?
Nov 19, 2020, 2:15 PM
https://fravoll.github.io/solidity-patterns/tight_variable_packing.html
Nov 19, 2020, 2:18 PM

© 2024 Draquery.com All rights reserved.