ethereum / solidity

Solidity, the Smart Contract Programming Language
https://soliditylang.org
GNU General Public License v3.0
22.66k stars 5.62k forks source link

Support for Inlining #15160

Closed zemse closed 1 month ago

zemse commented 1 month ago

Abstract

Currently, Solidity compiles functions into code sections and then JUMP instruction is used to go into the function and return to the parent code section.

If functions can be declared to be inlined, then that can enable runtime gas optimizations at the cost of bytecode size. Solidity could do this automatically, or allow users to explicitly mark a function that should be inlined. Small function logic makes sense to be inlined.

Examples of this functionality in other languages:

Rust Inlining Inlining - The Rust Performance Book Huff Macros Huff by Example | Huff Language

Specification

Introduce a inline keyword.

function add(uint a, uint b) internal view inline returns (uint) {
  return a + b;
}

It would also be great if inlining is possible for yul code.

Backwards Compatibility

None

nikola-matic commented 1 month ago

We already do this automatically: https://github.com/ethereum/solidity/blob/develop/libevmasm/Inliner.h#L20 (evmasm) https://github.com/ethereum/solidity/blob/develop/libyul/optimiser/ExpressionInliner.h#L35-L46 (yul)

Adding an actual keyword is not at the top of the list of priorities honestly. Ultimately, in most language that have this, the inline keyword is really just a suggestion to the compiler that it should if possible inline, instead of that it must inline.

Also, we have to be especially careful to strike a fine balance between optimizing for runtime performance (skip jump instructions) versus optimizing for code size, i.e. deploy time code, which the inliner steps already do.

cameel commented 1 month ago

Related: #12782.