FAQ
Hi
I like GO language so i'm trying to learn this language.
Using a slice in a little piece of example that append space in a []byte
the result of capacity of slice varying in function of number of appends.
Is there an algorithm to determine the cap?
I found ... the cap increase by step of 2^n when the cap of bytes is
insufficient
when i have added 33 elements the cap of slice is 64 so i have a slice
double
I think that when increasing the number of elements growing the bytes
allocated but not used.

example:
readerString:=strings.NewReader(" ")
onebyte,_:=readerString.ReadByte()

testing:=[]byte{}
fmt.Printf("n° byte before append>testing len(%d) cap(%d) %v
\n",len(testing),cap(testing),testing)
for i:=0 ;i<10; i++ {
testing=append(testing, onebyte)
fmt.Printf("\tn° byte after append %d >testing len(%d) cap(%d) %v
\n",i,len(testing),cap(testing),testing)
}

result:

n° byte before append>testing len(0) cap(0) []
n° byte after append 0 >testing len(1) cap(1) [32]
n° byte after append 1 >testing len(2) cap(2) [32 32]
n° byte after append 2 >testing len(3) cap(4) [32 32 32]
n° byte after append 3 >testing len(4) cap(4) [32 32 32 32]
n° byte after append 4 >testing len(5) cap(8) [32 32 32 32 32]
n° byte after append 5 >testing len(6) cap(8) [32 32 32 32 32 32]
n° byte after append 6 >testing len(7) cap(8) [32 32 32 32 32 32 32]
n° byte after append 7 >testing len(8) cap(8) [32 32 32 32 32 32 32 32]
n° byte after append 8 >testing len(9) cap(16) [32 32 32 32 32 32 32 32 32]
n° byte after append 9 >testing len(10) cap(16) [32 32 32 32 32 32 32 32 32
32]




--

Search Discussions

  • Kyle Lemons at Sep 24, 2012 at 9:54 pm
    there's no way to know what cap append will give you before you call it,
    because it's not specified in the spec. You could guess that it will
    follow the current scheme used by gc and/or gccgo, but it could change
    without warning.
    On Mon, Sep 24, 2012 at 2:09 PM, alex martin wrote:

    Hi
    I like GO language so i'm trying to learn this language.
    Using a slice in a little piece of example that append space in a []byte
    the result of capacity of slice varying in function of number of appends.
    Is there an algorithm to determine the cap?
    I found ... the cap increase by step of 2^n when the cap of bytes is
    insufficient
    when i have added 33 elements the cap of slice is 64 so i have a slice
    double
    I think that when increasing the number of elements growing the bytes
    allocated but not used.

    example:
    readerString:=strings.NewReader(" ")
    onebyte,_:=readerString.ReadByte()

    testing:=[]byte{}
    fmt.Printf("n° byte before append>testing len(%d) cap(%d) %v
    \n",len(testing),cap(testing),testing)
    for i:=0 ;i<10; i++ {
    testing=append(testing, onebyte)
    fmt.Printf("\tn° byte after append %d >testing len(%d) cap(%d) %v
    \n",i,len(testing),cap(testing),testing)
    }

    result:

    n° byte before append>testing len(0) cap(0) []
    n° byte after append 0 >testing len(1) cap(1) [32]
    n° byte after append 1 >testing len(2) cap(2) [32 32]
    n° byte after append 2 >testing len(3) cap(4) [32 32 32]
    n° byte after append 3 >testing len(4) cap(4) [32 32 32 32]
    n° byte after append 4 >testing len(5) cap(8) [32 32 32 32 32]
    n° byte after append 5 >testing len(6) cap(8) [32 32 32 32 32 32]
    n° byte after append 6 >testing len(7) cap(8) [32 32 32 32 32 32 32]
    n° byte after append 7 >testing len(8) cap(8) [32 32 32 32 32 32 32 32]
    n° byte after append 8 >testing len(9) cap(16) [32 32 32 32 32 32 32 32
    32]
    n° byte after append 9 >testing len(10) cap(16) [32 32 32 32 32 32 32 32
    32 32]




    --

    --
  • Francesc Campoy Flores at Sep 24, 2012 at 10:09 pm
    Note that the slices doubles its capacity only for small sizes, for bigger
    slices the growth rate will be smaller.

    http://play.golang.org/p/sqIgt3AkFT

    But again, this is not part of the spec, so I'd avoid writing code assuming
    this behavior.
    On Mon, Sep 24, 2012 at 2:53 PM, Kyle Lemons wrote:

    there's no way to know what cap append will give you before you call it,
    because it's not specified in the spec. You could guess that it will
    follow the current scheme used by gc and/or gccgo, but it could change
    without warning.

    On Mon, Sep 24, 2012 at 2:09 PM, alex martin wrote:

    Hi
    I like GO language so i'm trying to learn this language.
    Using a slice in a little piece of example that append space in a []byte
    the result of capacity of slice varying in function of number of appends.
    Is there an algorithm to determine the cap?
    I found ... the cap increase by step of 2^n when the cap of bytes is
    insufficient
    when i have added 33 elements the cap of slice is 64 so i have a slice
    double
    I think that when increasing the number of elements growing the bytes
    allocated but not used.

    example:
    readerString:=strings.NewReader(" ")
    onebyte,_:=readerString.ReadByte()

    testing:=[]byte{}
    fmt.Printf("n° byte before append>testing len(%d) cap(%d) %v
    \n",len(testing),cap(testing),testing)
    for i:=0 ;i<10; i++ {
    testing=append(testing, onebyte)
    fmt.Printf("\tn° byte after append %d >testing len(%d) cap(%d) %v
    \n",i,len(testing),cap(testing),testing)
    }

    result:

    n° byte before append>testing len(0) cap(0) []
    n° byte after append 0 >testing len(1) cap(1) [32]
    n° byte after append 1 >testing len(2) cap(2) [32 32]
    n° byte after append 2 >testing len(3) cap(4) [32 32 32]
    n° byte after append 3 >testing len(4) cap(4) [32 32 32 32]
    n° byte after append 4 >testing len(5) cap(8) [32 32 32 32 32]
    n° byte after append 5 >testing len(6) cap(8) [32 32 32 32 32 32]
    n° byte after append 6 >testing len(7) cap(8) [32 32 32 32 32 32 32]
    n° byte after append 7 >testing len(8) cap(8) [32 32 32 32 32 32 32 32]
    n° byte after append 8 >testing len(9) cap(16) [32 32 32 32 32 32 32 32
    32]
    n° byte after append 9 >testing len(10) cap(16) [32 32 32 32 32 32 32 32
    32 32]




    --

    --



    --
    --
    Francesc

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 24, '12 at 9:38p
activeSep 24, '12 at 10:09p
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase