Hi Kortschak,

That's interesting! I've actually never had use for 3 index slicing in the 
past, so I didn't think of this. It definitely achieves the desired result, 
but also forces two allocations. I still find creating with the full 
capacity in one go and copying / appending to be faster.

func BenchmarkCombine4(b *testing.B) {
n := New("one", "two")
with := New("three")

for i := 0; i < b.N; i++ {
_ = append(n[:len(n):len(n)], with...)
}
}

func BenchmarkCombine5(b *testing.B) {
n := New("one", "two")
with := New("three")

for i := 0; i < b.N; i++ {
test := make(Namespace, 0, len(n)+len(with))
test = append(test, n...)
test = append(test, with...)
}
}

func BenchmarkCombine6(b *testing.B) {
n := New("one", "two")
with := New("three")

for i := 0; i < b.N; i++ {
test := make(Namespace, len(n), len(n)+len(with))
copy(test, n)
test = append(test, with...)
}
}
 

Of those three benchmarks, the 3rd consistently benchmarks lower. Not by 
huge margins, but enough:

BenchmarkCombine4-4       10000000       161 ns/op

BenchmarkCombine5-4       10000000       147 ns/op

BenchmarkCombine6-4       10000000       145 ns/op

Since my full use case (wasn't included in the original post) requires 
appending more than one thing in a loop, that exaggerates this issue 
further.

Thanks!
Evan

On Sunday, 17 July 2016 16:31:33 UTC-7, kortschak wrote:
>
> On Sun, 2016-07-17 at 09:09 -0700, Evan Digby wrote: 
> > For now the solution is to explicitly make copies, which was the desired 
> result in the first place. 
> > 
> > The code I posted earlier works as desired. 
>
> You don't need to make explicit copies. If you use three index slicing, 
> you get the behaviour you want. 
>
> https://play.golang.org/p/ENpRmubQV4 
>
>

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