IT资讯 Go 语言新提案「arena」:优化内存分配

sammie · 2022-03-01 09:30:06 · 热度: 15

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

Go 语言新提案「arena」:优化内存分配

根据提案的介绍,「Go arena」用于优化内存分配。arena 是一种从连续的内存区域分配一组内存对象的方法,其优点是从 arena 分配对象通常比一般内存分配更有效。更重要的是,arena 中的对象能够用最少的内存管理垃圾回收开销一次释放所有内容。

通常来说,arena 不会在具备垃圾回收的编程语言中实现,因为它们用于显式释放 arena 内存的操作不安全,所以不符合垃圾回收语义。不过此提案的实现使用了动态检查来确保 arena 操作是安全的。此外还保证,如果 arena 操作不安全,程序将在任何不正确的行为发生之前终止。目前 Go 团队已在 Google 内部实现并使用了 arena,结果显示其为许多大型应用程序节省了高达 15% 的 CPU 和内存使用量,这主要是由于垃圾回收 CPU 时间和堆内存使用量的减少。

提案简介

在 Go 标准库中增加一个新的arena包,arena包可用于分配任意数量的 arena,可以从 arena 的内存中分配任意类型的对象,并且 arena 会根据需要自动增长大小。当一个 arena 中的所有对象不再使用时,可以显式释放该 arena 以有效地回收其内存,而无需进行常见的垃圾回收操作。我们要求此实现提供安全检查,如果 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)

arena 用法示例:

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()
}

猜你喜欢:
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册