transmissions11/solmate

Changing decimals from uint8 to uint256 for better packing?

svskaushik opened this issue · 2 comments

Disclaimer: I'm relatively new around some of this stuff, so kindly ignore/correct me if I'm just babbling nonsense.

I came across ERC20.sol and was trying to mess around with the variable packing when I found this:

Changing uint8 public immutable decimals to uint256 public immutable decimals here
and
Changing uint8 _decimals to uint256 _decimals here
results in a drop in deployment execution gas from 1266504 gas to 1260272 gas, a delta of 6232 (gas numbers are from remix, btw)

I suppose it's the better packing enabled by uint256 compared to uint8 (unless it's something else entirely), is this something useful or am I way off here?

since the variable is immutable its not cuz of packing (it gets directed straight into the bytecode)

i believe its cuz changing the constructor argument to use a full word size type means the compiler doesn't have to clean the higher order bits of the calldata for safety. for readability sakes and considering how little gas is saved on deployment would rather keep as is, but thanks for taking a look!

Ah, got it, that does make sense. Thanks for explaining it!