Let's say we have the following payable method defined in a contract:
function foo() public payable {
if (msg.value >= thresholdValue0) {
// some logic..
} else if (msg.value < thresholdValue0 && msg.value >= thresholdValue1) {
// some logic..
} else if (msg.value < thresholdValue1) {
// some logic..
}
}
Notice how msg.value
is being used more than once (3 times, to be precise). I'm not sure if that would cost 3 times as much as the cost of a single msg.value
read, and whether it would be more efficient to load it into memory first as follows:
function foo() public payable {
uint256 msgValue = msg.value;
if (msgValue >= thresholdValue0) {
// some logic..
} else if (msgValue < thresholdValue0 && msgValue >= thresholdValue1) {
// some logic..
} else if (msgValue < thresholdValue1) {
// some logic..
}
}