/go-heap-performance

Go Heap Performance

Primary LanguageGo

Heap vs Stack Performance

Call By Value and Call by Reference

  • Call by value default for datatypes: int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr, float32, float64, string, bool, byte, rune, Array, Structs
  • Slices and Maps are Called by Reference by default
  • Receiver functions

Stack vs Heap

  • Call by value does not use the heap, no GC involved here
  • Call by reference could use the heap, it is not always the case

Execution

go build -gcflags '-m -l' # -m print decisions from compile, -l avoid inlining
go test -bench=. -benchmem 

Some Take Away

  • Call by seems to be faster in our artificial example
  • API-Design mutability should not be mixed
  • No absense value is possible (like nil), typical solution is to wrap it into a struct:
type Counter struct {
    valid boolean
    i int
}

References