Find functions that return a reference and cause allocations.
When a function allocates a value and returns a reference to it, the value has to escape to the heap. This is slower and puts pressure on the garbage collector.
This may be optimized: You can avoid the heap allocation and allow the function to be inlined.
struct Coffee {
Type string
}
func New() *Coffee {
c := Coffee{
Type: "espresso"
}
return &c
}
- Download refreturn and copy the binary into your project's root for example.
- Run
./refreturn <directory>
and you'll see thatNew
returns a reference. - Check if the returned value is being created in the function.
- This is true for our
c
variable. - Optimize the function like so:
func New() *Coffee {
var c Coffee
return new(&c)
}
func new(c *Coffee) *Coffee {
c.Type = "espresso"
return c
}
New()
is now merely a wrapper which allocates the instance. The "real work" will be done in new()
.
This will allow mid-stack inlining as described in this blog post.