日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Go語言開發(fā)者的ApacheArrow使用指南:內(nèi)存管理

如果你看了上一篇《Go語言開發(fā)者的Apache Arrow使用指南:數(shù)據(jù)類型》[1]中的諸多Go操作arrow的代碼示例,你很可能會(huì)被代碼中大量使用的Retain和Release方法搞暈。不光大家有這樣的感覺,我也有同樣的feeling:**Go是GC語言[2],為什么還要借助另外一套Retain和Release來進(jìn)行內(nèi)存管理呢**?

在這一篇文章中,我們就來探索一下這個(gè)問題的答案,并看看如何使用Retain和Release,順便再了解一下Apache Arrow的Go實(shí)現(xiàn)原理。

注:本文的內(nèi)容基于Apache Arrow Go v13版本(go.mod中g(shù)o version為v13)的代碼。

1. Go Arrow實(shí)現(xiàn)中的builder模式

看過第一篇文章中的代碼的童鞋可能發(fā)現(xiàn)了,無論是Primitive array type還是嵌套類型的諸如List array type,其array的創(chuàng)建套路都是這樣的:

  • 首先創(chuàng)建對應(yīng)類型的Builder,比如array.Int32Builder;
  • 然后,向Builder實(shí)例中append值;
  • 最后,通過Builder的NewArray方法獲得目標(biāo)Array的實(shí)例,比如array.Int32。

據(jù)說這個(gè)builder模式是參考了Arrow的C++實(shí)現(xiàn)。這里將Go的builder模式中各個(gè)類型之間的關(guān)系以下面這幅示意圖的形式呈現(xiàn)一下:

圖片

當(dāng)然這幅圖也大概可以作為Go Arrow實(shí)現(xiàn)的原理圖。

從圖中,我們可以看到:

  • Arrow go提供了Builder、Array、ArrayData接口作為抽象,在這些接口中都包含了用作內(nèi)存引用計(jì)數(shù)管理的Retain和Release方法;
  • array包提供了Builder接口的一個(gè)默認(rèn)實(shí)現(xiàn)builder類型,所有的XXXBuilder都組(內(nèi))合(嵌)了這個(gè)類型,這個(gè)類型實(shí)現(xiàn)了Retain方法,Release方法需要XXXBuilder自行實(shí)現(xiàn)。
  • array包提供了Array接口的一個(gè)默認(rèn)實(shí)現(xiàn)array類型,所有的array type(比如array.Int32)都組(內(nèi))合(嵌)了這個(gè)array類型。該類型實(shí)現(xiàn)了Retain和Release方法。
// github.com/apache/arrow/go/arrow/array/array.go
type array struct {
    refCount        int64
    data            *Data
    nullBitmapBytes []byte
}

// Retain increases the reference count by 1.
// Retain may be called simultaneously from multiple goroutines.
func (a *array) Retain() {
    atomic.AddInt64(&a.refCount, 1)
}

// Release decreases the reference count by 1.
// Release may be called simultaneously from multiple goroutines.
// When the reference count goes to zero, the memory is freed.
func (a *array) Release() {
    debug.Assert(atomic.LoadInt64(&a.refCount) > 0, "too many releases")

    if atomic.AddInt64(&a.refCount, -1) == 0 {
        a.data.Release()
        a.data, a.nullBitmapBytes = nil, nil
    }
}

下面以Int64 array type為例:

// github.com/apache/arrow/go/arrow/array/numeric.gen.go 

