Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shingonu/b56b1ae5fe52cb74c2b8a96499a837c5 to your computer and use it in GitHub Desktop.
Save shingonu/b56b1ae5fe52cb74c2b8a96499a837c5 to your computer and use it in GitHub Desktop.

Reference

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.

public 함수는 496 gas를 소모하고, external 함수는 261 gas만을 소모한다. 함수가 public인지 external인지에 따라 gas 차이가 있다.

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.

gas양이 다른 이유는 public 함수에서 Solidity가 즉시 배열 인수를 memory에 복사하는 반면 external 함수는 calldata에서 직접 읽을 수 있기 때문입니다. 메모리 할당은 비싸지 만 calldata에서 읽기는 저렴합니다.

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.

public 함수가 모든 argument를 memory에 write하는 이유는 public function은 내부적으로 호출될 수 있기 때문이다. 외부에서 함수를 호출하는 것과 내부에서 함수를 호출하는 것은 완전히 다르게 동작한다. 내부적으로 함수를 호출하면 코드상에서 jump를 통해 이루어진다. 그리고 array arguments들은 pointer에 의해 memory로 보내진다. 따라서 public function은 컴파일될 때 internal function을 위한 코드를 생성한다.

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.

external 함수는 internal call을 생각할 필요없기 때문에 argument를 메모리가 아닌 calldata에만 저장해둔다.

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.

만약 외부에서만 함수가 호출이 된다면 external 함수로 선언해야 한다. 만약 내부적으로 함수가 호출이 된다면 public으로 선언해야 한다. this.f () 패턴을 사용해서 external 함수를 호출할 수는 있지만, 실제 CALL을 실행해야하므로 비용이 많이 든다. 또한, 이 메소드를 통해 배열을 전달하는 것은 내부적으로 전달하는 것보다 훨씬 비싸다.

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

따라서 외부에서만 함수가 호출된다면 external 키워드를 사용해야 하고 large array를 argument로 보낼 때도 역시 external 키워드를 쓰는 것이 좋다. 왜냐하면 public 함수는 large array를 메모리에 copy해야 하기 때문에 훨씬 비싸다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment