memtest

package module
v0.0.0-...-bc09edb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2018 License: BSD-3-Clause Imports: 6 Imported by: 0

README

memtest

GoDoc Build Status

Documentation

Overview

Package memtest provides functions for demonstrating multiple optimizations of memory allocation in Go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeDynamic

func DecodeDynamic(r io.Reader) ([]byte, error)

DecodeDynamic functions similarly to DecodeNoAlloc except that it allows for parsing arbitrarily long input - as long as there is sufficient memory available for the output.

Input is read into a small, fixed size, pre-allocated buffer.

Output is written to a dynamically allocated buffer which will grow as a function of the output length, at a rate of O(log n). That is, an insignificantly small number of allocations compared to the output length.

The output buffer never shrinks, so subsequent calls will never incur a memory allocation if their input length is equal to or shorter than previous calls. This memory remains allocated and unusuable to the rest of the program.

The dynamic buffer incurs a marginal computational performance penalty.

func DecodeNoAlloc

func DecodeNoAlloc(r io.Reader) ([]byte, error)

DecodeNoAlloc is a more complex DecoderFunc that avoids all memory allocations by parsing the input as a byte stream - without converting to string.

func DecodePrealloc

func DecodePrealloc(r io.Reader) ([]byte, error)

DecodePrealloc is a DecoderFunc that makes use of pre-allocated buffers for input and output. This negates the expense of allocating these buffers on demand, but incurs the penalty that this function is no longer safe to use concurrently from multiple goroutines, as each concurrent call should corrupt the contents of the same buffers used by the other calls.

It also requires that all input is shorter than the fixed length of the input buffer.

func DecodeSimple

func DecodeSimple(r io.Reader) ([]byte, error)

DecodeSimple is a naive DecoderFunc that takes a simple, but inefficient approach to decoding input.

Input may be of any length, provided there is sufficient memory available to store multiple copies and the entire output.

Types

type DecoderFunc

type DecoderFunc func(io.Reader) ([]byte, error)

DecoderFunc is any function that reads a string of space-separated integers and returns the ASCII character of each integer in a string.

For example, "79 75" becomes "OK".

func NewDecodeConcurrent

func NewDecodeConcurrent() DecoderFunc

NewDecodeConcurrent returns a DecoderFunc that behaves similarly to DecodeDynamic, except that the returned function has it own local buffers that are reused in each subsequent call.

For concurrency-safe decoding, call NewDecodeConcurrent in each spawned goroutine and call the returned DecoderFunc only within the goroutine that created it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL