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 ¶
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 ¶
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 ¶
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.
Types ¶
type DecoderFunc ¶
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.