You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Solidity 0.6.5
introduced immutable as a major feature. It allows setting
contract-level variables at construction time which gets stored in code
rather than storage.
Consider the following generic example:
contractC {
/// The owner is set during contruction time, and never changed afterwards.addresspublic owner =msg.sender;
}
In the above example, each call to the function owner() reads from
storage, using a sload. After EIP-2929, this costs 2100 gas
cold or 100 gas warm. However, the following snippet is more gas
efficient:
contractC {
/// The owner is set during contruction time, and never changed afterwards.addresspublic immutable owner =msg.sender;
}
In the above example, each storage read of the owner state variable is
replaced by the instruction push32 value, where value is set during
contract construction time. Unlike the last example, this costs only 3
gas.
This is a good improvement that will have some nice gas savings considering how often we access these variables. Which should probably be hardcoded rather than constructor arguments anyways.
Handle
hrkrshnn
Vulnerability details
State Variables that can be changed to
immutable
Solidity 0.6.5
introduced
immutable
as a major feature. It allows settingcontract-level variables at construction time which gets stored in code
rather than storage.
Consider the following generic example:
In the above example, each call to the function
owner()
reads fromstorage, using a
sload
. AfterEIP-2929, this costs 2100 gas
cold or 100 gas warm. However, the following snippet is more gas
efficient:
In the above example, each storage read of the
owner
state variable isreplaced by the instruction
push32 value
, wherevalue
is set duringcontract construction time. Unlike the last example, this costs only 3
gas.
Examples
The text was updated successfully, but these errors were encountered: