FAQ
Pass by reference can save memory and more efficient, but in built-in
package most functions I saw used passed-by-value

ie:

func (b *Buffer) Write(p []byte) (n int, err error) {

b.lastRead = opInvalid

m := b.grow(len(p))

return copy(b.buf[m:], p), nil

}

the p[]byte will make a copy of src byte slice, since the copy operation is
not atomic (am I right?), the code itself is not thread safe.

If there's a reason behind this? I am just afraid of misused pointer
arguments

thanks for any hints


--
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/groups/opt_out.

Search Discussions

  • Dave Cheney at Mar 12, 2013 at 7:20 am
    Slices are reference types, all you are passing is three words (on the stack) regardless of how large the array backing the slice is.
    On 12/03/2013, at 18:17, davy zhang wrote:

    Pass by reference can save memory and more efficient, but in built-in package most functions I saw used passed-by-value

    ie:
    func (b *Buffer) Write(p []byte) (n int, err error) {

    b.lastRead = opInvalid

    m := b.grow(len(p))

    return copy(b.buf[m:], p), nil

    }

    the p[]byte will make a copy of src byte slice, since the copy operation is not atomic (am I right?), the code itself is not thread safe.

    If there's a reason behind this? I am just afraid of misused pointer arguments

    thanks for any hints



    --
    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/groups/opt_out.
    --
    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/groups/opt_out.
  • Davy zhang at Mar 12, 2013 at 7:26 am
    thanks, that's the reason :D

    I checked the manual it says:

    Slices, maps and channels are reference types

    On Tuesday, March 12, 2013 3:20:10 PM UTC+8, Dave Cheney wrote:

    Slices are reference types, all you are passing is three words (on the
    stack) regardless of how large the array backing the slice is.

    On 12/03/2013, at 18:17, davy zhang <davy...@gmail.com <javascript:>>
    wrote:

    Pass by reference can save memory and more efficient, but in built-in
    package most functions I saw used passed-by-value

    ie:

    func (b *Buffer) Write(p []byte) (n int, err error) {

    b.lastRead = opInvalid

    m := b.grow(len(p))

    return copy(b.buf[m:], p), nil

    }

    the p[]byte will make a copy of src byte slice, since the copy operation
    is not atomic (am I right?), the code itself is not thread safe.

    If there's a reason behind this? I am just afraid of misused pointer
    arguments

    thanks for any hints


    --
    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...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.


    --
    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/groups/opt_out.
  • Rémy Oudompheng at Mar 12, 2013 at 7:25 am

    On 2013/3/12 davy zhang wrote:
    the p[]byte will make a copy of src byte slice, since the copy operation is
    not atomic (am I right?), the code itself is not thread safe.
    There are already many more reasons why the code is not thread safe.
    If there's a reason behind this? I am just afraid of misused pointer
    arguments
    A lot of functions modify their argument but you don't want them to
    modify the original value.

    Rémy.

    --
    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/groups/opt_out.
  • Anoop K at Mar 12, 2013 at 7:39 am
    Does the 3 words correspond to (ptr, len, cap) ?

    struct Slice
    { // must not move anything
    byte* array; // actual data
    uint32 len; // number of elements
    uint32 cap; // allocated number of elements
    };

    Are there any article/doc which explains how references are implemented in
    GO ? Is it basically copying the struct
    with meta fields and ptr ? If so for a map does it need to copy following
    struct ?


    struct Hmap { /* a hash table; initialize with hash_init() */
    uint32 count; /* elements in table - must be first */
    uint8 datasize; /* amount of data to store in entry */
    uint8 flag;
    uint8 valoff; /* offset of value in key+value data block */
    int32 changes; /* inc'ed whenever a subtable is created/grown */
    uintptr hash0; /* hash seed */
    struct hash_subtable *st; /* first-level table */
    };


    Thanks
    Anoop
    On Tuesday, 12 March 2013 12:47:46 UTC+5:30, davy zhang wrote:

    Pass by reference can save memory and more efficient, but in built-in
    package most functions I saw used passed-by-value

    ie:

    func (b *Buffer) Write(p []byte) (n int, err error) {

    b.lastRead = opInvalid

    m := b.grow(len(p))

    return copy(b.buf[m:], p), nil

    }

    the p[]byte will make a copy of src byte slice, since the copy operation
    is not atomic (am I right?), the code itself is not thread safe.

    If there's a reason behind this? I am just afraid of misused pointer
    arguments

    thanks for any hints

    --
    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/groups/opt_out.
  • Dmitry Vyukov at Mar 12, 2013 at 7:52 am

    On Tue, Mar 12, 2013 at 11:39 AM, Anoop K wrote:
    Does the 3 words correspond to (ptr, len, cap) ? Yes
    struct Slice
    { // must not move anything
    byte* array; // actual data
    uint32 len; // number of elements
    uint32 cap; // allocated number of elements
    };

    Are there any article/doc which explains how references are implemented in
    GO ? Is it basically copying the struct
    with meta fields and ptr ? If so for a map does it need to copy following
    struct ?
    Yes, interface copy copies 2 words -- ptr to object and ptr to type descriptor.

    Strings are 2 words.
    struct Hmap { /* a hash table; initialize with hash_init() */
    uint32 count; /* elements in table - must be first */
    uint8 datasize; /* amount of data to store in entry */
    uint8 flag;
    uint8 valoff; /* offset of value in key+value data block */
    int32 changes; /* inc'ed whenever a subtable is created/grown */
    uintptr hash0; /* hash seed */
    struct hash_subtable *st; /* first-level table */
    };

    Map and chans are actually *Hmap and *Hchan, so Hmap is *not* copied.
    map/chan copy is just 1 pointer copy.

    --
    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/groups/opt_out.
  • Jan Mercl at Mar 12, 2013 at 8:07 am

    On Tue, Mar 12, 2013 at 8:39 AM, Anoop K wrote:
    Does the 3 words correspond to (ptr, len, cap) ?

    struct Slice
    { // must not move anything
    byte* array; // actual data
    uint32 len; // number of elements
    uint32 cap; // allocated number of elements
    };
    The tip version should be s/uint32/uintptr/g (or equivalent), I think.

    -j

    --
    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/groups/opt_out.
  • Nigel Tao at Mar 12, 2013 at 8:41 am

    On Tue, Mar 12, 2013 at 6:39 PM, Anoop K wrote:
    struct Slice
    { // must not move anything
    byte* array; // actual data
    uint32 len; // number of elements
    uint32 cap; // allocated number of elements
    };

    Are there any article/doc which explains how references are implemented in
    GO ?
    http://golang.org/doc/articles/slices_usage_and_internals.html

    --
    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/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 12, '13 at 7:17a
activeMar 12, '13 at 8:41a
posts8
users7
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase