Skip to Content
marmot
DiagnosticsError codes

Heap allocations cost time in the allocator and the garbage collector; those in loops cost on every iteration.

The mistake and the fix

The mistake
func newPoint() *Point {
	return &Point{1, 2} // returned: heap
}
Fixed
func point() Point { 
	return Point{1, 2} // a value: no allocation
}

The fix

Return values instead of pointers, pass buffers in, keep large arrays out of loops, or avoid interfaces on hot paths.

See also all error codes and marmot explain.

On this page

Was this page helpful?

Report an issueOpen issues