Skip to content

Commit 1ae5028

Browse files
committed
update map
1 parent 3511c51 commit 1ae5028

File tree

1 file changed

+73
-1
lines changed

1 file changed

+73
-1
lines changed

map.md

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,79 @@ func makemap(t *maptype, hint int, h *hmap) *hmap {
219219

220220
## 元素访问
221221

222-
元素访问有 mapaccess1,mapaccess2,mapaccessK,但几个方法都差不多,只差别在返回内容上。mapaccess1 和 mapaccess2 同时有 32 位、64 位和 string 类型的变种:
222+
主要是对 key 进行 hash 计算,计算后用 low bits 和高 8 位 hash 找到对应的位置:
223+
224+
```
225+
┌─────────────┬─────────────────────────────────────────────────────┬─────────────┐
226+
│ 10010111 │ 000011110110110010001111001010100010010110010101010 │ 01010 │
227+
└─────────────┴─────────────────────────────────────────────────────┴─────────────┘
228+
▲ ▲
229+
│ │
230+
│ │ B = 5
231+
│ │ bucketMask = 11111
232+
│ │
233+
│ ┌─────────────┬───┬─────────────┐
234+
│ │ low bits │ & │ bucketMask │
235+
│ ├─────────────┴───┴─────────────┤
236+
│ │ 01010 & 11111 │
237+
│ └───────────────────────────────┘
238+
│ │
239+
│ │
240+
│ │
241+
│ ▼
242+
│ ┌───────────────────┐
243+
│ │ 01010 = 12 │
244+
│ └───────────────────┘
245+
│ │
246+
│ │
247+
│ │
248+
│ │
249+
│ │
250+
┌─────────────┐ │
251+
│ tophash │ │
252+
└─────────────┘ ┌─────────┐ │
253+
│ │ buckets │ ▼
254+
│ ├───┬───┬─┴─┬───┬───┬───┬───┬───┬───┬───┬───┬───┐ ┌───┬───┬───┐
255+
│ │ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │ 9 │10 │11 │ ...│29 │30 │31 │
256+
│ └───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘ └───┴───┴───┘
257+
▼ │
258+
┌──────────────────┐ │
259+
│ 10010111 = 151 │─────────────┐ │
260+
└──────────────────┘ │ │
261+
│ │
262+
│ │
263+
│ ┌───────────────────────┘
264+
│ │
265+
│ │
266+
│ │
267+
│ │
268+
│ │
269+
│ │
270+
│ ▼
271+
│ ┌─────────────┐
272+
│ │ bucket │
273+
┌─────────────────────────────┼──────────────────────┴─────────────┤
274+
│ │ │
275+
│ ┌─────────┐ │ │
276+
│ │ tophash │ ▼ │
277+
│ ├───────┬─┴─────┬───────┬───────┬───────┬───────┬───────┬───────┐│
278+
│ │ 124 │ 33 │ 41 │ 151 │ 1 │ 0 │ 0 │ 0 ││
279+
│ ├───────┴─┬─────┴───────┴───────┴───────┴───────┴───────┴───────┘│
280+
│ │ 1 │ │
281+
│ ├───────┬─┴─────┬───────┬───────┬───────┬───────┬───────┬───────┐│
282+
│ │ 124 │ 412 │ 423 │ 5 │ 14 │ 0 │ 0 │ 0 ││
283+
│ ├───────┴─┬─────┴───────┴───────┴───────┴───────┴───────┴───────┘│
284+
│ │ values │ │
285+
│ ├───────┬─┴─────┬───────┬───────┬───────┬───────┬───────┬───────┐│
286+
│ │ v0 │ v1 │ v2 │ v3 │ v4 │ 0 │ 0 │ 0 ││
287+
│ ├───────┴───────┴──┬────┴───────┴───────┴───────┴───────┴───────┘│
288+
│ │ overflow pointer │ │
289+
│ └──────────────────┘ │
290+
│ │
291+
└──────────────────────────────────────────────────────────────────┘
292+
```
293+
294+
实现上有 mapaccess1,mapaccess2,mapaccessK 几个方法,但几个方法都差不多,只差别在返回内容上。mapaccess1 和 mapaccess2 同时有 32 位、64 位和 string 类型的变种:
223295

224296
1. mapaccess1_fast32, mapaccess1_fast64, mapaccess1_faststr
225297
2. mapaccess2_fast32, mapaccess2_fast64, mapaccess2_faststr

0 commit comments

Comments
 (0)