// A type which represents an immutable sequence of int64 values.
type Int64 struct {
    array // “繼承”了array的Retain和Release方法。
    values []int64
}
  • 通過XXXBuilder類型的NewArray方法可以獲得該Builder對應(yīng)的Array type實(shí)例,比如:調(diào)用Int32Builder的NewArray可獲得一個(gè)Int32 array type的實(shí)例。一個(gè)array type實(shí)例對應(yīng)的數(shù)據(jù)是邏輯上immutable的,一旦創(chuàng)建便不能改變。
  • 通過Array接口的Data方法可以得到該array type的底層數(shù)據(jù)layout實(shí)現(xiàn)(arrow.ArrayData接口的實(shí)現(xiàn)),包括child data。
  • arrow包定義了所有的數(shù)據(jù)類型對應(yīng)的ID值和string串,這個(gè)與arrow.DataType接口放在了一個(gè)源文件中。
  • 另外要注意,XXXBuilder的實(shí)例是“一次性”的,一旦調(diào)用NewArray方法返回一個(gè)array type實(shí)例,該XXXBuilder就會(huì)被reset。如果再次調(diào)用其NewArray方法,只能得到一個(gè)空的array type實(shí)例。你可以重用該Builder,只需向該Builder實(shí)例重新append值即可(見下面示例):
// reuse_string_builder.go

func main() {
    bldr := array.NewStringBuilder(memory.DefaultAllocator)
    defer bldr.Release()
    bldr.AppendValues([]string{"hello", "apache arrow"}, nil)
    arr := bldr.NewArray()
    defer arr.Release()
    bitmaps := arr.NullBitmapBytes()
    fmt.Println(hex.Dump(bitmaps))
    bufs := arr.Data().Buffers()
    for _, buf := range bufs {
        fmt.Println(hex.Dump(buf.Buf()))
    }
    fmt.Println(arr)

    // reuse the builder
    bldr.AppendValues([]string{"happy birthday", "leo messi"}, nil)
    arr1 := bldr.NewArray()
    defer arr1.Release()
    bitmaps1 := arr1.NullBitmapBytes()
    fmt.Println(hex.Dump(bitmaps1))
    bufs1 := arr1.Data().Buffers()
    for _, buf := range bufs1 {
        if buf != nil {
            fmt.Println(hex.Dump(buf.Buf()))
        }
    }
    fmt.Println(arr1)
}

輸出上面示例運(yùn)行結(jié)果:

$go run reuse_string_builder.go
00000000  03                                                |.|

00000000  03 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

00000000  00 00 00 00 05 00 00 00  11 00 00 00 00 00 00 00  |................|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

00000000  68 65 6c 6c 6f 61 70 61  63 68 65 20 61 72 72 6f  |helloapache arro|
00000010  77 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |w...............|
00000020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

["hello" "apache arrow"]
00000000  03                                                |.|

00000000  03 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

00000000  00 00 00 00 0e 00 00 00  17 00 00 00 00 00 00 00  |................|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

00000000  68 61 70 70 79 20 62 69  72 74 68 64 61 79 6c 65  |happy birthdayle|
00000010  6f 20 6d 65 73 73 69 00  00 00 00 00 00 00 00 00  |o messi.........|
00000020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

["happy birthday" "leo messi"]

想必到這里,大家對Arrow的Go實(shí)現(xiàn)原理有了一個(gè)大概的認(rèn)知了。接下來,我們再來看Go arrow實(shí)現(xiàn)的內(nèi)存引用計(jì)數(shù)管理。

2. Go Arrow實(shí)現(xiàn)的內(nèi)存引用計(jì)數(shù)管理

在上面圖中,我們看到Go Arrow實(shí)現(xiàn)的幾個(gè)主要接口Builder、Array、ArrayData都包含了Release和Retain方法,也就是說實(shí)現(xiàn)了這些接口的類型都支持采用引用計(jì)數(shù)方法(Reference Counting)進(jìn)行內(nèi)存的跟蹤和管理。Retain方法的語義是引用計(jì)數(shù)加1,而Release方法則是引用計(jì)數(shù)減1。由于采用了原子操作對引用計(jì)數(shù)進(jìn)行加減,因此這兩個(gè)方法是并發(fā)安全的。當(dāng)引用計(jì)數(shù)減到0時(shí),該引用計(jì)數(shù)對應(yīng)的內(nèi)存塊就可以被釋放掉了。

