Skip to content

Add function selector optimization #5319

Closed
@fulldecent

Description

@fulldecent

Test case

Compiler: 0.4.25+commit.59dbf8f1.Emscripten.clang
Optimization: on with default settings in Remix IDE

pragma solidity ^0.4.23;

contract Test
{
    function a() payable returns (address) {
        return address(this);
    }
}

608060405234801561001057600080fd5b5060c08061001f6000396000f300608060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680630dbe671f146044575b600080fd5b604a608c565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6000309050905600a165627a7a72305820effc577e00e0dab11af3a8f067f6755dd66ef9191ab577e8ea00920b04f8429e0029

Opcodes | Loc | Op | Data | | ---- | ------------ | ------------------------------------------------------------ | | 0 | PUSH1 | 80 | | 2 | PUSH1 | 40 | | 4 | MSTORE | | | 5 | PUSH1 | 04 | | 7 | CALLDATASIZE | | | 8 | LT | | | 9 | PUSH1 | 3f | | 11 | JUMPI | | | 12 | PUSH1 | 00 | | 14 | CALLDATALOAD | | | 15 | PUSH29 | 0100000000000000000000000000000000000000000000000000000000 | | 45 | SWAP1 | | | 46 | DIV | | | 47 | PUSH4 | ffffffff | | 52 | AND | | | 53 | DUP1 | | | 54 | PUSH4 | 0dbe671f | | 59 | EQ | | | 60 | PUSH1 | 44 | | 62 | JUMPI | | | 63 | JUMPDEST | | | 64 | PUSH1 | 00 | | 66 | DUP1 | | | 67 | REVERT | | | 68 | JUMPDEST | | | 69 | PUSH1 | 4a | | 71 | PUSH1 | 8c | | 73 | JUMP | | | 74 | JUMPDEST | | | 75 | PUSH1 | 40 | | 77 | MLOAD | | | 78 | DUP1 | | | 79 | DUP3 | | | 80 | PUSH20 | ffffffffffffffffffffffffffffffffffffffff | | 101 | AND | | | 102 | PUSH20 | ffffffffffffffffffffffffffffffffffffffff | | 123 | AND | | | 124 | DUP2 | | | 125 | MSTORE | | | 126 | PUSH1 | 20 | | 128 | ADD | | | 129 | SWAP2 | | | 130 | POP | | | 131 | POP | | | 132 | PUSH1 | 40 | | 134 | MLOAD | | | 135 | DUP1 | | | 136 | SWAP2 | | | 137 | SUB | | | 138 | SWAP1 | | | 139 | RETURN | | | 140 | JUMPDEST | | | 141 | PUSH1 | 00 | | 143 | ADDRESS | | | 144 | SWAP1 | | | 145 | POP | | | 146 | SWAP1 | | | 147 | JUMP | | | 148 | STOP | | | 149 | LOG1 | | | 150 | PUSH6 | 627a7a723058 | | 157 | SHA3 | | | 158 | INVALID | | | 159 | INVALID | | | 160 | JUMPI | | | 161 | PUSH31 | 00e0dab11af3a8f067f6755dd66ef9191ab577e8ea00920b04f8429e0029 |

Subjects

Check function signature

Currently this uses:

Loc Op Data
12 PUSH1 00
14 CALLDATALOAD
15 PUSH29 0100000000000000000000000000000000000000000000000000000000
45 SWAP1
46 DIV
47 PUSH4 ffffffff
52 AND

Optimizations

Function selector

Because words are 32 bytes, a divide by 0100000000000000000000000000000000000000000000000000000000 need not be followed by an and with ffffffff.

Maybe this case is an over optimization. But function selectors apply to every call of every contract. So they should be airtight.

New version:

Loc Op Data
12 PUSH1 00
14 CALLDATALOAD
15 PUSH29 0100000000000000000000000000000000000000000000000000000000
45 SWAP1
46 DIV

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions