網站首頁 編程語言 正文
背景:
golang的interface是一種satisfied式的。A類只要實現了IA interface定義的方法,A就satisfied了接口IA。更抽象一層,如果某些設計上需要一些更抽象的共性,比如print各類型,這時需要使用reflect機制,reflect實質上就是將interface的實現暴露了一部分給應用代碼。要理解reflect,需要深入了解interface。go的interface是一種隱式的interface,但golang的類型是編譯階段定的,是static的,如:
type MyInt int var i int var j MyInt
雖然MyInt底層就是int,但在編譯器角度看,i的類型是int,j的類型是MyInt,是靜態、不一致的。兩者要賦值必須要進行類型轉換。即使是interface,就語言角度來看也是靜態的。如:
var r io.Reader
不管r后面用什么來初始化,它的類型總是io.Reader。更進一步,對于空的interface,也是如此。記住go語言類型是靜態這一點,對于理解interface/reflect很重要。看一例:
var r io.Reader tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0) if err != nil { return nil, err } r = tty
到這里,r的類型是什么?r的類型仍然是interface io.Reader,只是r = tty這一句,隱含了一個類型轉換,將tty轉成了io.Reader。
interface的實現:
作為一門編程語言,對方法的處理一般分為兩種類型:一是將所有方法組織在一個表格里,靜態地調用(C++, java);二是調用時動態查找方法(python, smalltalk, js)。而go語言是兩者的結合:雖然有table,但是是需要在運行時計算的table。如下例:Binary類實現了兩個方法,String()和Get()
type Binary uint64 func (i Binary) String() string { return strconv.Uitob64(i.Get(), 2) } func (i Binary) Get() uint64 { return uint64(i) }
因為它實現了String(),按照golang的隱式方法實現來看,Binary satisfied了Stringer接口。因此它可以賦值: s:=Stringer(b)。以此為例來說明下interface的實現:interface的內存組織如圖:
一個interface值由兩個指針組成,第一個指向一個interface table,叫 itable。itable開頭是一些描述類型的元字段,后面是一串方法。注意這個方法是interface本身的方法,并非其dynamic value(Binary)的方法。即這里只有String()方法,而沒有Get方法。但這個方法的實現肯定是具體類的方法,這里就是Binary的方法。
當這個interface無方法時,itable可以省略,直接指向一個type即可。
另一個指針data指向dynamic value的一個拷貝,這里則是b的一份拷貝。也就是,給interface賦值時,會在堆上分配內存,用于存放拷貝的值。
同樣,當值本身只有一個字長時,這個指針也可以省略。
一個interface的初始值是兩個nil。比如,
var w io.Writer
這時,tab和data都是nil。interface是否為nil取決于itable字段。所以不一定data為nil就是nil,判斷時要額外注意。
這樣,像這樣的代碼:
switch v := any.(type) { case int: return strconv.Itoa(v) case float: return strconv.Ftoa(v, 'g', -1) }
實際上是any這個interface取了 ?any. tab->type。
而interface的函數調用實際上就變成了:
s.tab->fun[0](s.data)
。第一個參數即自身類型指針。
itable的生成:
itable的生成是理解interface的關鍵。
如剛開始處提的,為了支持go語言這種接口間僅通過方法來聯系的特性,是沒有辦法像C++一樣,在編譯時預先生成一個method table的,只能在運行時生成。因此,自然的,所有的實體類型都必須有一個包含此類型所有方法的“類型描述符”(type description structure);而interface類型也同樣有一個類似的描述符,包含了所有的方法。
這樣,interface賦值時,計算interface對象的itable時,需要對兩種類型的方法列表進行遍歷對比。如后面代碼所示,這種計算只需要進行一次,而且優化成了O(m+n)。
可見,interface與itable之間的關系不是獨立的,而是與interface具體的value類型有關。即(interface類型, 具體類型)->itable。
var any interface{} // initialized elsewhere s := any.(Stringer) // dynamic conversion for i := 0; i < 100; i++ { fmt.Println(s.String()) }
itable的計算不需要到函數調用時進行,只需要在interface賦值時進行即可,如上第2行,不需要在第4行進行。
最后,看一些實現代碼:
以下是上面圖中的兩個字段。
type iface struct { tab *itab // 指南itable data unsafe.Pointer // 指向真實數據 }
再看itab的實現:
type itab struct { inter *interfacetype _type *_type link *itab bad int32 unused int32 fun [1]uintptr // variable sized }
可見,它使用一個疑似鏈表的東西,可以猜這是用作hash表的拉鏈。前兩個字段應該是用來表達具體的interface類型和實際擁有的值的類型的,即一個itable的key。(上文提到的(interface類型, 具體類型) )
type imethod struct { name nameOff ityp typeOff } type interfacetype struct { typ _type pkgpath name mhdr []imethod }
interfacetype如有若干imethod,可以猜想這是表達interface定義的方法數據結構。
type _type struct { size uintptr ptrdata uintptr // size of memory prefix holding all pointers hash uint32 tflag tflag align uint8 fieldalign uint8 kind uint8 alg *typeAlg // gcdata stores the GC type data for the garbage collector. // If the KindGCProg bit is set in kind, gcdata is a GC program. // Otherwise it is a ptrmask bitmap. See mbitmap.go for details. gcdata *byte str nameOff ptrToThis typeOff }
對于_type,可見里面有gc的東西,應該就是具體的類型了。這里有個hash字段,itable實現就是掛在一個全局的hash table中。hash時用到了這個字段:
func itabhash(inter *interfacetype, typ *_type) uint32 { // compiler has provided some good hash codes for us. h := inter.typ.hash h += 17 * typ.hash // TODO(rsc): h += 23 * x.mhash ? return h % hashSize }
可見,這里有個把interface類型與具體類型之間的信息結合起來做一個hash的過程,這個hash就是上述的itab的存儲地點,itab中的link就是hash中的拉鏈。
回到itab,看取一個itab的邏輯:
如果發生了typeassert或是interface的賦值(強轉),需要臨時計算一個itab。這時會先在hash表中找,找不到才會真實計算。
h := itabhash(inter, typ) // look twice - once without lock, once with. // common case will be no lock contention. var m *itab var locked int for locked = 0; locked < 2; locked++ { if locked != 0 { lock(&ifaceLock) } for m = (*itab)(atomic.Loadp(unsafe.Pointer(&hash[h]))); m != nil; m = m.link { if m.inter == inter && m._type == typ { return m // 找到了前面計算過的itab } } } // 沒有找到,生成一個,并加入到itab的hash中。 m = (*itab)(persistentalloc(unsafe.Sizeof(itab{})+uintptr(len(inter.mhdr)-1)*sys.PtrSize, 0, &memstats.other_sys)) m.inter = inter m._type = typ additab(m, true, canfail)
這個hash是個全局變量:
const ( hashSize = 1009 ) var ( ifaceLock mutex // lock for accessing hash hash [hashSize]*itab )
最后,看一下如何生成itab:
// both inter and typ have method sorted by name, // and interface names are unique, // so can iterate over both in lock step; // the loop is O(ni+nt) not O(ni*nt). // 按name排序過的,因此這里的匹配只需要O(ni+nt) j := 0 for k := 0; k < ni; k++ { i := &inter.mhdr[k] itype := inter.typ.typeOff(i.ityp) name := inter.typ.nameOff(i.name) iname := name.name() for ; j < nt; j++ { t := &xmhdr[j] tname := typ.nameOff(t.name) if typ.typeOff(t.mtyp) == itype && tname.name() == iname { if m != nil { ifn := typ.textOff(t.ifn) *(*unsafe.Pointer)(add(unsafe.Pointer(&m.fun[0]), uintptr(k)*sys.PtrSize)) = ifn // 找到匹配,將實際類型的方法填入itab的fun } goto nextimethod } } } nextimethod: } h := itabhash(inter, typ) //插入上面的全局hash m.link = hash[h] atomicstorep(unsafe.Pointer(&hash[h]), unsafe.Pointer(m)) }
到這里,interface的數據結構的框架。
reflection實質上是將interface背后的實現暴露了一部分給應用代碼,使應用程序可以使用interface實現的一些內容。只要理解了interface的實現,reflect就好理解了。如reflect.typeof(i)返回interface i的type,Valueof返回value。
原文鏈接:https://www.cnblogs.com/qqmomery/p/6298771.html
相關推薦
- 2022-03-14 Spring mvc解決跨域請求:Response to preflight request doe
- 2022-08-02 Python+Selenium實現瀏覽器標簽頁的切換_python
- 2022-09-29 LyScript實現內存交換與差異對比的方法詳解_python
- 2022-11-10 C語言宏定義容易認不清的盲區梳理_C 語言
- 2022-03-23 android安裝后啟動出錯解決_Android
- 2022-01-12 對比原生Node封裝的Express路由 和 express框架路由
- 2023-01-02 Kotlin?FrameLayout與ViewPager2控件實現滾動廣告欄方法_Android
- 2023-03-15 React受控組件與非受控組件實例分析講解_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支