3

Recently, the Go language community is discussing a new proposal called "arena".

According to reports, arena is a method of allocating a set of memory objects from a contiguous memory area. The advantage is that allocating objects from arena is usually more efficient than general memory allocation, and more importantly, objects in arena can be managed with minimal memory Or garbage collection overhead to free everything at once.

arena are not typically implemented in a programming language with garbage collection because their operations for explicitly freeing arena memory are not safe and therefore do not conform to garbage collection semantics.

However, the implementation of this proposal uses dynamic checks to ensure that arena operations are safe. If the arena operation is unsafe, the program will terminate before any incorrect behavior occurs.

The Go team has used arena internally at Google, and results show that arena saves up to 15% of CPU and memory usage for many large applications, mainly due to the reduction in garbage collection CPU time and heap memory usage.

Proposal introduction

The Go team is trying to add a new arena package to the Go standard library. The arena package will allocate any number of arena, any type of object can be allocated from the arena's memory, and the arena will automatically grow in size as needed.

When all objects in an arena are no longer in use, the arena can be explicitly freed to effectively reclaim its memory without the need for normal garbage collection operations. The Go team requires this implementation to provide safety checks, and if the arena operation is unsafe, the program will terminate before any incorrect behavior occurs. For maximum flexibility, the API is able to allocate objects and slices of any type, including types that can be generated by reflection at runtime.

Proposal 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)

Usage example:

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

snakesss
1.1k 声望244 粉丝

SegmentFault 思否编辑,欢迎投稿优质技术资讯!