Home > other >  In 2020-11-25: go, what is the underlying data structure of map?
In 2020-11-25: go, what is the underlying data structure of map?

Time:11-26

In 2020-11-25: go, what is the underlying data structure of map? # # f greatly architects a daily topic

CodePudding user response:

The language of the map is a kind of built-in reference types

Storage of the map key cannot repeat, no order, it sorts the key can be sorted, and then remove the corresponding value. The only comparable types can be as a key, the value is unlimited.

Go in the map USES a hash map

After a given key, can through the hash algorithm is a hash value, low B bits (2 ^ here is the capital of B, B says the number of buckets in the current map) which represents the map in the bucket, high eight is yes the bucket in a uint [8] in the array, high eight indec in the array can be used to find the corresponding key index.

Map can be abstracted as the bucket structure composed of structure, the bucket number is fixed at the beginning, after the late enough will be expanded, the bucket contains array, inside the bucket array is the key and the value of the storage that is 8 sets of data, stored as key0, key1, key2... Value0 value1, value2... Form, rather than the key and the value in order to store, this is to prevent the key and the length of the vaule doesn't conform need extra padding. The key and the value stored in the same array.

The structure of the bucket

Type hmap struct {
The count int//# number
elementFlags uint8
Uint8 B//that contains 2 ^ B bucket
Noverflow uint16//overflow bucket number
Hash0 uint32//hash seed
Buckets unsafe. Pointer//buckets array Pointer
Oldbuckets unsafe. Pointer//structure capacity for replication of buckets array
Nevacuate uintptr//relocation schedule number of buckets (been relocated)
Extra * mapextra
}
1
2
3
4
5
6
7
8
9
10
11.
The data structure of the bucket

Type bmap struct {
//tophash generally contains the top byte of the hash value
//for each key in this bucket. If tophash [0] //tophash [0] is a bucket evacuation state home.
Tophash [bucketCnt] uint8
//Followed by bucketCnt keys and then bucketCnt values.
//NOTE: packing all the keys together and then all the values together top service the
//code a bit more complicated than alternating key/value/key/value/... It allows
//us to eliminate the padding which whenever men for, e.g., map [int64] int8.
//Followed by an overflow pointer.
}
1
2
3
4
5
6
7
8
9
10
11.
It is important to note that more than a tophash array to the bucket, is stored inside the key by the hash function to calculate the hash value of the high 8-bit, the storage array length of 8, the key and the value of the byte array index... Note behind a few lines of comments, hmap is not only a tophash, but followed group 8 kv and a pointer overflow, such ability make overflow a chain table structure, but the two structures is not according to defined, but rather direct access through the pointer arithmetic,

The storage form of kv is a "key0key1key2key3... Key7val1val2val3... Val7 ", the advantage is: in different times the length of the key and the value, save the padding space, as the example, in the map [int64] int8, four adjacent int8 can be stored in the same memory location, if use kv staggered storage, is occupied by a padding every int8 separate memory unit (in order to improve the addressing speed),

A complete map of key lookup process:

1. According to the incoming key with the corresponding hash function to calculate the hash value.

2. Take the hash value of low B as to which one is the bucket

3. After the bucket, and hash value high 8 bits, and the bucket uint [8] array stored in the high 8-bit compares, match exactly according to the array inidex in key, the value in the byte array to find the corresponding key, if match the return key, value, if not exactly match the continue than high 8-bit values, the current high eight bucket array than everything, if you have any overflow bucket continues to compare, if all finish than not found the current map does not contain the key.

CodePudding user response:

Don't understand the go, but learned
https://baijiahao.baidu.com/s? Id=1609310227824363709 & amp; WFR=spider& For=PC
  • Related