Go 語言社區(qū)新提案 arena,可優(yōu)化內(nèi)存分配


提案介紹
提案?API
package?arena
type?Arena?struct?{
?//?contains?filtered?or?unexported?fields
}
//?New?allocates?a?new?arena.
func?New()?*Arena
//?Free?frees?the?arena?(and?all?objects?allocated?from?the?arena)?so?that
//?memory?backing?the?arena?can?be?reused?fairly?quickly?without?garbage
//?collection?overhead.??Applications?must?not?call?any?method?on?this
//?arena?after?it?has?been?freed.
func?(a?*Arena)?Free()
//?New?allocates?an?object?from?arena?a.??If?the?concrete?type?of?objPtr?is
//?a?pointer?to?a?pointer?to?type?T?(**T),?New?allocates?an?object?of?type
//?T?and?stores?a?pointer?to?the?object?in?*objPtr.??The?object?must?not
//?be?accessed?after?arena?a?is?freed.
func?(a?*Arena)?New(objPtr?interface{})
//?NewSlice?allocates?a?slice?from?arena?a.??If?the?concrete?type?of?slicePtr
//?is?*[]T,?NewSlice?creates?a?slice?of?element?type?T?with?the?specified
//?capacity?whose?backing?store?is?from?the?arena?a?and?stores?it?in
//?*slicePtr.?The?length?of?the?slice?is?set?to?the?capacity.??The?slice?must
//?not?be?accessed?after?arena?a?is?freed.
func?(a?*Arena)?NewSlice(slicePtr?interface{},?cap?int)
import?(
?“arena”
?…
)
type?T?struct?{
?val?int
}
func?main()?{
?a?:=?arena.New()
?var?ptrT?*T
?a.New(&ptrT)
?ptrT.val?=?1
?var?sliceT?[]T
?a.NewSlice(&sliceT,?100)
?sliceT[99]?.val?=?4
?a.Free()
}文章轉(zhuǎn)載:SegmentFault
(版權(quán)歸原作者所有,侵刪)

點擊下方“閱讀原文”查看更多
評論
圖片
表情
