On 27 August 2016 at 00:21, Peng Gao <peng.gao....@gmail.com> wrote:
>
>  Is there any good for complier or is just a simplification?
>

It doesn't change the escape aspects of this code, but it makes the program
smaller and thus easier to read and possibly faster.


 Sorry, I made a wrong description about Get, in fact cache2 implementation
> is like
> ...
> return &item.Object
>

Ah, that changes everything.  You are returning a pointer to a part of the
variable item, so the compiler must allocate that variable on the heap so
that it outlives the function call.


I guess it is the compiler decide the item will be  still be referenced, so
> it escapes? (But in fact I just want to hold Object of it and use nil
> pointer to indicate not exists, I don't want to hold the item itself).


In that case, don't return a pointer, return (v int64, ok bool).  Any time
you return a pointer, that is, the address of a variable, you should think
about two things.  First, aliasing: for correctness, consider what would
happen if the caller were to modify that variable.  Second, escaping: for
performance, consider whether you just caused an additional heap allocation.

cheers
alan

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