Skip to content

Instantly share code, notes, and snippets.

@IllIllI000
Created June 1, 2022 22:21
Show Gist options
  • Save IllIllI000/a5d8b486a8259f9f77891a919febd1a9 to your computer and use it in GitHub Desktop.
Save IllIllI000/a5d8b486a8259f9f77891a919febd1a9 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
/**
* @title OptimizeName
* @author IllIllI
*/
/*
Function hashes sorted:
"0000eb74": "changeNonce_$g5()"
"0000f995": "incrementNonce_Cbn()"
"002f679e": "alterNonce()"
"0feca68a": "updateNonce()"
"173e7703": "changeNonce()"
"627cdcb9": "incrementNonce()"
"8d13f33c": "invalidateOldNonce()"
"b4c70052": "invalidateOldNonces()"
"c53a0292": "increaseNonce()"
Gas costs sorted:
"329": "changeNonce()"
"330": "changeNonce_$g5()"
"351": "incrementNonce()"
"352": "incrementNonce_Cbn()"
"373": "invalidateOldNonce()"
"374": "alterNonce()"
"395": "invalidateOldNonces()"
"396": "updateNonce()"
"417": "increaseNonce()"
Byte code snipit:
CALLDATALOAD PUSH1 0xE0 SHR DUP1 PUSH4 0x173E7703 GT PUSH1 0x5E JUMPI
DUP1 PUSH4 0x173E7703 EQ PUSH1 0x87 JUMPI
DUP1 PUSH4 0x627CDCB9 EQ PUSH1 0x87 JUMPI
DUP1 PUSH4 0x8D13F33C EQ PUSH1 0x87 JUMPI
DUP1 PUSH4 0xB4C70052 EQ PUSH1 0x87 JUMPI
DUP1 PUSH4 0xC53A0292 EQ PUSH1 0x87 JUMPI
PUSH1 0x0 DUP1 REVERT JUMPDEST
DUP1 PUSH2 0xEB74 EQ PUSH1 0x87 JUMPI
DUP1 PUSH2 0xF995 EQ PUSH1 0x87 JUMPI
DUP1 PUSH3 0x2F679E EQ PUSH1 0x87 JUMPI
DUP1 PUSH4 0xFECA68A EQ PUSH1 0x87 JUMPI
The compiler is sorting the method IDs, choosing the ones that do not
have leading zeroes first, and interleaving the ones with leading zeroes
between them. These two factors together can be used to come up
with contract interfaces that give the most-frequently-called
functions lower gas costs
References:
https://medium.com/joyso/solidity-how-does-function-name-affect-gas-consumption-in-smart-contract-47d270d8ac92
https://github.com/enzosv/solidity-optimize-name
*/
contract OptimizeName {
function incrementNonce() external returns (uint256 newNonce) { return 0; }
function increaseNonce() external returns (uint256 newNonce) { return 0; }
function changeNonce() external returns (uint256 newNonce) { return 0; }
function alterNonce() external returns (uint256 newNonce) { return 0; }
function updateNonce() external returns (uint256 newNonce) { return 0; }
function invalidateOldNonce() external returns (uint256 newNonce) { return 0; }
function invalidateOldNonces() external returns (uint256 newNonce) { return 0; }
function incrementNonce_Cbn() external returns (uint256 newNonce) { return 0; }
function changeNonce_$g5() external returns (uint256 newNonce) { return 0; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment