dominikbraun / refreturn

Find functions that return a reference and cause allocations.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

refreturn

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.


Example: a simple constructor

struct Coffee {
    Type string
}

func New() *Coffee {
    c := Coffee{
        Type: "espresso"
    }
    return &c
}
  1. Download refreturn and copy the binary into your project's root for example.
  2. Run ./refreturn <directory> and you'll see that New returns a reference.
  3. Check if the returned value is being created in the function.
  4. This is true for our c variable.
  5. 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.

About

Find functions that return a reference and cause allocations.


Languages

Language:Go 92.5%Language:Makefile 7.5%