Hello gophers,

Does Go compiler has some optimizations for function and *method* calls
with `variadic` arguments? For example, this simple benchmark:

```
package main

import "testing"

func Do(xs ...int) (s int) {
        for i, x := range xs {
                s += i + x
        }
        return s
}

func BenchmarkDo(b *testing.B) {
        for i := 0; i < b.N; i++ {
                Do(i, i, i, i)
        }
}
```

Provides these results:

```
BenchmarkDo-8   300000000       4.29 ns/op      0 B/op  0 allocs/op
```

That is, no allocation for slice of arguments is done. I may assume that
it is made by preparing `xs` slice on stack of `Do()`.

But, this example:


```
package main

import "testing"

type Doer interface {
        Do(...int) int
}

type impl struct {}

func (impl) Do(xs ...int) (s int) {
        for i, x := range xs {
                s += i + x
        }
        return s
}

func BenchmarkDo(b *testing.B) {
        var d Doer = impl{}
        for i := 0; i < b.N; i++ {
                d.Do(i, i, i, i)
        }
}
```

Provides different results:

```
BenchmarkDo-8   300000000       25.8 ns/op      32 B/op 1 allocs/op
```

That is, I assume that this allocation is made for the slice of arguments.

Also, I may assume that this because the compiler does not knows exactly
which function it will call in a runtime.

Am I understand it right and are there any plans for preparing such
optimizations?

Regards,
Sergey.

-- 
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