Go Arrow實(shí)現(xiàn)的主頁[3]上對引用計(jì)數(shù)的使用場景和規(guī)則做了如下說明:

  • 如果你被傳遞了一個(gè)對象并希望獲得它的所有權(quán)(ownership),你必須調(diào)用Retain方法。當(dāng)你不再需要該對象時(shí),你必須調(diào)用對應(yīng)的Release方法。"獲得所有權(quán)"意味著你希望在當(dāng)前函數(shù)調(diào)用的范圍之外訪問該對象。
  • 你通過名稱以New或Copy開頭的函數(shù)創(chuàng)建的任何對象,或者在通過channel接收對象時(shí),你都將擁有所有權(quán)。因此,一旦你不再需要這個(gè)對象,你必須調(diào)用Release。
  • 如果你通過一個(gè)channel發(fā)送一個(gè)對象,你必須在發(fā)送之前調(diào)用Retain,因?yàn)榻邮照邔碛性搶ο?。接收者有義務(wù)在以后不再需要該對象時(shí)調(diào)用Release。

有了這個(gè)說明后,我們對于Retain和Release的使用場景基本做到心里有譜了。但還有一個(gè)問題亟待解決,那就是:Go是GC語言,為何還要在GC之上加上一套引用計(jì)數(shù)呢?

這個(gè)問題我在這個(gè)issue[4]中找到了答案。一個(gè)Go arrow實(shí)現(xiàn)的commiter在回答issue時(shí)提到:“理論上,如果你知道你使用的是默認(rèn)的Go分配器,你實(shí)際上不必在你的消費(fèi)者(指的是Arrow Go包 API的使用者)代碼中調(diào)用Retain/Release,可以直接讓Go垃圾回收器管理一切。我們只需要確保我們在庫內(nèi)調(diào)用Retain/Release,這樣如果消費(fèi)者使用非Go GC分配器,我們就可以確保他們不會(huì)出現(xiàn)內(nèi)存泄漏”。

下面是默認(rèn)的Go分配器的實(shí)現(xiàn)代碼:

package memory

// DefaultAllocator is a default implementation of Allocator and can be used anywhere
// an Allocator is required.
//
// DefaultAllocator is safe to use from multiple goroutines.
var DefaultAllocator Allocator = NewGoAllocator()

type GoAllocator struct{}

func NewGoAllocator() *GoAllocator { return &GoAllocator{} }

func (a *GoAllocator) Allocate(size int) []byte {
    buf := make([]byte, size+alignment) // padding for 64-byte alignment
    addr := int(addressOf(buf))
    next := roundUpToMultipleOf64(addr)
    if addr != next {
        shift := next - addr
        return buf[shift : size+shift : size+shift]
    }
    return buf[:size:size]
}

func (a *GoAllocator) Reallocate(size int, b []byte) []byte {
    if size == len(b) {
        return b
    }

    newBuf := a.Allocate(size)
    copy(newBuf, b)
    return newBuf
}

func (a *GoAllocator) Free(b []byte) {}

我們看到默認(rèn)的Allocator只是分配一個(gè)原生切片,并且切片的底層內(nèi)存塊要保證64-byte對齊。

但為什么Retain和Release依然存在且需要調(diào)用呢?這位commiter給出了他理解的幾點(diǎn)原因:

  • 允許用戶控制buffer和內(nèi)部數(shù)據(jù)何時(shí)被設(shè)置為nil,以便在可能的情況下提前標(biāo)記為可被垃圾收集;
  • 如果用戶愿意,允許正確使用不依賴Go垃圾收集器的分配器(比如mallocator實(shí)現(xiàn),它使用malloc/free來管理C內(nèi)存而不是使用Go垃圾收集來管理);
  • 雖然用戶可以通過SetFinalizer來使用Finalizer進(jìn)行內(nèi)存釋放,但一般來說,我們建議最好有一個(gè)顯式的釋放動(dòng)作,而不是依賴finalizer,因?yàn)闆]有實(shí)際保證finalizer會(huì)運(yùn)行。此外,finalizer只在GC期間運(yùn)行,這意味著如果你的分配器正在分配C內(nèi)存或其他東西,而Go內(nèi)存一直很低,那么你有可能在任何finalizer運(yùn)行以實(shí)際調(diào)用Free之前,就被分配了大量的C內(nèi)存,從而耗盡了你的內(nèi)存。

