V · 2022年03月02日

Go 语言社区新提案 arena,可优化内存分配

​近日,Go 语言社区正在讨论名为「arena」的新提案。

据介绍,arena 是一种从连续的内存区域分配一组内存对象的方法,其优点是从 arena 分配对象通常比一般内存分配更有效,更重要的是,arena 中的对象可以以最少的内存管理或垃圾回收开销一次释放所有内容。

arena 通常不会在具备垃圾回收的编程语言中实现,因为它们用于显式释放 arena 内存的操作并不安全,所以不符合垃圾回收语义。

但是,此提案的实现使用了动态检查来确保 arena 操作是安全的。如果 arena 操作不安全,程序将在任何不正确的行为发生之前终止。

目前 Go 团队已在Google 内部使用了arena,结果显示 arena为许多大型应用程序节省了高达 15% 的 CPU 和内存使用量,这主要是由于垃圾回收CPU 时间和堆内存使用量的减少。

提案介绍

Go 团队试图在Go 标准库中添加一个新的 arena 包。arena 包将分配任意数量的 arena,可以从 arena 的内存中分配任意类型的对象,并且 arena 会根据需要自动增长大小。

当一个 arena 中的所有对象不再使用时,可以显式释放该 arena 以有效地回收其内存,而无需进行一般的垃圾回收操作。Go 团队要求此实现提供安全检查,如果 arena操作不安全,程序将在任何不正确的行为发生之前终止。为了获得最大的灵活性,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()
}
推荐阅读
关注数
4177
内容数
826
SegmentFault 思否旗下人工智能领域产业媒体,专注技术与产业,一起探索人工智能。
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息