[英]What's the benefit of that escape in the reflect package
// reflect/value.go
func ValueOf(i interface{}) Value {
if i == nil {
return Value{}
}
// TODO: Maybe allow contents of a Value to live on the stack.
// For now we make the contents always escape to the heap. It
// makes life easier in a few places (see chanrecv/mapassign
// comment below).
escapes(i)
上面的代碼是Value.go
中Value.go的源碼, escapes(i)
上面的注釋顯示,每次調用ValueOf
function, i
都會逃到堆中,這是為什么呢? 即,如何解釋它使一些地方的生活更輕松?
我還在學習 go,所以我無法描述更多,這就是社區 wiki 回答的原因。 但這是摘錄的說明(在 chanrecv 函數上方的說明):
注意:下面的一些 noescape 注釋在技術上是謊言,但在此 package 的上下文中是安全的。 像 chansend 和 mapassign 這樣的函數不會轉義所指對象,但可能會轉義所指對象指向的任何內容(它們對所指對象進行淺拷貝)。 在這個 package 中它是安全的,因為所指對象可能只指向一個 Value 可能指向的東西,並且它總是在堆中(由於 ValueOf 中的 escapes() 調用)。
另見:
// Dummy annotation marking that the value x escapes,
// for use in cases where the reflect code is so clever that
// the compiler cannot follow.
func escapes(x interface{}) {
if dummy.b {
dummy.x = x
}
}
var dummy struct {
b bool
x interface{}
}
我希望,這會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.