Yes, it currently requires two lookups (two hashes, two bucket searches, 
etc.).  This general problem is issue 17133 
(https://github.com/golang/go/issues/17133).  Your example has the extra 
complication that the update operation is an append, not just a +=.


On Sunday, November 27, 2016 at 7:00:23 AM UTC-8, Ian Cottrell wrote:
>
> Yes you will get two map operations happen, one lookup and one assign.
>
> Wrapping the code into a (mildly modified for pretty output) runnable 
> example: https://play.golang.org/p/oGKvfS9ssH
>
> In the basic case of a new key, there is no way to avoid doing both a 
> lookup and assign, but in the case where the key exists, you can avoid the 
> assign by adding another level of indirection, like this: 
> https://play.golang.org/p/Rsm1uT4tlP
> You can see the only changes are the type of the likes map (from 
> map[string]People to map[string]*People), and the way it's modified in the 
> loop.
> I don't recommend actually doing this in general though. The result is not 
> horrific, but it's definitely less clear, and most cases where the map 
> lookup is actually a significant factor, you have probably already picked 
> the wrong algorithm, so micro optimising it is not going to get you far. At 
> the very least you would want to see it actually improve a benchmark before 
> going with it.
>
>
> On Sun, 27 Nov 2016 at 13:40 <stinkin...@gmail.com <javascript:>> wrote:
>
>> I should clarify that this actually came up in practice and I have no 
>> clue on how I can eliminate them
>>
>> -- 
>> 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/d/optout.
>>
>

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