Debian Codegraph @

libgo/go/runtime/map_faststr.go

raw
1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package runtime
6
7 import (
8 "internal/abi"
9 "internal/goarch"
10 "unsafe"
11 )
12
13 // For gccgo, use go:linkname to export compiler-called functions.
14 //
15 //go:linkname mapaccess1_faststr
16 //go:linkname mapaccess2_faststr
17 //go:linkname mapassign_faststr
18 //go:linkname mapdelete_faststr
19
20 func mapaccess1_faststr(t *maptype, h *hmap, ky string) unsafe.Pointer {
21 if raceenabled && h != nil {
22 callerpc := getcallerpc()
23 racereadpc(unsafe.Pointer(h), callerpc, abi.FuncPCABIInternal(mapaccess1_faststr))
24 }
25 if h == nil || h.count == 0 {
26 return unsafe.Pointer(&zeroVal[0])
27 }
28 if h.flags&hashWriting != 0 {
29 throw("concurrent map read and map write")
30 }
31 key := stringStructOf(&ky)
32 if h.B == 0 {
33 // One-bucket table.
34 b := (*bmap)(h.buckets)
35 if key.len < 32 {
36 // short key, doing lots of comparisons is ok
37 for i, kptr := uintptr(0), b.keys(); i < bucketCnt; i, kptr = i+1, add(kptr, 2*goarch.PtrSize) {
38 k := (*stringStruct)(kptr)
39 if k.len != key.len || isEmpty(b.tophash[i]) {
40 if b.tophash[i] == emptyRest {
41 break
42 }
43 continue
44 }
45 if k.str == key.str || memequal(k.str, key.str, uintptr(key.len)) {
46 return add(unsafe.Pointer(b), dataOffset+bucketCnt*2*goarch.PtrSize+i*uintptr(t.elemsize))
47 }
48 }
49 return unsafe.Pointer(&zeroVal[0])
50 }
51 // long key, try not to do more comparisons than necessary
52 keymaybe := uintptr(bucketCnt)
53 for i, kptr := uintptr(0), b.keys(); i < bucketCnt; i, kptr = i+1, add(kptr, 2*goarch.PtrSize) {
54 k := (*stringStruct)(kptr)
55 if k.len != key.len || isEmpty(b.tophash[i]) {
56 if b.tophash[i] == emptyRest {
57 break
58 }
59 continue
60 }
61 if k.str == key.str {
62 return add(unsafe.Pointer(b), dataOffset+bucketCnt*2*goarch.PtrSize+i*uintptr(t.elemsize))
63 }
64 // check first 4 bytes
65 if *((*[4]byte)(key.str)) != *((*[4]byte)(k.str)) {
66 continue
67 }
68 // check last 4 bytes
69 if *((*[4]byte)(add(key.str, uintptr(key.len)-4))) != *((*[4]byte)(add(k.str, uintptr(key.len)-4))) {
70 continue
71 }
72 if keymaybe != bucketCnt {
73 // Two keys are potential matches. Use hash to distinguish them.
74 goto dohash
75 }
76 keymaybe = i
77 }
78 if keymaybe != bucketCnt {
79 k := (*stringStruct)(add(unsafe.Pointer(b), dataOffset+keymaybe*2*goarch.PtrSize))
80 if memequal(k.str, key.str, uintptr(key.len)) {
81 return add(unsafe.Pointer(b), dataOffset+bucketCnt*2*goarch.PtrSize+keymaybe*uintptr(t.elemsize))
82 }
83 }
84 return unsafe.Pointer(&zeroVal[0])
85 }
86 dohash:
87 hash := t.hasher(noescape(unsafe.Pointer(&ky)), uintptr(h.hash0))
88 m := bucketMask(h.B)
89 b := (*bmap)(add(h.buckets, (hash&m)*uintptr(t.bucketsize)))
90 if c := h.oldbuckets; c != nil {
91 if !h.sameSizeGrow() {
92 // There used to be half as many buckets; mask down one more power of two.
93 m >>= 1
94 }
95 oldb := (*bmap)(add(c, (hash&m)*uintptr(t.bucketsize)))
96 if !evacuated(oldb) {
97 b = oldb
98 }
99 }
100 top := tophash(hash)
101 for ; b != nil; b = b.overflow(t) {
102 for i, kptr := uintptr(0), b.keys(); i < bucketCnt; i, kptr = i+1, add(kptr, 2*goarch.PtrSize) {
103 k := (*stringStruct)(kptr)
104 if k.len != key.len || b.tophash[i] != top {
105 continue
106 }
107 if k.str == key.str || memequal(k.str, key.str, uintptr(key.len)) {
108 return add(unsafe.Pointer(b), dataOffset+bucketCnt*2*goarch.PtrSize+i*uintptr(t.elemsize))
109 }
110 }
111 }
112 return unsafe.Pointer(&zeroVal[0])
113 }
114
115 func mapaccess2_faststr(t *maptype, h *hmap, ky string) (unsafe.Pointer, bool) {
116 if raceenabled && h != nil {
117 callerpc := getcallerpc()
118 racereadpc(unsafe.Pointer(h), callerpc, abi.FuncPCABIInternal(mapaccess2_faststr))
119 }
120 if h == nil || h.count == 0 {
121 return unsafe.Pointer(&zeroVal[0]), false
122 }
123 if h.flags&hashWriting != 0 {
124 throw("concurrent map read and map write")
125 }
126 key := stringStructOf(&ky)
127 if h.B == 0 {
128 // One-bucket table.
129 b := (*bmap)(h.buckets)
130 if key.len < 32 {
131 // short key, doing lots of comparisons is ok
132 for i, kptr := uintptr(0), b.keys(); i < bucketCnt; i, kptr = i+1, add(kptr, 2*goarch.PtrSize) {
133 k := (*stringStruct)(kptr)
134 if k.len != key.len || isEmpty(b.tophash[i]) {
135 if b.tophash[i] == emptyRest {
136 break
137 }
138 continue
139 }
140 if k.str == key.str || memequal(k.str, key.str, uintptr(key.len)) {
141 return add(unsafe.Pointer(b), dataOffset+bucketCnt*2*goarch.PtrSize+i*uintptr(t.elemsize)), true
142 }
143 }
144 return unsafe.Pointer(&zeroVal[0]), false
145 }
146 // long key, try not to do more comparisons than necessary
147 keymaybe := uintptr(bucketCnt)
148 for i, kptr := uintptr(0), b.keys(); i < bucketCnt; i, kptr = i+1, add(kptr, 2*goarch.PtrSize) {
149 k := (*stringStruct)(kptr)
150 if k.len != key.len || isEmpty(b.tophash[i]) {
151 if b.tophash[i] == emptyRest {
152 break
153 }
154 continue
155 }
156 if k.str == key.str {
157 return add(unsafe.Pointer(b), dataOffset+bucketCnt*2*goarch.PtrSize+i*uintptr(t.elemsize)), true
158 }
159 // check first 4 bytes
160 if *((*[4]byte)(key.str)) != *((*[4]byte)(k.str)) {
161 continue
162 }
163 // check last 4 bytes
164 if *((*[4]byte)(add(key.str, uintptr(key.len)-4))) != *((*[4]byte)(add(k.str, uintptr(key.len)-4))) {
165 continue
166 }
167 if keymaybe != bucketCnt {
168 // Two keys are potential matches. Use hash to distinguish them.
169 goto dohash
170 }
171 keymaybe = i
172 }
173 if keymaybe != bucketCnt {
174 k := (*stringStruct)(add(unsafe.Pointer(b), dataOffset+keymaybe*2*goarch.PtrSize))
175 if memequal(k.str, key.str, uintptr(key.len)) {
176 return add(unsafe.Pointer(b), dataOffset+bucketCnt*2*goarch.PtrSize+keymaybe*uintptr(t.elemsize)), true
177 }
178 }
179 return unsafe.Pointer(&zeroVal[0]), false
180 }
181 dohash:
182 hash := t.hasher(noescape(unsafe.Pointer(&ky)), uintptr(h.hash0))
183 m := bucketMask(h.B)
184 b := (*bmap)(add(h.buckets, (hash&m)*uintptr(t.bucketsize)))
185 if c := h.oldbuckets; c != nil {
186 if !h.sameSizeGrow() {
187 // There used to be half as many buckets; mask down one more power of two.
188 m >>= 1
189 }
190 oldb := (*bmap)(add(c, (hash&m)*uintptr(t.bucketsize)))
191 if !evacuated(oldb) {
192 b = oldb
193 }
194 }
195 top := tophash(hash)
196 for ; b != nil; b = b.overflow(t) {
197 for i, kptr := uintptr(0), b.keys(); i < bucketCnt; i, kptr = i+1, add(kptr, 2*goarch.PtrSize) {
198 k := (*stringStruct)(kptr)
199 if k.len != key.len || b.tophash[i] != top {
200 continue
201 }
202 if k.str == key.str || memequal(k.str, key.str, uintptr(key.len)) {
203 return add(unsafe.Pointer(b), dataOffset+bucketCnt*2*goarch.PtrSize+i*uintptr(t.elemsize)), true
204 }
205 }
206 }
207 return unsafe.Pointer(&zeroVal[0]), false
208 }
209
210 func mapassign_faststr(t *maptype, h *hmap, s string) unsafe.Pointer {
211 if h == nil {
212 panic(plainError("assignment to entry in nil map"))
213 }
214 if raceenabled {
215 callerpc := getcallerpc()
216 racewritepc(unsafe.Pointer(h), callerpc, abi.FuncPCABIInternal(mapassign_faststr))
217 }
218 if h.flags&hashWriting != 0 {
219 throw("concurrent map writes")
220 }
221 key := stringStructOf(&s)
222 hash := t.hasher(noescape(unsafe.Pointer(&s)), uintptr(h.hash0))
223
224 // Set hashWriting after calling t.hasher for consistency with mapassign.
225 h.flags ^= hashWriting
226
227 if h.buckets == nil {
228 h.buckets = newobject(t.bucket) // newarray(t.bucket, 1)
229 }
230
231 again:
232 bucket := hash & bucketMask(h.B)
233 if h.growing() {
234 growWork_faststr(t, h, bucket)
235 }
236 b := (*bmap)(add(h.buckets, bucket*uintptr(t.bucketsize)))
237 top := tophash(hash)
238
239 var insertb *bmap
240 var inserti uintptr
241 var insertk unsafe.Pointer
242
243 bucketloop:
244 for {
245 for i := uintptr(0); i < bucketCnt; i++ {
246 if b.tophash[i] != top {
247 if isEmpty(b.tophash[i]) && insertb == nil {
248 insertb = b
249 inserti = i
250 }
251 if b.tophash[i] == emptyRest {
252 break bucketloop
253 }
254 continue
255 }
256 k := (*stringStruct)(add(unsafe.Pointer(b), dataOffset+i*2*goarch.PtrSize))
257 if k.len != key.len {
258 continue
259 }
260 if k.str != key.str && !memequal(k.str, key.str, uintptr(key.len)) {
261 continue
262 }
263 // already have a mapping for key. Update it.
264 inserti = i
265 insertb = b
266 // Overwrite existing key, so it can be garbage collected.
267 // The size is already guaranteed to be set correctly.
268 k.str = key.str
269 goto done
270 }
271 ovf := b.overflow(t)
272 if ovf == nil {
273 break
274 }
275 b = ovf
276 }
277
278 // Did not find mapping for key. Allocate new cell & add entry.
279
280 // If we hit the max load factor or we have too many overflow buckets,
281 // and we're not already in the middle of growing, start growing.
282 if !h.growing() && (overLoadFactor(h.count+1, h.B) || tooManyOverflowBuckets(h.noverflow, h.B)) {
283 hashGrow(t, h)
284 goto again // Growing the table invalidates everything, so try again
285 }
286
287 if insertb == nil {
288 // The current bucket and all the overflow buckets connected to it are full, allocate a new one.
289 insertb = h.newoverflow(t, b)
290 inserti = 0 // not necessary, but avoids needlessly spilling inserti
291 }
292 insertb.tophash[inserti&(bucketCnt-1)] = top // mask inserti to avoid bounds checks
293
294 insertk = add(unsafe.Pointer(insertb), dataOffset+inserti*2*goarch.PtrSize)
295 // store new key at insert position
296 *((*stringStruct)(insertk)) = *key
297 h.count++
298
299 done:
300 elem := add(unsafe.Pointer(insertb), dataOffset+bucketCnt*2*goarch.PtrSize+inserti*uintptr(t.elemsize))
301 if h.flags&hashWriting == 0 {
302 throw("concurrent map writes")
303 }
304 h.flags &^= hashWriting
305 return elem
306 }
307
308 func mapdelete_faststr(t *maptype, h *hmap, ky string) {
309 if raceenabled && h != nil {
310 callerpc := getcallerpc()
311 racewritepc(unsafe.Pointer(h), callerpc, abi.FuncPCABIInternal(mapdelete_faststr))
312 }
313 if h == nil || h.count == 0 {
314 return
315 }
316 if h.flags&hashWriting != 0 {
317 throw("concurrent map writes")
318 }
319
320 key := stringStructOf(&ky)
321 hash := t.hasher(noescape(unsafe.Pointer(&ky)), uintptr(h.hash0))
322
323 // Set hashWriting after calling t.hasher for consistency with mapdelete
324 h.flags ^= hashWriting
325
326 bucket := hash & bucketMask(h.B)
327 if h.growing() {
328 growWork_faststr(t, h, bucket)
329 }
330 b := (*bmap)(add(h.buckets, bucket*uintptr(t.bucketsize)))
331 bOrig := b
332 top := tophash(hash)
333 search:
334 for ; b != nil; b = b.overflow(t) {
335 for i, kptr := uintptr(0), b.keys(); i < bucketCnt; i, kptr = i+1, add(kptr, 2*goarch.PtrSize) {
336 k := (*stringStruct)(kptr)
337 if k.len != key.len || b.tophash[i] != top {
338 continue
339 }
340 if k.str != key.str && !memequal(k.str, key.str, uintptr(key.len)) {
341 continue
342 }
343 // Clear key's pointer.
344 k.str = nil
345 e := add(unsafe.Pointer(b), dataOffset+bucketCnt*2*goarch.PtrSize+i*uintptr(t.elemsize))
346 if t.elem.ptrdata != 0 {
347 memclrHasPointers(e, t.elem.size)
348 } else {
349 memclrNoHeapPointers(e, t.elem.size)
350 }
351 b.tophash[i] = emptyOne
352 // If the bucket now ends in a bunch of emptyOne states,
353 // change those to emptyRest states.
354 if i == bucketCnt-1 {
355 if b.overflow(t) != nil && b.overflow(t).tophash[0] != emptyRest {
356 goto notLast
357 }
358 } else {
359 if b.tophash[i+1] != emptyRest {
360 goto notLast
361 }
362 }
363 for {
364 b.tophash[i] = emptyRest
365 if i == 0 {
366 if b == bOrig {
367 break // beginning of initial bucket, we're done.
368 }
369 // Find previous bucket, continue at its last entry.
370 c := b
371 for b = bOrig; b.overflow(t) != c; b = b.overflow(t) {
372 }
373 i = bucketCnt - 1
374 } else {
375 i--
376 }
377 if b.tophash[i] != emptyOne {
378 break
379 }
380 }
381 notLast:
382 h.count--
383 // Reset the hash seed to make it more difficult for attackers to
384 // repeatedly trigger hash collisions. See issue 25237.
385 if h.count == 0 {
386 h.hash0 = fastrand()
387 }
388 break search
389 }
390 }
391
392 if h.flags&hashWriting == 0 {
393 throw("concurrent map writes")
394 }
395 h.flags &^= hashWriting
396 }
397
398 func growWork_faststr(t *maptype, h *hmap, bucket uintptr) {
399 // make sure we evacuate the oldbucket corresponding
400 // to the bucket we're about to use
401 evacuate_faststr(t, h, bucket&h.oldbucketmask())
402
403 // evacuate one more oldbucket to make progress on growing
404 if h.growing() {
405 evacuate_faststr(t, h, h.nevacuate)
406 }
407 }
408
409 func evacuate_faststr(t *maptype, h *hmap, oldbucket uintptr) {
410 b := (*bmap)(add(h.oldbuckets, oldbucket*uintptr(t.bucketsize)))
411 newbit := h.noldbuckets()
412 if !evacuated(b) {
413 // TODO: reuse overflow buckets instead of using new ones, if there
414 // is no iterator using the old buckets. (If !oldIterator.)
415
416 // xy contains the x and y (low and high) evacuation destinations.
417 var xy [2]evacDst
418 x := &xy[0]
419 x.b = (*bmap)(add(h.buckets, oldbucket*uintptr(t.bucketsize)))
420 x.k = add(unsafe.Pointer(x.b), dataOffset)
421 x.e = add(x.k, bucketCnt*2*goarch.PtrSize)
422
423 if !h.sameSizeGrow() {
424 // Only calculate y pointers if we're growing bigger.
425 // Otherwise GC can see bad pointers.
426 y := &xy[1]
427 y.b = (*bmap)(add(h.buckets, (oldbucket+newbit)*uintptr(t.bucketsize)))
428 y.k = add(unsafe.Pointer(y.b), dataOffset)
429 y.e = add(y.k, bucketCnt*2*goarch.PtrSize)
430 }
431
432 for ; b != nil; b = b.overflow(t) {
433 k := add(unsafe.Pointer(b), dataOffset)
434 e := add(k, bucketCnt*2*goarch.PtrSize)
435 for i := 0; i < bucketCnt; i, k, e = i+1, add(k, 2*goarch.PtrSize), add(e, uintptr(t.elemsize)) {
436 top := b.tophash[i]
437 if isEmpty(top) {
438 b.tophash[i] = evacuatedEmpty
439 continue
440 }
441 if top < minTopHash {
442 throw("bad map state")
443 }
444 var useY uint8
445 if !h.sameSizeGrow() {
446 // Compute hash to make our evacuation decision (whether we need
447 // to send this key/elem to bucket x or bucket y).
448 hash := t.hasher(k, uintptr(h.hash0))
449 if hash&newbit != 0 {
450 useY = 1
451 }
452 }
453
454 b.tophash[i] = evacuatedX + useY // evacuatedX + 1 == evacuatedY, enforced in makemap
455 dst := &xy[useY] // evacuation destination
456
457 if dst.i == bucketCnt {
458 dst.b = h.newoverflow(t, dst.b)
459 dst.i = 0
460 dst.k = add(unsafe.Pointer(dst.b), dataOffset)
461 dst.e = add(dst.k, bucketCnt*2*goarch.PtrSize)
462 }
463 dst.b.tophash[dst.i&(bucketCnt-1)] = top // mask dst.i as an optimization, to avoid a bounds check
464
465 // Copy key.
466 *(*string)(dst.k) = *(*string)(k)
467
468 typedmemmove(t.elem, dst.e, e)
469 dst.i++
470 // These updates might push these pointers past the end of the
471 // key or elem arrays. That's ok, as we have the overflow pointer
472 // at the end of the bucket to protect against pointing past the
473 // end of the bucket.
474 dst.k = add(dst.k, 2*goarch.PtrSize)
475 dst.e = add(dst.e, uintptr(t.elemsize))
476 }
477 }
478 // Unlink the overflow buckets & clear key/elem to help GC.
479 if h.flags&oldIterator == 0 && t.bucket.ptrdata != 0 {
480 b := add(h.oldbuckets, oldbucket*uintptr(t.bucketsize))
481 // Preserve b.tophash because the evacuation
482 // state is maintained there.
483 ptr := add(b, dataOffset)
484 n := uintptr(t.bucketsize) - dataOffset
485 memclrHasPointers(ptr, n)
486 }
487 }
488
489 if oldbucket == h.nevacuate {
490 advanceEvacuationMark(h, t, newbit)
491 }
492 }