My question is: Should I use uint40 for time in Solidity?
All the examples I've seen use uint256 for time, however, I think it's too wasteful -- while using uint32 is maybe a little aggressive due to its 86-year-till-overflow, the 34K-year-till-overflow from uint40 seems pretty safe to me.
Could there be any potential downfall from using uint40 that I'm not aware of?
Edit:
Sorry for not making myself clear enough earlier. Consider the following struct --
struct User {
address user; // 20 bytes
uint40 userRegisterTime; // 5 bytes
uint40 userDepositTime; // 5 bytes
uint256 userBalanceWei; // 32 bytes
}
Had the two time vars been defined as uint256, the struct would need 4 * 32-byte, while in the case of uint40, the first three vars can be packed into one 32-byte, saving 2 * 32-byte (reference, which I have verified -- https://medium.com/@novablitz/storing-structs-is-costing-you-gas-774da988895e )
Everybody agrees?
time * 2will be stored intouint40, while the result might be larger than 40 bits. For this purpose only, it is already better to just keep all your stuff inuint256s. As to gas cost, keeping all your data aligned to 256 bits, will actually yield lower gas-cost. – goodvibration Jun 02 '19 at 09:54