FAQ
Hello!
This is my code in which i create a buffer and receive data in it.
in my code i allocate the buffer in every loop , and while go is a garbage
collector language so i don't need to destruct it.
my question is : is there a better solution to create the buffer with a
better consumption of memory ,
or should i destruct it every time ??


for {

         buffer1 := make([]byte,512)
         client.ReadClient(buffer1)
         str:=str2+string(buffer1)

         for {
                                 posSTX := strings.Index(str, string(0x02))
         posETX := strings.Index(str, string(0x03))
         if(posSTX==-1 || posETX==-1) {
return
}

if (strings.LastIndex(str, string(0x02)) != posSTX ||
strings.LastIndex(str, string(0x03)) != posETX) {

str1:=str[posSTX+1:posETX]
str2=str[posETX+1:]
str=str2

}else {
     Log("MsgR: ",str[posSTX+1:posETX])
     str2=""
}

if str2=="" || strings.Index(str2,string(0x03))==-1 {break}
     }
}

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Dave Cheney at Apr 15, 2014 at 11:43 am

    On Tuesday, 15 April 2014 21:23:32 UTC+10, farah wrote:
    Hello!
    This is my code in which i create a buffer and receive data in it.
    in my code i allocate the buffer in every loop , and while go is a garbage
    collector language so i don't need to destruct it.
    my question is : is there a better solution to create the buffer with a
    better consumption of memory ,
    or should i destruct it every time ??


    for {

    buffer1 := make([]byte,512)
    client.ReadClient(buffer1)
    ^^ don't you need to record the amount of data the client actually sent
    you, and handle any error, of course

    str:=str2+string(buffer1)
    you are copying the contents into a string here, and then appending it to
    another string, there is lots of copying going on here.

    for {
    posSTX := strings.Index(str, string(0x02))
    posETX := strings.Index(str, string(0x03))
    if(posSTX==-1 || posETX==-1) {
    return
    }

    if (strings.LastIndex(str, string(0x02)) != posSTX ||
    strings.LastIndex(str, string(0x03)) != posETX) {

    str1:=str[posSTX+1:posETX]
    str2=str[posETX+1:]
    str=str2

    }else {
    Log("MsgR: ",str[posSTX+1:posETX])
    str2=""
    }

    if str2=="" || strings.Index(str2,string(0x03))==-1 {break}
    }
    }
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Francesco Bochicchio at Apr 15, 2014 at 12:52 pm
    1. Maybe you could consider using bytes.Index, bytes.LastIndex etc
         instead of strings.Index, strings.LastIndex etc, so that you don't need to
        do str := str1 + buffer1
        2. You could allocate buffer1 once outside of the loop, then you do:

    read_bytes := client,ReadClient(buffer1)



    where ReadClient returns the slice representing the portion of buffer1
    containing the read bytes. The you do all the other stuff using read_bytes
    instead of buffer1



    Il giorno martedì 15 aprile 2014 13:23:32 UTC+2, farah ha scritto:
    Hello!
    This is my code in which i create a buffer and receive data in it.
    in my code i allocate the buffer in every loop , and while go is a garbage
    collector language so i don't need to destruct it.
    my question is : is there a better solution to create the buffer with a
    better consumption of memory ,
    or should i destruct it every time ??


    for {

    buffer1 := make([]byte,512)
    client.ReadClient(buffer1)
    str:=str2+string(buffer1)

    for {
    posSTX := strings.Index(str, string(0x02))
    posETX := strings.Index(str, string(0x03))
    if(posSTX==-1 || posETX==-1) {
    return
    }

    if (strings.LastIndex(str, string(0x02)) != posSTX ||
    strings.LastIndex(str, string(0x03)) != posETX) {

    str1:=str[posSTX+1:posETX]
    str2=str[posETX+1:]
    str=str2

    }else {
    Log("MsgR: ",str[posSTX+1:posETX])
    str2=""
    }

    if str2=="" || strings.Index(str2,string(0x03))==-1 {break}
    }
    }
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Keith Randall at Apr 15, 2014 at 11:28 pm
    Why can't you allocate buffer1 outside the outer for loop?
    On Tuesday, April 15, 2014 5:52:39 AM UTC-7, Francesco Bochicchio wrote:


    1. Maybe you could consider using bytes.Index, bytes.LastIndex etc
    instead of strings.Index, strings.LastIndex etc, so that you don't need to
    do str := str1 + buffer1
    2. You could allocate buffer1 once outside of the loop, then you do:

    read_bytes := client,ReadClient(buffer1)



    where ReadClient returns the slice representing the portion of buffer1
    containing the read bytes. The you do all the other stuff using read_bytes
    instead of buffer1



    Il giorno martedì 15 aprile 2014 13:23:32 UTC+2, farah ha scritto:
    Hello!
    This is my code in which i create a buffer and receive data in it.
    in my code i allocate the buffer in every loop , and while go is a
    garbage collector language so i don't need to destruct it.
    my question is : is there a better solution to create the buffer with a
    better consumption of memory ,
    or should i destruct it every time ??


    for {

    buffer1 := make([]byte,512)
    client.ReadClient(buffer1)
    str:=str2+string(buffer1)

    for {
    posSTX := strings.Index(str, string(0x02))
    posETX := strings.Index(str, string(0x03))
    if(posSTX==-1 || posETX==-1) {
    return
    }

    if (strings.LastIndex(str, string(0x02)) != posSTX ||
    strings.LastIndex(str, string(0x03)) != posETX) {

    str1:=str[posSTX+1:posETX]
    str2=str[posETX+1:]
    str=str2

    }else {
    Log("MsgR: ",str[posSTX+1:posETX])
    str2=""
    }

    if str2=="" || strings.Index(str2,string(0x03))==-1 {break}
    }
    }
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Farah at Apr 16, 2014 at 7:58 am
    Why can't you allocate buffer1 outside the outer for loop?
            the buffer should be allocated and empty every time the server
    receives a message because the buffer will recover the message received.
           and i can't allocate a buffer with a huge capacity , it's a loss of
    memory and also i can't predict the size of the buffer.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Keith Randall at Apr 17, 2014 at 11:43 pm
    You have lots of other options. One is to allocate a buffer outside the
    loop for use in small messages, and allocate per-message for big messages.

    msgbuf := make([]byte, 1024)

    for ;; {
         msgsize = ....
         var buf []byte
         if msgsize > 1024 {
             buf = make([]byte, msgsize)
         } else {
             buf = msgbuf[:msgsize]
         }
         client.ReadClient(buf)
    }

    You could get arbitrarily more complicated if you care. You probably
    shouldn't.
    On Wednesday, April 16, 2014 12:58:07 AM UTC-7, farah wrote:

    Why can't you allocate buffer1 outside the outer for loop?
    the buffer should be allocated and empty every time the server
    receives a message because the buffer will recover the message received.
    and i can't allocate a buffer with a huge capacity , it's a loss of
    memory and also i can't predict the size of the buffer.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dave Cheney at Apr 18, 2014 at 1:11 am
    go build -gcflags -m

    will tell you if the buffer escapes to the heap or not.

    If it doesn't escape, your job is done

    If it does, then fix that.

    Either way, I think you're optimising before measuring.
    On Wednesday, 16 April 2014 17:58:07 UTC+10, farah wrote:

    Why can't you allocate buffer1 outside the outer for loop?
    the buffer should be allocated and empty every time the server
    receives a message because the buffer will recover the message received.
    and i can't allocate a buffer with a huge capacity , it's a loss of
    memory and also i can't predict the size of the buffer.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Farah at Apr 18, 2014 at 10:20 am

    I did that! but i couldn't understood what does "escape to the heap or
    not" mean ???
    when should i find escape to heap and when not?
    and what should i do for that?
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 15, '14 at 11:23a
activeApr 18, '14 at 10:20a
posts8
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase