github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/arena/arena.go (about)

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build goexperiment.arenas
     6  
     7  /*
     8  The arena package provides the ability to allocate memory for a collection
     9  of Go values and free that space manually all at once, safely. The purpose
    10  of this functionality is to improve efficiency: manually freeing memory
    11  before a garbage collection delays that cycle. Less frequent cycles means
    12  the CPU cost of the garbage collector is incurred less frequently.
    13  
    14  This functionality in this package is mostly captured in the Arena type.
    15  Arenas allocate large chunks of memory for Go values, so they're likely to
    16  be inefficient for allocating only small amounts of small Go values. They're
    17  best used in bulk, on the order of MiB of memory allocated on each use.
    18  
    19  Note that by allowing for this limited form of manual memory allocation
    20  that use-after-free bugs are possible with regular Go values. This package
    21  limits the impact of these use-after-free bugs by preventing reuse of freed
    22  memory regions until the garbage collector is able to determine that it is
    23  safe. Typically, a use-after-free bug will result in a fault and a helpful
    24  error message, but this package reserves the right to not force a fault on
    25  freed memory. That means a valid implementation of this package is to just
    26  allocate all memory the way the runtime normally would, and in fact, it
    27  reserves the right to occasionally do so for some Go values.
    28  */
    29  package arena
    30  
    31  import (
    32  	"github.com/shogo82148/std/unsafe"
    33  )
    34  
    35  // Arena represents a collection of Go values allocated and freed together.
    36  // Arenas are useful for improving efficiency as they may be freed back to
    37  // the runtime manually, though any memory obtained from freed arenas must
    38  // not be accessed once that happens. An Arena is automatically freed once
    39  // it is no longer referenced, so it must be kept alive (see runtime.KeepAlive)
    40  // until any memory allocated from it is no longer needed.
    41  //
    42  // An Arena must never be used concurrently by multiple goroutines.
    43  type Arena struct {
    44  	a unsafe.Pointer
    45  }
    46  
    47  // NewArena allocates a new arena.
    48  func NewArena() *Arena
    49  
    50  // Free frees the arena (and all objects allocated from the arena) so that
    51  // memory backing the arena can be reused fairly quickly without garbage
    52  // collection overhead. Applications must not call any method on this
    53  // arena after it has been freed.
    54  func (a *Arena) Free()
    55  
    56  // New creates a new *T in the provided arena. The *T must not be used after
    57  // the arena is freed. Accessing the value after free may result in a fault,
    58  // but this fault is also not guaranteed.
    59  func New[T any](a *Arena) *T
    60  
    61  // MakeSlice creates a new []T with the provided capacity and length. The []T must
    62  // not be used after the arena is freed. Accessing the underlying storage of the
    63  // slice after free may result in a fault, but this fault is also not guaranteed.
    64  func MakeSlice[T any](a *Arena, len, cap int) []T
    65  
    66  // Clone makes a shallow copy of the input value that is no longer bound to any
    67  // arena it may have been allocated from, returning the copy. If it was not
    68  // allocated from an arena, it is returned untouched. This function is useful
    69  // to more easily let an arena-allocated value out-live its arena.
    70  // T must be a pointer, a slice, or a string, otherwise this function will panic.
    71  func Clone[T any](s T) T