Re: [go-nuts] Pop out first element of a slice.

2016-09-20 Thread 'Axel Wagner' via golang-nuts
FWIW, I whipped up a short benchmark for this and it seems to confirm my intuition. For small N (depending on the exact setup, on my machine, the threshold was around N ~ 16 to 20) the copy approach is significantly faster, but for growing N the quadratic behavior of the copy quickly makes it much

Re: [go-nuts] Pop out first element of a slice.

2016-09-20 Thread Gabriel Adumitrachioaiei
Very good mathematical explanation. Thanks! On Tuesday, September 20, 2016 at 7:24:39 PM UTC+3, Axel Wagner wrote: > > if we assume, that the number of elements N is roughly stable (so pops and > pushes are roughly balanced): > > • If we append and reslice, we need to reallocate every N pops, bec

Re: [go-nuts] Pop out first element of a slice.

2016-09-20 Thread 'Axel Wagner' via golang-nuts
if we assume, that the number of elements N is roughly stable (so pops and pushes are roughly balanced): • If we append and reslice, we need to reallocate every N pops, because when space runs out, append will allocate 2N elements, so it has space for N new ones. After N pop/push sequences, it wil

Re: [go-nuts] Pop out first element of a slice.

2016-09-20 Thread Ian Davis
On Tue, Sep 20, 2016, at 04:15 PM, Gabriel Adumitrachioaiei wrote: > You might be right, but I just don't realize how. Since capacity will > be 2x or 1.5x as before, reallocating the slice will not happen often. > Or do you think that this would still be worse than copying almost all > slice everyt

Re: [go-nuts] Pop out first element of a slice.

2016-09-20 Thread Gabriel Adumitrachioaiei
You might be right, but I just don't realize how. Since capacity will be 2x or 1.5x as before, reallocating the slice will not happen often. Or do you think that this would still be worse than copying almost all slice everytime there is a pop ? On Tuesday, September 20, 2016 at 5:59:27 PM UTC+3

Re: [go-nuts] Pop out first element of a slice.

2016-09-20 Thread Ian Davis
On Tue, Sep 20, 2016, at 03:54 PM, Gabriel Adumitrachioaiei wrote: > Well, the capacity will be reduced by one. I don't think this makes > much difference. It makes a difference for a long running service that repeatedly pushes and pops. Ian -- You received this message because you are subscrib

Re: [go-nuts] Pop out first element of a slice.

2016-09-20 Thread Gabriel Adumitrachioaiei
Well, the capacity will be reduced by one. I don't think this makes much difference. On Tuesday, September 20, 2016 at 5:44:20 PM UTC+3, Ian Davis wrote: > > On Tue, Sep 20, 2016, at 03:23 PM, Gabriel Adumitrachioaiei wrote: > > I don't understand something when I want to pop out first element of

Re: [go-nuts] Pop out first element of a slice.

2016-09-20 Thread Ian Davis
On Tue, Sep 20, 2016, at 03:23 PM, Gabriel Adumitrachioaiei wrote: > I don't understand something when I want to pop out first element of a > slice and use it. > Here is my version: > > s := []int{1,2,3} first := s[] s = s[1:] > > > Here is a version that I saw in the standard library: > https://go

[go-nuts] Pop out first element of a slice.

2016-09-20 Thread Gabriel Adumitrachioaiei
I don't understand something when I want to pop out first element of a slice and use it. Here is my version: s := []int{1,2,3} first := s[0] s = s[1:] Here is a version that I saw in the standard library: https://golang.org/src/database/sql/sql.go#L791 first := s[0] copy(s, s[1:]) s = s[:len(s)