这篇文章主要讲解了“Golang基础学习之map的实现原理是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Golang基础学习之map的实现原理是什么”吧!
0. 简介
Gomap
1. 实现原理
1.1 底层结构
hmap
GomaphmapmaphmapGo”引用“sliceappend
// A header for a Go map.
type hmap struct {
// Note: the format of the hmap is also encoded in cmd/compile/internal/reflectdata/reflect.go.
// Make sure this stays in sync with the compiler's definition.
count int // # live cells == size of map. Must be first (used by len() builtin)
flags uint8
B uint8 // log_2 of # of buckets (can hold up to loadFactor * 2^B items)
noverflow uint16 // approximate number of overflow buckets; see incrnoverflow for details
hash0 uint32 // hash seed
buckets unsafe.Pointer // array of 2^B Buckets. may be nil if count==0.
oldbuckets unsafe.Pointer // previous bucket array of half the size, non-nil only when growing
nevacuate uintptr // progress counter for evacuation (buckets less than this have been evacuated)
extra *mapextra // optional fields
}
bucketsoldbucketsextrabucketkey-value
bmap
runtime.bmapbuckettophashkey
// A bucket for a Go map.
type bmap struct {
// tophash generally contains the top byte of the hash value
// for each key in this bucket. If tophash[0] < minTopHash,
// tophash[0] is a bucket evacuation state instead.
tophash [bucketCnt]uint8
// Followed by bucketCnt keys and then bucketCnt elems.
// NOTE: packing all the keys together and then all the elems together makes the
// code a bit more complicated than alternating key/elem/key/elem/... but it allows
// us to eliminate padding which would be needed for, e.g., map[int64]int8.
// Followed by an overflow pointer.
}
bmapkey1/elem1/key2/elem2...keyvalue
type bmap struct {
topbits [8]uint8
keys [8]keytype
elems [8]elemtype
overflow uintptr
keyselemsmapbucketsGCmapoverflowuintptrunsafe.PointerhmapextraextraGC
// mapextra holds fields that are not present on all maps.
type mapextra struct {
// If both key and elem do not contain pointers and are inline, then we mark bucket
// type as containing no pointers. This avoids scanning such maps.
// However, bmap.overflow is a pointer. In order to keep overflow buckets
// alive, we store pointers to all overflow buckets in hmap.extra.overflow and hmap.extra.oldoverflow.
// overflow and oldoverflow are only used if key and elem do not contain pointers.
// overflow contains overflow buckets for hmap.buckets.
// oldoverflow contains overflow buckets for hmap.oldbuckets.
// The indirection allows to store a pointer to the slice in hiter.
overflow *[]*bmap
oldoverflow *[]*bmap
// nextOverflow holds a pointer to a free overflow bucket.
nextOverflow *bmap
}
1.2 map创建
mapmake(map[KT]VT, hint intType)hintmapmap
var map1 = map[int]int{
1: 1,
}
func makeMapIntInt() map[int]int {
return make(map[int]int)
}
func makeMapIntIntWithHint(hint int) map[int]int {
return make(map[int]int, hint)
}
func main() {
_ = map1
map2 := make(map[int]int)
_ = map2
map3 := makeMapIntInt()
_ = map3
map4 := make(map[int]int, 9)
_ = map4
map5 := makeMapIntIntWithHint(9)
_ = map5
map6 := make(map[int]int, 53)
_ = map6
map7 := makeMapIntIntWithHint(53)
_ = map7
go tool compile -S main.go > main.i
maphintbucketCnt = 8map2
MOVD $""..autotmp_28-1200(SP), R16
MOVD $""..autotmp_28-1072(SP), R0
STP.P (ZR, ZR), 16(R16)
CMP R0, R16
BLE 44
PCDATA $1, ZR
CALL runtime.fastrand(SB)
maphintmap1makemap3makemap_small
maphinthmapB=3makemapextranilbuckets
hinthmap.B ≥ 4makemapextranilbuckets
func makemap64(t *maptype, hint int64, h *hmap) *hmap // makemap_small implements Go map creation for make(map[k]v) and // make(map[k]v, hint) when hint is known to be at most bucketCnt // at compile time and the map needs to be allocated on the heap. func makemap_small() *hmap // makemap implements Go map creation for make(map[k]v, hint). // If the compiler has determined that the map or the first bucket // can be created on the stack, h and/or bucket may be non-nil. // If h != nil, the map can be created directly in h. // If h.buckets != nil, bucket pointed to can be used as the first bucket. func makemap(t *maptype, hint int, h *hmap) *hmap
mapmakemap_smallmakemap
makemaphintmakemap_smallhmap
func makemap_small() *hmap {
h := new(hmap)
h.hash0 = fastrand()
return h
}
hmapbucketsbucketmap
m := make(map[int]int) m[1] = 1
hintmakemapmakemap
func makemap(t *maptype, hint int, h *hmap) *hmap {
mem, overflow := math.MulUintptr(uintptr(hint), t.bucket.size)
if overflow || mem > maxAlloc {
hint = 0
}
// initialize Hmap
if h == nil {
h = new(hmap)
}
h.hash0 = fastrand()
// Find the size parameter B which will hold the requested # of elements.
// For hint < 0 overLoadFactor returns false since hint < bucketCnt.
B := uint8(0)
for overLoadFactor(hint, B) {
B++
}
h.B = B
// allocate initial hash table
// if B == 0, the buckets field is allocated lazily later (in mapassign)
// If hint is large zeroing this memory could take a while.
if h.B != 0 {
var nextOverflow *bmap
h.buckets, nextOverflow = makeBucketArray(t, h.B, nil)
if nextOverflow != nil {
h.extra = new(mapextra)
h.extra.nextOverflow = nextOverflow
}
}
return h
}
makemaphinthintmaphintoverLoadFactorhintmap6.5bucketh.Bh.Btbucketshint=53h.B=4
m := make(map[int]int, 53)
hmap.Bextra.nextOverflowextra.overflowextra.oldoverflowmapkey-valueGC
1.3 写操作
mapkeyvaluevalueruntime.mapassignGo SDKkey
| key类型 | 插入函数 | 备注 |
|---|---|---|
| uint32 | runtime.mapassign_fast32 | |
| uint64 | runtime.mapassign_fast64 | int类型时也会用这个函数 |
| string | runtime.mapassign_faststr |
runtime.mapassign
if h == nil {
panic(plainError("assignment to entry in nil map"))
}
...
if h.flags&hashWriting != 0 {
throw("concurrent map writes")
}
hash := t.hasher(key, uintptr(h.hash0))
// Set hashWriting after calling t.hasher, since t.hasher may panic,
// in which case we have not actually done a write.
h.flags ^= hashWriting
mapassignmapmaphashhasherpanicbutpanicrecoverthrow
again:
bucket := hash & bucketMask(h.B)
if h.growing() {
growWork(t, h, bucket)
}
b := (*bmap)(add(h.buckets, bucket*uintptr(t.bucketsize)))
top := tophash(hash)
var inserti *uint8
var insertk unsafe.Pointer
var elem unsafe.Pointer
bucketMaskh.BbucketBbucketbinsertiinsertkelem
bucketloop:
for {
for i := uintptr(0); i < bucketCnt; i++ {
if b.tophash[i] != top {
if isEmpty(b.tophash[i]) && inserti == nil {
inserti = &b.tophash[i]
insertk = add(unsafe.Pointer(b), dataOffset+i*uintptr(t.keysize))
elem = add(unsafe.Pointer(b), dataOffset+bucketCnt*uintptr(t.keysize)+i*uintptr(t.elemsize))
}
if b.tophash[i] == emptyRest {
break bucketloop
}
continue
}
k := add(unsafe.Pointer(b), dataOffset+i*uintptr(t.keysize))
if t.indirectkey() {
k = *((*unsafe.Pointer)(k))
}
if !t.key.equal(key, k) {
continue
}
// already have a mapping for key. Update it.
if t.needkeyupdate() {
typedmemmove(t.key, k, key)
}
elem = add(unsafe.Pointer(b), dataOffset+bucketCnt*uintptr(t.keysize)+i*uintptr(t.elemsize))
goto done
}
ovf := b.overflow(t)
if ovf == nil {
break
}
b = ovf
}
tophashtophash[i]tophash[i]key-value
b.overflow(t)
// Did not find mapping for key. Allocate new cell & add entry.
// If we hit the max load factor or we have too many overflow buckets,
// and we're not already in the middle of growing, start growing.
if !h.growing() && (overLoadFactor(h.count+1, h.B) || tooManyOverflowBuckets(h.noverflow, h.B)) {
hashGrow(t, h)
goto again // Growing the table invalidates everything, so try again
}
if inserti == nil {
// The current bucket and all the overflow buckets connected to it are full, allocate a new one.
newb := h.newoverflow(t, b)
inserti = &newb.tophash[0]
insertk = add(unsafe.Pointer(newb), dataOffset)
elem = add(insertk, bucketCnt*uintptr(t.keysize))
}
// store new key/elem at insert position
if t.indirectkey() {
kmem := newobject(t.key)
*(*unsafe.Pointer)(insertk) = kmem
insertk = kmem
}
if t.indirectelem() {
vmem := newobject(t.elem)
*(*unsafe.Pointer)(elem) = vmem
}
typedmemmove(t.key, insertk, key)
*inserti = top
h.count++
overLoadFactor(h.count+1, h.B) || tooManyOverflowBuckets(h.noverflow, h.B)mapagain
Gomap
done:
if h.flags&hashWriting == 0 {
throw("concurrent map writes")
}
h.flags &^= hashWriting
if t.indirectelem() {
elem = *((*unsafe.Pointer)(elem))
}
return elem
value
1.4 读操作
v := m[k] // 如果存在对应 v,则返回 v;如果不存在,则返回 对应零值 v, ok := m[k] // 如果存在对应 v,则返回 v, true;如果不存在,则返回 对应零值, false
mapruntimemapaccess1mapaccess2Go SDK
| key类型 | 读取函数1 | 读取函数2 | 备注 |
|---|---|---|---|
| uint32 | runtime.mapaccess1_fast32 | runtime.mapaccess2_fast32 | |
| uint64 | runtime.mapaccess1_fast64 | runtime.mapaccess2_fast64 | int类型时也会用这个函数 |
| string | runtime.mapaccess1_faststr | runtime.mapaccess2_faststr |
mapaccess1map
if h == nil || h.count == 0 {
if t.hashMightPanic() {
t.hasher(key, 0) // see issue 23734
}
return unsafe.Pointer(&zeroVal[0])
}
if h.flags&hashWriting != 0 {
throw("concurrent map read and map write")
}
panic
bucketloop:
for ; b != nil; b = b.overflow(t) {
for i := uintptr(0); i < bucketCnt; i++ {
if b.tophash[i] != top {
if b.tophash[i] == emptyRest {
break bucketloop
}
continue
}
k := add(unsafe.Pointer(b), dataOffset+i*uintptr(t.keysize))
if t.indirectkey() {
k = *((*unsafe.Pointer)(k))
}
if t.key.equal(key, k) {
e := add(unsafe.Pointer(b), dataOffset+bucketCnt*uintptr(t.keysize)+i*uintptr(t.elemsize))
if t.indirectelem() {
e = *((*unsafe.Pointer)(e))
}
return e
}
}
}
return unsafe.Pointer(&zeroVal[0])
ikey
1.5 for-range操作
mapkeyelemstartBucketoffset
// A hash iteration structure.
// If you modify hiter, also change cmd/compile/internal/reflectdata/reflect.go
// and reflect/value.go to match the layout of this structure.
type hiter struct {
key unsafe.Pointer // Must be in first position. Write nil to indicate iteration end (see cmd/compile/internal/walk/range.go).
elem unsafe.Pointer // Must be in second position (see cmd/compile/internal/walk/range.go).
t *maptype
h *hmap
buckets unsafe.Pointer // bucket ptr at hash_iter initialization time
bptr *bmap // current bucket
overflow *[]*bmap // keeps overflow buckets of hmap.buckets alive
oldoverflow *[]*bmap // keeps overflow buckets of hmap.oldbuckets alive
startBucket uintptr // bucket iteration started at
offset uint8 // intra-bucket offset to start from during iteration (should be big enough to hold bucketCnt-1)
wrapped bool // already wrapped around from end of bucket array to beginning
B uint8
i uint8
bucket uintptr
checkBucket uintptr
}
1.5.1 注意遍历时的闭包
hiterfor-rangekeyelemkey-value
m := map[int]string{
1: "hello",
2: "world",
3: "hello",
4: "go",
}
wg := sync.WaitGroup{}
for k, v := range m {
wg.Add(1)
go func() {
defer wg.Done()
fmt.Println(k, v)
}()
}
wg.Wait()
map
4 go
4 go
4 go
4 go
1.5.2 map的遍历是无序的
maphiterstartBucketoffsethiterruntime.mapiterinit
// decide where to start
r := uintptr(fastrand())
if h.B > 31-bucketCntBits {
r += uintptr(fastrand()) << 31
}
it.startBucket = r & bucketMask(h.B)
it.offset = uint8(r >> h.B & (bucketCnt - 1))
// iterator state
it.bucket = it.startBucket
mapGo SDK
1.6 删除操作
mapruntime.mapdelete
| key类型 | 删除函数 | 备注 |
|---|---|---|
| uint32 | runtime.mapdelete_fast32 | |
| uint64 | runtime.mapdelete_fast64 | int类型时也会用这个函数 |
| string | runtime.mapdelete_faststr |
tophash[i]mapmap
// If the bucket now ends in a bunch of emptyOne states,
// change those to emptyRest states.
// It would be nice to make this a separate function, but
// for loops are not currently inlineable.
if i == bucketCnt-1 {
if b.overflow(t) != nil && b.overflow(t).tophash[0] != emptyRest {
goto notLast
}
} else {
if b.tophash[i+1] != emptyRest {
goto notLast
}
}
for {
b.tophash[i] = emptyRest
if i == 0 {
if b == bOrig {
break // beginning of initial bucket, we're done.
}
// Find previous bucket, continue at its last entry.
c := b
for b = bOrig; b.overflow(t) != c; b = b.overflow(t) {
}
i = bucketCnt - 1
} else {
i--
}
if b.tophash[i] != emptyOne {
break
}
}
notLast:
h.count--
tophash[i]emptyOne
1.7 扩容
map
overLoadFactor(h.count+1, h.B)h.count+1tooManyOverflowBuckets(h.noverflow, h.B)sameSizeGrowsameSizeGrow
runtime.hashGrowbiggerruntime.makeBucketArrayoldbucketsbucketsh.buckets
func hashGrow(t *maptype, h *hmap) {
// If we've hit the load factor, get bigger.
// Otherwise, there are too many overflow buckets,
// so keep the same number of buckets and "grow" laterally.
bigger := uint8(1)
if !overLoadFactor(h.count+1, h.B) {
bigger = 0
h.flags |= sameSizeGrow
}
oldbuckets := h.buckets
newbuckets, nextOverflow := makeBucketArray(t, h.B+bigger, nil)
flags := h.flags &^ (iterator | oldIterator)
if h.flags&iterator != 0 {
flags |= oldIterator
}
// commit the grow (atomic wrt gc)
h.B += bigger
h.flags = flags
h.oldbuckets = oldbuckets
h.buckets = newbuckets
h.nevacuate = 0
h.noverflow = 0
if h.extra != nil && h.extra.overflow != nil {
// Promote current overflow buckets to the old generation.
if h.extra.oldoverflow != nil {
throw("oldoverflow is not nil")
}
h.extra.oldoverflow = h.extra.overflow
h.extra.overflow = nil
}
if nextOverflow != nil {
if h.extra == nil {
h.extra = new(mapextra)
}
h.extra.nextOverflow = nextOverflow
}
// the actual copying of the hash table data is done incrementally
// by growWork() and evacuate().
}
runtime.growWorkhmapnevacuatebucketnevacuate
func growWork(t *maptype, h *hmap, bucket uintptr) {
// make sure we evacuate the oldbucket corresponding
// to the bucket we're about to use
evacuate(t, h, bucket&h.oldbucketmask())
// evacuate one more oldbucket to make progress on growing
if h.growing() {
evacuate(t, h, h.nevacuate)
}
}
runtime.evacuatenevacuateadvanceEvacuationMarkruntime.growWorknevacuateevacuate(t, h, h.nevacuate)if !evacuated(b)tophash[0]
func evacuate(t *maptype, h *hmap, oldbucket uintptr) {
b := (*bmap)(add(h.oldbuckets, oldbucket*uintptr(t.bucketsize)))
newbit := h.noldbuckets()
if !evacuated(b) {
...
}
if oldbucket == h.nevacuate {
advanceEvacuationMark(h, t, newbit)
}
}
runtime.evacuateruntime.evacDstruntime.evacDst
// TODO: reuse overflow buckets instead of using new ones, if there
// is no iterator using the old buckets. (If !oldIterator.)
// xy contains the x and y (low and high) evacuation destinations.
var xy [2]evacDst
x := &xy[0]
x.b = (*bmap)(add(h.buckets, oldbucket*uintptr(t.bucketsize)))
x.k = add(unsafe.Pointer(x.b), dataOffset)
x.e = add(x.k, bucketCnt*uintptr(t.keysize))
if !h.sameSizeGrow() {
// Only calculate y pointers if we're growing bigger.
// Otherwise GC can see bad pointers.
y := &xy[1]
y.b = (*bmap)(add(h.buckets, (oldbucket+newbit)*uintptr(t.bucketsize)))
y.k = add(unsafe.Pointer(y.b), dataOffset)
y.e = add(y.k, bucketCnt*uintptr(t.keysize))
}
bucketruntime.evacDsttypedmemmove
for ; b != nil; b = b.overflow(t) {
k := add(unsafe.Pointer(b), dataOffset)
e := add(k, bucketCnt*uintptr(t.keysize))
for i := 0; i < bucketCnt; i, k, e = i+1, add(k, uintptr(t.keysize)), add(e, uintptr(t.elemsize)) {
top := b.tophash[i]
if isEmpty(top) {
b.tophash[i] = evacuatedEmpty
continue
}
if top < minTopHash {
throw("bad map state")
}
k2 := k
if t.indirectkey() {
k2 = *((*unsafe.Pointer)(k2))
}
var useY uint8
if !h.sameSizeGrow() {
// Compute hash to make our evacuation decision (whether we need
// to send this key/elem to bucket x or bucket y).
hash := t.hasher(k2, uintptr(h.hash0))
if h.flags&iterator != 0 && !t.reflexivekey() && !t.key.equal(k2, k2) {
// If key != key (NaNs), then the hash could be (and probably
// will be) entirely different from the old hash. Moreover,
// it isn't reproducible. Reproducibility is required in the
// presence of iterators, as our evacuation decision must
// match whatever decision the iterator made.
// Fortunately, we have the freedom to send these keys either
// way. Also, tophash is meaningless for these kinds of keys.
// We let the low bit of tophash drive the evacuation decision.
// We recompute a new random tophash for the next level so
// these keys will get evenly distributed across all buckets
// after multiple grows.
useY = top & 1
top = tophash(hash)
} else {
if hash&newbit != 0 {
useY = 1
}
}
}
if evacuatedX+1 != evacuatedY || evacuatedX^1 != evacuatedY {
throw("bad evacuatedN")
}
b.tophash[i] = evacuatedX + useY // evacuatedX + 1 == evacuatedY
dst := &xy[useY] // evacuation destination
if dst.i == bucketCnt {
dst.b = h.newoverflow(t, dst.b)
dst.i = 0
dst.k = add(unsafe.Pointer(dst.b), dataOffset)
dst.e = add(dst.k, bucketCnt*uintptr(t.keysize))
}
dst.b.tophash[dst.i&(bucketCnt-1)] = top // mask dst.i as an optimization, to avoid a bounds check
if t.indirectkey() {
*(*unsafe.Pointer)(dst.k) = k2 // copy pointer
} else {
typedmemmove(t.key, dst.k, k) // copy elem
}
if t.indirectelem() {
*(*unsafe.Pointer)(dst.e) = *(*unsafe.Pointer)(e)
} else {
typedmemmove(t.elem, dst.e, e)
}
dst.i++
// These updates might push these pointers past the end of the
// key or elem arrays. That's ok, as we have the overflow pointer
// at the end of the bucket to protect against pointing past the
// end of the bucket.
dst.k = add(dst.k, uintptr(t.keysize))
dst.e = add(dst.e, uintptr(t.elemsize))
}
}
mask0b11hash & 0b11newbitshashhash & 0b100mask0b111XYhash&newbitif h.flags&iterator != 0 && !t.reflexivekey() && !t.key.equal(k2, k2)
hashmasknevacuateadvanceEvacuationMark(h, t, newbit)+1runtime.growWorkevacuatenevacuate
if oldbucket == h.nevacuate {
advanceEvacuationMark(h, t, newbit)
}
另外,在读表的时候,当判断旧桶还没有被迁移的时候,会从旧桶中取出数据。
func mapaccess1(t *maptype, h *hmap, key unsafe.Pointer) unsafe.Pointer {
...
hash := t.hasher(key, uintptr(h.hash0))
m := bucketMask(h.B)
b := (*bmap)(add(h.buckets, (hash&m)*uintptr(t.bucketsize)))
if c := h.oldbuckets; c != nil {
if !h.sameSizeGrow() {
// There used to be half as many buckets; mask down one more power of two.
m >>= 1
}
oldb := (*bmap)(add(c, (hash&m)*uintptr(t.bucketsize)))
if !evacuated(oldb) {
b = oldb
}
}
...
}
mapredisrehash
2. Map使用的一些注意事项
map
2.1 大数据量map不使用指针作为key-value
mapkvGCGCruntime._type.gcdatabitmap
// Needs to be in sync with ../cmd/link/internal/ld/decodesym.go:/^func.commonsize,
// ../cmd/compile/internal/reflectdata/reflect.go:/^func.dcommontype and
// ../reflect/type.go:/^type.rtype.
// ../internal/reflectlite/type.go:/^type.rtype.
type _type struct {
size uintptr
ptrdata uintptr // size of memory prefix holding all pointers
hash uint32
tflag tflag
align uint8
fieldAlign uint8
kind uint8
// function for comparing objects of this type
// (ptr to object A, ptr to object B) -> ==?
equal func(unsafe.Pointer, unsafe.Pointer) bool
// gcdata stores the GC type data for the garbage collector.
// If the KindGCProg bit is set in kind, gcdata is a GC program.
// Otherwise it is a ptrmask bitmap. See mbitmap.go for details.
gcdata *byte
str nameOff
ptrToThis typeOff
}
value
func TestGCTimeWithoutPointer(t *testing.T) {
for _, N := range Ns {
runtime.GC()
m1 := make(map[int]int)
for i := 0; i < N; i++ {
m1[i] = rand.Int()
}
start := time.Now()
runtime.GC()
delta := time.Since(start)
t.Logf("GC without pointer spend %+v, when N = %d", delta, N)
runtime.KeepAlive(m1)
}
}
func TestGCTimeWithPointer(t *testing.T) {
for _, N := range Ns {
runtime.GC()
m2 := make(map[int]*int)
for i := 0; i < N; i++ {
val := rand.Int()
m2[i] = &val
}
start := time.Now()
runtime.GC()
delta := time.Since(start)
t.Logf("GC with pointer spend %+v, when N = %d", delta, N)
runtime.KeepAlive(m2)
}
}
GC
=== RUN TestGCTimeWithoutPointer map_test.go:63: GC without pointer spend 252.208µs, when N = 10 map_test.go:63: GC without pointer spend 297.292µs, when N = 100 map_test.go:63: GC without pointer spend 438.208µs, when N = 1000 map_test.go:63: GC without pointer spend 377µs, when N = 10000 map_test.go:63: GC without pointer spend 205.666µs, when N = 100000 map_test.go:63: GC without pointer spend 380.584µs, when N = 1000000 --- PASS: TestGCTimeWithoutPointer (0.13s) === RUN TestGCTimeWithPointer map_test.go:81: GC with pointer spend 570.041µs, when N = 10 map_test.go:81: GC with pointer spend 325.708µs, when N = 100 map_test.go:81: GC with pointer spend 287.542µs, when N = 1000 map_test.go:81: GC with pointer spend 476.709µs, when N = 10000 map_test.go:81: GC with pointer spend 1.714833ms, when N = 100000 map_test.go:81: GC with pointer spend 15.756958ms, when N = 1000000 --- PASS: TestGCTimeWithPointer (0.18s)
hmap.extra.overflowGC
这一点也同样适用于其他容器类型,比如切片、数组和通道。
2.1.1 引申1——使用字节数组代替字符串作为key
keyN[N]byte
2.2 清空表操作
map
m = nil // 后续不再使用 m = make(map[K]V) // 后续继续使用
2.3 确定大小时尽量传入hint
hintGo SDK
知识补充
HashMap拉链法简介
1.拉链法用途
解决hash冲突(即put操作时计算key值问题)。
2.拉链法原理
把具有相同散列地址的关键字(同义词)值放在同一个单链表中,称为同义词链表。
有m个散列地址就有m个链表,同时用指针数组A[0,1,2…m-1]存放各个链表的头指针,凡是散列地址为i的记录都以结点方式插入到以A[i]为指针的单链表中。A中各分量的初值为空指针。
3.拉链法原理解释
HashMap是一个数组,数组中的每个元素是链表。put元素进去的时候,会通过计算key的hash值来获取到一个index,根据index找到数组中的位置,进行元素插入。当新来的元素映射到冲突的数组位置时,就会插入到链表的头部。
HashMap采用拉链法将HashMap的key是转化成了hashcode,但hashcode可能重复,所以采用求交集方式解决冲突。
4.举例如下
有序集合a1={1,3,5,7,8,9},有序集合a2={2,3,4,5,6,7}
两个指针指向首元素,比较元素的大小:
(1)如果相同,放入结果集,随意移动一个指针
(2)否则,移动值较小的一个指针,直到队尾
好处:
(1)集合中的元素最多被比较一次,时间复杂度为O(n)。
(2)多个有序集合可以同时进行,这适用于多个分词的item求url_id交集。
这个方法就像一条拉链的两边齿轮,然后逐个对比,故称为拉链法。
感谢各位的阅读,以上就是“Golang基础学习之map的实现原理是什么”的内容了,经过本文的学习后,相信大家对Golang基础学习之map的实现原理是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是本站,小编将为大家推送更多相关知识点的文章,欢迎关注!