108

Apart from the public modifier Ethereum introduces the external one. Both may be called outside of the contract and inside (the later one by this.f() pattern). Moreover, according to the docs:

External functions are sometimes more efficient when they receive large arrays of data.

but there is no further information what does actually sometimes mean and if this efficiency gain is held also for internal calls.

What are the best practices of using external vs public keyword? Are there any patterns or recommendations?

174

A simple example demonstrating this effect looks like this:

pragma solidity^0.4.12;

contract Test {
    function test(uint[20] a) public returns (uint){
         return a[10]*2;
    }

    function test2(uint[20] a) external returns (uint){
         return a[10]*2;
    }
}

Calling each function, we can see that the public function uses 496 gas, while the external function uses only 261.

The difference is because in public functions, Solidity immediately copies array arguments to memory, while external functions can read directly from calldata. Memory allocation is expensive, whereas reading from calldata is cheap.

The reason that public functions need to write all of the arguments to memory is that public functions may be called internally, which is actually an entirely different process than external calls. Internal calls are executed via jumps in the code, and array arguments are passed internally by pointers to memory. Thus, when the compiler generates the code for an internal function, that function expects its arguments to be located in memory.

For external functions, the compiler doesn't need to allow internal calls, and so it allows arguments to be read directly from calldata, saving the copying step.

As for best practices, you should use external if you expect that the function will only ever be called externally, and use public if you need to call the function internally. It almost never makes sense to use the this.f() pattern, as this requires a real CALL to be executed, which is expensive. Also, passing arrays via this method would be far more expensive than passing them internally.

You will essentially see performance benefits with external any time you are only calling a function externally, and passing in large arrays.

Examples to differentiate:

public - all can access

external - Cannot be accessed internally, only externally

internal - only this contract and contracts deriving from it can access

private - can be accessed only from this contract

  • 8
    Excellent and a very helpful answer. Thx Tjaden! – Jakub Wojciechowski Jul 4 '17 at 13:48
  • 6
    If we are designing a public interface for contracts and other people will rely on the interface (think EIPs), then should we always be using external? – William Entriken Jan 20 '18 at 5:56
  • 2
    ^ This should be added to the Solidity docs. – Justin May 15 '18 at 16:53
  • 2
    Something wrong with your answer: joxi.ru/Drlz51Xc4MDGX2 – Anton Pegov May 16 '18 at 14:13
  • 1
    @Anron Pegov I am reporting the execution gas cost, not the total. i.e. only the gas cost incurred by actually running the contract, not sending the transaction to it – Tjaden Hess Mar 13 at 13:08
5

Restructuring the answer above for clarity:

pragma solidity^0.4.12;

contract Test {

    /*
    Cost: 496 Gas 
    This can be called internally or externally
    Since internal calls expects function arguements to be allocated to memory, solidity immediately
    copies array arguments to memory (This is what cost the additional gas.) 
    */
    function test(uint[20] a) public returns (uint) {
        return a[10] * 2;
    }

    /*
    Cost: Gas 261
    Doesnt allow internal calls, read directly from CALLDATA saving on the copying step(memory allocation).
    */
    function test(uint[20] a) external returns (uint) {
        return a[10] * 2;
    }


    /*
     Executed via JUMPs in code, array arguments are passed internally by pointers to memmory
      Function expects argument to be located in memory. 
     */
    function test(uint[20] a) internal returns (uint) {
        return a[10] * 2;
    }
}
  • Internal calls are the cheapest, since its executed via code JUMP, passing pointers to memory.
  • Internal calls for public functions are expensive because internal function calls expects the arguments to be allocated to memory, since the public function does not know if the invocation is external or internal, it copies the arguments to memory and hence is more expensive
  • If you know the function is only going to be called externally use external

  • It almost never makes sense to use the this.f() pattern, as this requires a real CALL to be executed, which is expensive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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