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 isfrom the arena a and stores it in//*slicePtr. The length of the slice isset to the capacity. The slice must
//not be accessed after arena a is freed.
func (a *Arena) NewSlice(slicePtr interface{}, cap int)
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
这一实践已经在 Google 得到了应用,且在一些大型应用程序中节省了高达 15% 的CPU和内存使用,这主要是由于减少了垃圾收集的CPU时间和堆内存使用所带来的效果。
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()}