Use 'go build -gcflags="-m"' to see what inlining actions the compiler
takes. more here:
https://github.com/golang/go/wiki/CompilerOptimizations#escape-analysis-and-inlining

for example this https://play.golang.org/p/QyAauePKbn- will result in:

$ go build -gcflags='-m' t.go
# command-line-arguments
./t.go:9:6: can inline NewStuff
./t.go:14:28: inlining call to NewStuff
./t.go:14:28: NewStuff() escapes to heap
./t.go:14:12: main ... argument does not escape
$


multiple -m options may be supplied, although the extra output isn't
especially user-friendly :)

On Sun, Mar 4, 2018 at 6:57 AM, Bruno Novais <bruno.nov...@synng.com> wrote:
> Hello Gophers!
>
> As you probably noticed by my question, I'm new to this awesome language
> called Go (coming from C/C++). In C++ I rely a lot on constructor (copy)
> elision. I think Go doesn't have this concept, but I would like to know what
> happen when I do something like this:
>
> func NewStuff() Stuff {
>   return Sutff{}
> }
>
> What will happen in that case? Will it be store on the stack, and then
> copied to the caller function? That will probably be inlined, so:
>
> func NewStuff() Stuff {
>   // A lot of pre-processing that won't be inlined.
>
>   return Sutff{
>     Value: var1,
>     Another: var2,
>     Etc: var3,
>   }
> }
>
> The returned value will be copied, or the compiler will optimize and change
> the memory space of the caller function?
>
> Thank you!
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to