evacuate in hashmap: where to put when migrate data from oldbucket to bucket

105 views
Skip to first unread message

lizhi...@gmail.com

unread,
Mar 15, 2018, 10:39:31 AM3/15/18
to golang-nuts
when evacuate oldbucket, go will migrate the data to a new position. 

and i found it used X and Y as two bucket where Xi equal the old index in h.oldbuckets and Yi equal Xi plus newbit

suppose the length of oldbuckets is 8 , and the hash of a key is 13 (just a example), then it will be put in the 5. Now, the buckets grow to 16,  it should be put in the 13. I think.

but i found the code below.  




useX will be true, if so, the 13 will be put in the 5 still, then i can't understand it. Did i miss something ? 

go 1.9

Ian Lance Taylor

unread,
Mar 15, 2018, 12:46:34 PM3/15/18
to 李志远, golang-nuts
When a bucket is split into two new buckets, X and Y, each value has
to go to either X or Y. The decision is made by looking at the hash.
That is the code you are looking at.

Ian

keith....@gmail.com

unread,
Mar 15, 2018, 7:35:18 PM3/15/18
to golang-nuts


On Thursday, March 15, 2018 at 9:46:34 AM UTC-7, Ian Lance Taylor wrote:
On Thu, Mar 15, 2018 at 1:34 AM,  <lizhi...@gmail.com> wrote:
>
> when evacuate oldbucket, go will migrate the data to a new position.
>
> and i found it used X and Y as two bucket where Xi equal the old index in
> h.oldbuckets and Yi equal Xi plus newbit
>
> suppose the length of oldbuckets is 8 , and the hash of a key is 13 (just a
> example), then it will be put in the 5. Now, the buckets grow to 16,  it
> should be put in the 13. I think.

Correct.
 
>
> but i found the code below.
>
> https://github.com/golang/go/blob/6732fcc06df713fc737cee5c5860bad87599bc6d/src/runtime/hashmap.go#L1115
>
>
>
>
>
> useX will be true, if so, the 13 will be put in the 5 still, then i can't
> understand it. Did i miss something ?

useX will be false because 13 & 8 == 0 is false.

lizhi...@gmail.com

unread,
Mar 19, 2018, 2:52:01 AM3/19/18
to golang-nuts
thanks for your correction ... i think i lost my mind some how. 

but can you tell me how the formula works? Or give me a hint that i can search 

lizhi...@gmail.com

unread,
Mar 19, 2018, 11:17:09 PM3/19/18
to golang-nuts
I  think i have got it, thank you all
Reply all
Reply to author
Forward
0 new messages