0

In the transferFrom() function, msg.sender is used to check the allowance and change it three times

Wouldn't it be better to set msg.sender once in a variable called _msgSender and then use _msgSender instead of repeating msg.sender?

    function transferFrom(address from, address to, uint value) external returns (bool) {
        if (allowance[from][msg.sender] != uint(-1)) {
            allowance[from][msg.sender] = allowance[from][msg.sender].sub(value);
        }
        _transfer(from, to, value);
        return true;
    }

my suggestion :


    function transferFrom(address from, address to, uint value) external returns (bool) {
        address _msgSender = msg.sender;
        if (allowance[from][_msgSender] != uint(-1)) {
            allowance[from][_msgSender] = allowance[from][_msgSender].sub(value);
        }
        _transfer(from, to, value);
        return true;
    }

1

1 Answer 1

2

The CALLER operation which is reading the msg.sender global variable costs 2.

While MLOAD/MSTORE operations which are memory operations (storage where local variables are stored) costs 3.

https://stackoverflow.com/questions/64854256/how-much-accessing-msg-sender-costs-is-it-useful-to-store-it-in-a-variable-and

thanks to kfx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.