[컴][go] golang 의 interface{}(interface type) 의 itable 이 어떻게 생성될까.

golang itable  / how to generate itable in go / go virtual function table /

Go의 itable 의 생성

interface{}(interface type) 에 대한 itable 이 어떻게 생성되는지에 대한 설명이다.

Go 의 dynamic type conversion 들은 compiler 또는 linker 가 미리 모든 가능한 itable 들을 미리계산하는 것이 합리적이지 않다는 뜻이다.

너무 많은 (interface type, concrete type) pair 들이 존재한다. 그리고 대다수는 필요하지 않을 것이다. 대신에, 컴파일러는 위의 예제의 Binary, int 또는 func(map[int]string) 같은 각 concrete type 에 대해서 type description structure 를 생성한다.

비슷하게, 컴파일러는 interface type 에 대해 다른 type description structure 를 하나 생성한다. 그것 또한 method list 를 갖고 있다. interface runtime 은 interface type 의 method table 에 있는 모든 method를 concrete type의 method table 에서 찾아서 itable 을 계산한다.

runtime 은 itable 을 생성한 후에, 이것을 caching 한다. 그래서 이 것은 한번의 계산만 필요하다.


Computing the Itable

Now we know what the itables look like, but where do they come from? Go's dynamic type conversions mean that it isn't reasonable for the compiler or linker to precompute all possible itables: there are too many (interface type, concrete type) pairs, and most won't be needed. Instead, the compiler generates a type description structure for each concrete type like Binary or int or func(map[int]string). Among other metadata, the type description structure contains a list of the methods implemented by that type. Similarly, the compiler generates a (different) type description structure for each interface type like Stringer; it too contains a method list. The interface runtime computes the itable by looking for each method listed in the interface type's method table in the concrete type's method table. The runtime caches the itable after generating it, so that this correspondence need only be computed once. [ref. 1]

References

  1. research!rsc: Go Data Structures: Interfaces
  2. How to use interfaces in Go by jordan orelli

댓글 없음:

댓글 쓰기