<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

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

          共 2069字,需瀏覽 5分鐘

           ·

          2022-03-06 09:03

          近日,Go 語言社區(qū)正在討論名為「arena」的新提案。


          據(jù)介紹,arena 是一種從連續(xù)的內(nèi)存區(qū)域分配一組內(nèi)存對象的方法,其優(yōu)點是從 arena 分配對象通常比一般內(nèi)存分配更有效,更重要的是,arena 中的對象可以以最少的內(nèi)存管理或垃圾回收開銷一次釋放所有內(nèi)容。

          arena 通常不會在具備垃圾回收的編程語言中實現(xiàn),因為它們用于顯式釋放 arena 內(nèi)存的操作并不安全,所以不符合垃圾回收語義。

          但是,此提案的實現(xiàn)使用了動態(tài)檢查來確保 arena 操作是安全的。如果 arena 操作不安全,程序?qū)⒃谌魏尾徽_的行為發(fā)生之前終止。

          目前 Go 團隊已在 Google 內(nèi)部使用了 arena,結(jié)果顯示 arena 為許多大型應(yīng)用程序節(jié)省了高達 15% 的 CPU 和內(nèi)存使用量,這主要是由于垃圾回收 CPU 時間和堆內(nèi)存使用量的減少。

          提案介紹

          Go 團隊試圖在 Go 標準庫中添加一個新的 arena 包。arena 包將分配任意數(shù)量的 arena,可以從 arena 的內(nèi)存中分配任意類型的對象,并且 arena 會根據(jù)需要自動增長大小。

          當一個 arena 中的所有對象不再使用時,可以顯式釋放該 arena 以有效地回收其內(nèi)存,而無需進行一般的垃圾回收操作。Go 團隊要求此實現(xiàn)提供安全檢查,如果 arena 操作不安全,程序?qū)⒃谌魏尾徽_的行為發(fā)生之前終止。為了獲得最大的靈活性,API 能夠分配任何類型的對象和切片,包括可以在運行時通過反射生成的類型。
          ?

          提案?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)歸原作者所有,侵刪)


          點擊下方“閱讀原文”查看更多

          瀏覽 76
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  男人的天堂网2 | 97色色色色 | 国产一级内射视频 | 深爱激情五月网婷婷 | 污网址|