cockroach: Index | Files

package stringarena

import ""


Package Files


type Arena Uses

type Arena struct {
    // contains filtered or unexported fields

Arena provides arena allocation of a string from a []byte, reducing allocation overhead and GC pressure significantly if a large number of strings with a similar lifetime are being created. The reduction in allocation overhead should be obvious because we're replacing general allocation with what is essentially bump-pointer allocation. The reduction in GC pressure is due to the decrease in objects that the GC has to consider.

func Make Uses

func Make(acc *mon.BoundAccount) Arena

Make creates a new Arena with the specified monitoring account. If acc is nil, memory monitoring will be disabled.

func (*Arena) AllocBytes Uses

func (a *Arena) AllocBytes(ctx context.Context, b []byte) (string, error)

AllocBytes allocates a string in the arena with contents specified by b. Returns an error on memory accounting failure. The returned string can be used for as long as desired, but it will pin the entire underlying chunk of memory it was allocated from which can be significantly larger than the string itself. The primary use case for arena allocation is when all of the allocated strings will be released in mass.

func (*Arena) UnsafeReset Uses

func (a *Arena) UnsafeReset(ctx context.Context) error

UnsafeReset informs the memory account that previously allocated strings will no longer be used, and moves the current block pointer to the front of the buffer. Prefer to use this over creating a new arena in situations where we know that none of the strings currently on the arena will be referenced again.

NOTE: Do NOT use this if you cannot guarantee that previously allocated strings will never be referenced again! If we cannot guarantee that, we run the risk of overwriting their contents with new data, which violates assumptions about the immutability of Go's strings.

To prevent overwriting, we theoretically only need to ensure that strings allocated on the current block are never used again, but callers are oblivious to which block the string they get is allocated on, so it is easier to ensure that no previously allocated strings are used again.

Package stringarena imports 3 packages (graph) and is imported by 3 packages. Updated 2019-07-22. Refresh now. Tools for package owners.