Re: [go-nuts] retaining mapiterkey return values in *reflect.MapIter

2020-06-30 Thread 'Dan Kortschak' via golang-nuts
Looking again, the second part of my question was wrong (it is a call
to `mapiterkey` and then `mapiterelem` in `Value`).

The code for Key and Value are almost identical, so the question
whether to send a change boils down whether the similarity between the
two is a benefit or a cost. I'm actually leaning towards the similarity
being a benefit and one that would be broken by retaining the value.

Dan

On Tue, 2020-06-30 at 19:24 -0700, Ian Lance Taylor wrote:
> On Tue, Jun 30, 2020 at 6:39 PM 'Dan Kortschak' via golang-nuts
>  wrote:
> > 
> > Is there a reason not to retain the returned value from
> > `mapiterkey` on
> > line 1243 of reflect/value.go[1] for use on line 1249[2].
> > 
> > ```
> > // Key returns the key of the iterator's current map entry.
> > func (it *MapIter) Key() Value {
> > if it.it == nil {
> > panic("MapIter.Key called before Next")
> > }
> > if mapiterkey(it.it) == nil { //L1243
> > panic("MapIter.Key called on exhausted iterator")
> > }
> > 
> > t := (*mapType)(unsafe.Pointer(it.m.typ))
> > ktype := t.key
> > return copyVal(ktype, it.m.flag.ro()|flag(ktype.Kind()),
> > mapiterkey(it.it)) // L1249
> > }
> > ```
> > 
> > The same question applies to `*MapIter.Value` method just below
> > `Key`.
> 
> I don't think there is any reason to call the function twice.  The
> value could be retained.
> 
> I think the functionswill be inlined, so it probably won't make any
> performance difference, but it might be a little clearer.
> 
> Ian


-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/6a06fb0b70927f05ed2680e11953e28af50f5a16.camel%40kortschak.io.


Re: [go-nuts] retaining mapiterkey return values in *reflect.MapIter

2020-06-30 Thread Ian Lance Taylor
On Tue, Jun 30, 2020 at 6:39 PM 'Dan Kortschak' via golang-nuts
 wrote:
>
> Is there a reason not to retain the returned value from `mapiterkey` on
> line 1243 of reflect/value.go[1] for use on line 1249[2].
>
> ```
> // Key returns the key of the iterator's current map entry.
> func (it *MapIter) Key() Value {
> if it.it == nil {
> panic("MapIter.Key called before Next")
> }
> if mapiterkey(it.it) == nil { //L1243
> panic("MapIter.Key called on exhausted iterator")
> }
>
> t := (*mapType)(unsafe.Pointer(it.m.typ))
> ktype := t.key
> return copyVal(ktype, it.m.flag.ro()|flag(ktype.Kind()), 
> mapiterkey(it.it)) // L1249
> }
> ```
>
> The same question applies to `*MapIter.Value` method just below `Key`.

I don't think there is any reason to call the function twice.  The
value could be retained.

I think the functionswill be inlined, so it probably won't make any
performance difference, but it might be a little clearer.

Ian

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcUWyV5SHi9pw2n-jFJ%3D-e2dLcUqfNz2pJgL%2BkvwyR4tCA%40mail.gmail.com.