基于這些原因,Go Arrow實(shí)現(xiàn)保留了Retain和Release,雖然有上門的一些場景使用方法,但這兩個(gè)方法的加入一定程度上增加了Go Arrow API使用的門檻。并且在重度使用Go Arrow實(shí)現(xiàn)的程序中,大家務(wù)必對程序做穩(wěn)定性長測試驗(yàn)證,以確保memory沒有l(wèi)eak。

3. 如何實(shí)現(xiàn)ZeroCopy的內(nèi)存數(shù)據(jù)共享

《In-Memory Analytics with Apache Arrow》[5]一書在第二章中提到了采用Arrow實(shí)現(xiàn)zerocopy的內(nèi)存數(shù)據(jù)共享的原理,這里將其稱為“切片(slice)原理”,用書中的例子簡單描述就是這樣的:假設(shè)你想對一個(gè)有數(shù)十億行的非常大的數(shù)據(jù)集進(jìn)行一些分析操作。提高這種操作性能的一個(gè)常見方法是對行的子集進(jìn)行并行操作,即僅通過對數(shù)組和數(shù)據(jù)緩沖區(qū)進(jìn)行切分,而不需要復(fù)制底層數(shù)據(jù)。這樣你操作的每個(gè)批次都不是一個(gè)副本--它只是數(shù)據(jù)的一個(gè)視圖。書中還給出了如下示意圖:

圖片

右側(cè)切片列中的每個(gè)切片的虛線表示它們只是各自列中的數(shù)據(jù)子集的視圖,每個(gè)切片都可以安全地進(jìn)行并行操作。

array type是邏輯上immutable的,底層data buffer一旦建立后,便可以通過切片的方式來以zerocopy方式做內(nèi)存數(shù)據(jù)共享,極大提高了數(shù)據(jù)操作的性能。

4. 小結(jié)

本文介紹了Go arrow實(shí)現(xiàn)的主要結(jié)構(gòu)以及實(shí)現(xiàn)模式:builder模式,并結(jié)合Go arrow官方資料說明了采用引用計(jì)數(shù)進(jìn)行內(nèi)存管理的原因與使用方法,最后介紹了Arrow實(shí)現(xiàn)ZeroCopy的內(nèi)存數(shù)據(jù)共享的原理。這些將為后續(xù)繼續(xù)深入學(xué)習(xí)Arrow高級數(shù)據(jù)類型/結(jié)構(gòu)奠定良好的基礎(chǔ)。

注:本文涉及的源代碼在這里[6]可以下載。

Gopher Daily(Gopher每日新聞)歸檔倉庫 - https://github.com/bigwhite/gopherdaily

我的聯(lián)系方式:

  • 微博(暫不可用):https://weibo.com/bigwhite20xx
  • 微博2:https://weibo.com/u/6484441286
  • 博客:tonybai.com
  • github: https://github.com/bigwhite

參考資料

[1] 《Go語言開發(fā)者的Apache Arrow使用指南:數(shù)據(jù)類型》: https://tonybai.com/2023/06/25/a-guide-of-using-apache-arrow-for-gopher-part1

[2] Go是GC語言: https://tonybai.com/2023/06/13/understand-go-gc-overhead-behind-the-convenience

[3] Go Arrow實(shí)現(xiàn)的主頁: https://github.com/apache/arrow/tree/main/go

[4] 這個(gè)issue: https://github.com/apache/arrow/issues/35232

[5] 《In-Memory Analytics with Apache Arrow》: https://book.douban.com/subject/35954154/

[6] 這里: https://github.com/bigwhite/experiments/blob/master/arrow/memory-management

[7] “Gopher部落”知識星球: https://wx.zsxq.com/dweb2/index/group/51284458844544

[8] 鏈接地址: https://m.do.co/c/bff6eed92687

本文轉(zhuǎn)載自微信公眾號「TonyBai」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系TonyBai公眾號。


本文題目:Go語言開發(fā)者的ApacheArrow使用指南:內(nèi)存管理
本文URL:http://www.dlmjj.cn/article/djddcch.html