Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 1 | // Copyright 2014 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 | "unsafe" |
| 9 | ) |
| 10 | |
| 11 | func mapaccess1_fast32(t *maptype, h *hmap, key uint32) unsafe.Pointer { |
| 12 | if raceenabled && h != nil { |
Russ Cox | d21638b | 2014-08-27 21:59:49 -0400 | [diff] [blame] | 13 | callerpc := getcallerpc(unsafe.Pointer(&t)) |
Russ Cox | 0e07f1c | 2014-09-03 11:10:38 -0400 | [diff] [blame] | 14 | racereadpc(unsafe.Pointer(h), callerpc, funcPC(mapaccess1_fast32)) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 15 | } |
| 16 | if h == nil || h.count == 0 { |
Michael Hudson-Doyle | 38519e6 | 2015-08-21 14:54:55 +1200 | [diff] [blame] | 17 | return atomicloadp(unsafe.Pointer(&zeroptr)) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 18 | } |
| 19 | var b *bmap |
| 20 | if h.B == 0 { |
| 21 | // One-bucket table. No need to hash. |
| 22 | b = (*bmap)(h.buckets) |
| 23 | } else { |
Keith Randall | d5e4c40 | 2015-01-06 16:42:48 -0800 | [diff] [blame] | 24 | hash := t.key.alg.hash(noescape(unsafe.Pointer(&key)), uintptr(h.hash0)) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 25 | m := uintptr(1)<<h.B - 1 |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 26 | b = (*bmap)(add(h.buckets, (hash&m)*uintptr(t.bucketsize))) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 27 | if c := h.oldbuckets; c != nil { |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 28 | oldb := (*bmap)(add(c, (hash&(m>>1))*uintptr(t.bucketsize))) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 29 | if !evacuated(oldb) { |
| 30 | b = oldb |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | for { |
| 35 | for i := uintptr(0); i < bucketCnt; i++ { |
| 36 | k := *((*uint32)(add(unsafe.Pointer(b), dataOffset+i*4))) |
| 37 | if k != key { |
| 38 | continue |
| 39 | } |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 40 | x := *((*uint8)(add(unsafe.Pointer(b), i))) // b.topbits[i] without the bounds check |
| 41 | if x == empty { |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 42 | continue |
| 43 | } |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 44 | return add(unsafe.Pointer(b), dataOffset+bucketCnt*4+i*uintptr(t.valuesize)) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 45 | } |
Keith Randall | fbc56cf | 2014-12-19 20:44:18 -0800 | [diff] [blame] | 46 | b = b.overflow(t) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 47 | if b == nil { |
Michael Hudson-Doyle | 38519e6 | 2015-08-21 14:54:55 +1200 | [diff] [blame] | 48 | return atomicloadp(unsafe.Pointer(&zeroptr)) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func mapaccess2_fast32(t *maptype, h *hmap, key uint32) (unsafe.Pointer, bool) { |
| 54 | if raceenabled && h != nil { |
Russ Cox | d21638b | 2014-08-27 21:59:49 -0400 | [diff] [blame] | 55 | callerpc := getcallerpc(unsafe.Pointer(&t)) |
Russ Cox | 0e07f1c | 2014-09-03 11:10:38 -0400 | [diff] [blame] | 56 | racereadpc(unsafe.Pointer(h), callerpc, funcPC(mapaccess2_fast32)) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 57 | } |
| 58 | if h == nil || h.count == 0 { |
Michael Hudson-Doyle | 38519e6 | 2015-08-21 14:54:55 +1200 | [diff] [blame] | 59 | return atomicloadp(unsafe.Pointer(&zeroptr)), false |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 60 | } |
| 61 | var b *bmap |
| 62 | if h.B == 0 { |
| 63 | // One-bucket table. No need to hash. |
| 64 | b = (*bmap)(h.buckets) |
| 65 | } else { |
Keith Randall | d5e4c40 | 2015-01-06 16:42:48 -0800 | [diff] [blame] | 66 | hash := t.key.alg.hash(noescape(unsafe.Pointer(&key)), uintptr(h.hash0)) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 67 | m := uintptr(1)<<h.B - 1 |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 68 | b = (*bmap)(add(h.buckets, (hash&m)*uintptr(t.bucketsize))) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 69 | if c := h.oldbuckets; c != nil { |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 70 | oldb := (*bmap)(add(c, (hash&(m>>1))*uintptr(t.bucketsize))) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 71 | if !evacuated(oldb) { |
| 72 | b = oldb |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | for { |
| 77 | for i := uintptr(0); i < bucketCnt; i++ { |
| 78 | k := *((*uint32)(add(unsafe.Pointer(b), dataOffset+i*4))) |
| 79 | if k != key { |
| 80 | continue |
| 81 | } |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 82 | x := *((*uint8)(add(unsafe.Pointer(b), i))) // b.topbits[i] without the bounds check |
| 83 | if x == empty { |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 84 | continue |
| 85 | } |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 86 | return add(unsafe.Pointer(b), dataOffset+bucketCnt*4+i*uintptr(t.valuesize)), true |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 87 | } |
Keith Randall | fbc56cf | 2014-12-19 20:44:18 -0800 | [diff] [blame] | 88 | b = b.overflow(t) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 89 | if b == nil { |
Michael Hudson-Doyle | 38519e6 | 2015-08-21 14:54:55 +1200 | [diff] [blame] | 90 | return atomicloadp(unsafe.Pointer(&zeroptr)), false |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func mapaccess1_fast64(t *maptype, h *hmap, key uint64) unsafe.Pointer { |
| 96 | if raceenabled && h != nil { |
Russ Cox | d21638b | 2014-08-27 21:59:49 -0400 | [diff] [blame] | 97 | callerpc := getcallerpc(unsafe.Pointer(&t)) |
Russ Cox | 0e07f1c | 2014-09-03 11:10:38 -0400 | [diff] [blame] | 98 | racereadpc(unsafe.Pointer(h), callerpc, funcPC(mapaccess1_fast64)) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 99 | } |
| 100 | if h == nil || h.count == 0 { |
Michael Hudson-Doyle | 38519e6 | 2015-08-21 14:54:55 +1200 | [diff] [blame] | 101 | return atomicloadp(unsafe.Pointer(&zeroptr)) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 102 | } |
| 103 | var b *bmap |
| 104 | if h.B == 0 { |
| 105 | // One-bucket table. No need to hash. |
| 106 | b = (*bmap)(h.buckets) |
| 107 | } else { |
Keith Randall | d5e4c40 | 2015-01-06 16:42:48 -0800 | [diff] [blame] | 108 | hash := t.key.alg.hash(noescape(unsafe.Pointer(&key)), uintptr(h.hash0)) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 109 | m := uintptr(1)<<h.B - 1 |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 110 | b = (*bmap)(add(h.buckets, (hash&m)*uintptr(t.bucketsize))) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 111 | if c := h.oldbuckets; c != nil { |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 112 | oldb := (*bmap)(add(c, (hash&(m>>1))*uintptr(t.bucketsize))) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 113 | if !evacuated(oldb) { |
| 114 | b = oldb |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | for { |
| 119 | for i := uintptr(0); i < bucketCnt; i++ { |
| 120 | k := *((*uint64)(add(unsafe.Pointer(b), dataOffset+i*8))) |
| 121 | if k != key { |
| 122 | continue |
| 123 | } |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 124 | x := *((*uint8)(add(unsafe.Pointer(b), i))) // b.topbits[i] without the bounds check |
| 125 | if x == empty { |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 126 | continue |
| 127 | } |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 128 | return add(unsafe.Pointer(b), dataOffset+bucketCnt*8+i*uintptr(t.valuesize)) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 129 | } |
Keith Randall | fbc56cf | 2014-12-19 20:44:18 -0800 | [diff] [blame] | 130 | b = b.overflow(t) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 131 | if b == nil { |
Michael Hudson-Doyle | 38519e6 | 2015-08-21 14:54:55 +1200 | [diff] [blame] | 132 | return atomicloadp(unsafe.Pointer(&zeroptr)) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | func mapaccess2_fast64(t *maptype, h *hmap, key uint64) (unsafe.Pointer, bool) { |
| 138 | if raceenabled && h != nil { |
Russ Cox | d21638b | 2014-08-27 21:59:49 -0400 | [diff] [blame] | 139 | callerpc := getcallerpc(unsafe.Pointer(&t)) |
Russ Cox | 0e07f1c | 2014-09-03 11:10:38 -0400 | [diff] [blame] | 140 | racereadpc(unsafe.Pointer(h), callerpc, funcPC(mapaccess2_fast64)) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 141 | } |
| 142 | if h == nil || h.count == 0 { |
Michael Hudson-Doyle | 38519e6 | 2015-08-21 14:54:55 +1200 | [diff] [blame] | 143 | return atomicloadp(unsafe.Pointer(&zeroptr)), false |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 144 | } |
| 145 | var b *bmap |
| 146 | if h.B == 0 { |
| 147 | // One-bucket table. No need to hash. |
| 148 | b = (*bmap)(h.buckets) |
| 149 | } else { |
Keith Randall | d5e4c40 | 2015-01-06 16:42:48 -0800 | [diff] [blame] | 150 | hash := t.key.alg.hash(noescape(unsafe.Pointer(&key)), uintptr(h.hash0)) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 151 | m := uintptr(1)<<h.B - 1 |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 152 | b = (*bmap)(add(h.buckets, (hash&m)*uintptr(t.bucketsize))) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 153 | if c := h.oldbuckets; c != nil { |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 154 | oldb := (*bmap)(add(c, (hash&(m>>1))*uintptr(t.bucketsize))) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 155 | if !evacuated(oldb) { |
| 156 | b = oldb |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | for { |
| 161 | for i := uintptr(0); i < bucketCnt; i++ { |
| 162 | k := *((*uint64)(add(unsafe.Pointer(b), dataOffset+i*8))) |
| 163 | if k != key { |
| 164 | continue |
| 165 | } |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 166 | x := *((*uint8)(add(unsafe.Pointer(b), i))) // b.topbits[i] without the bounds check |
| 167 | if x == empty { |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 168 | continue |
| 169 | } |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 170 | return add(unsafe.Pointer(b), dataOffset+bucketCnt*8+i*uintptr(t.valuesize)), true |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 171 | } |
Keith Randall | fbc56cf | 2014-12-19 20:44:18 -0800 | [diff] [blame] | 172 | b = b.overflow(t) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 173 | if b == nil { |
Michael Hudson-Doyle | 38519e6 | 2015-08-21 14:54:55 +1200 | [diff] [blame] | 174 | return atomicloadp(unsafe.Pointer(&zeroptr)), false |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | func mapaccess1_faststr(t *maptype, h *hmap, ky string) unsafe.Pointer { |
| 180 | if raceenabled && h != nil { |
Russ Cox | d21638b | 2014-08-27 21:59:49 -0400 | [diff] [blame] | 181 | callerpc := getcallerpc(unsafe.Pointer(&t)) |
Russ Cox | 0e07f1c | 2014-09-03 11:10:38 -0400 | [diff] [blame] | 182 | racereadpc(unsafe.Pointer(h), callerpc, funcPC(mapaccess1_faststr)) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 183 | } |
| 184 | if h == nil || h.count == 0 { |
Michael Hudson-Doyle | 38519e6 | 2015-08-21 14:54:55 +1200 | [diff] [blame] | 185 | return atomicloadp(unsafe.Pointer(&zeroptr)) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 186 | } |
| 187 | key := (*stringStruct)(unsafe.Pointer(&ky)) |
| 188 | if h.B == 0 { |
| 189 | // One-bucket table. |
| 190 | b := (*bmap)(h.buckets) |
| 191 | if key.len < 32 { |
| 192 | // short key, doing lots of comparisons is ok |
| 193 | for i := uintptr(0); i < bucketCnt; i++ { |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 194 | x := *((*uint8)(add(unsafe.Pointer(b), i))) // b.topbits[i] without the bounds check |
| 195 | if x == empty { |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 196 | continue |
| 197 | } |
| 198 | k := (*stringStruct)(add(unsafe.Pointer(b), dataOffset+i*2*ptrSize)) |
| 199 | if k.len != key.len { |
| 200 | continue |
| 201 | } |
Keith Randall | 7aa4e5a | 2014-08-07 14:52:55 -0700 | [diff] [blame] | 202 | if k.str == key.str || memeq(k.str, key.str, uintptr(key.len)) { |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 203 | return add(unsafe.Pointer(b), dataOffset+bucketCnt*2*ptrSize+i*uintptr(t.valuesize)) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
Michael Hudson-Doyle | 38519e6 | 2015-08-21 14:54:55 +1200 | [diff] [blame] | 206 | return atomicloadp(unsafe.Pointer(&zeroptr)) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 207 | } |
| 208 | // long key, try not to do more comparisons than necessary |
| 209 | keymaybe := uintptr(bucketCnt) |
| 210 | for i := uintptr(0); i < bucketCnt; i++ { |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 211 | x := *((*uint8)(add(unsafe.Pointer(b), i))) // b.topbits[i] without the bounds check |
| 212 | if x == empty { |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 213 | continue |
| 214 | } |
| 215 | k := (*stringStruct)(add(unsafe.Pointer(b), dataOffset+i*2*ptrSize)) |
| 216 | if k.len != key.len { |
| 217 | continue |
| 218 | } |
| 219 | if k.str == key.str { |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 220 | return add(unsafe.Pointer(b), dataOffset+bucketCnt*2*ptrSize+i*uintptr(t.valuesize)) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 221 | } |
| 222 | // check first 4 bytes |
| 223 | // TODO: on amd64/386 at least, make this compile to one 4-byte comparison instead of |
| 224 | // four 1-byte comparisons. |
| 225 | if *((*[4]byte)(key.str)) != *((*[4]byte)(k.str)) { |
| 226 | continue |
| 227 | } |
| 228 | // check last 4 bytes |
| 229 | if *((*[4]byte)(add(key.str, uintptr(key.len)-4))) != *((*[4]byte)(add(k.str, uintptr(key.len)-4))) { |
| 230 | continue |
| 231 | } |
| 232 | if keymaybe != bucketCnt { |
| 233 | // Two keys are potential matches. Use hash to distinguish them. |
| 234 | goto dohash |
| 235 | } |
| 236 | keymaybe = i |
| 237 | } |
| 238 | if keymaybe != bucketCnt { |
| 239 | k := (*stringStruct)(add(unsafe.Pointer(b), dataOffset+keymaybe*2*ptrSize)) |
Keith Randall | 7aa4e5a | 2014-08-07 14:52:55 -0700 | [diff] [blame] | 240 | if memeq(k.str, key.str, uintptr(key.len)) { |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 241 | return add(unsafe.Pointer(b), dataOffset+bucketCnt*2*ptrSize+keymaybe*uintptr(t.valuesize)) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
Michael Hudson-Doyle | 38519e6 | 2015-08-21 14:54:55 +1200 | [diff] [blame] | 244 | return atomicloadp(unsafe.Pointer(&zeroptr)) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 245 | } |
| 246 | dohash: |
Keith Randall | d5e4c40 | 2015-01-06 16:42:48 -0800 | [diff] [blame] | 247 | hash := t.key.alg.hash(noescape(unsafe.Pointer(&ky)), uintptr(h.hash0)) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 248 | m := uintptr(1)<<h.B - 1 |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 249 | b := (*bmap)(add(h.buckets, (hash&m)*uintptr(t.bucketsize))) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 250 | if c := h.oldbuckets; c != nil { |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 251 | oldb := (*bmap)(add(c, (hash&(m>>1))*uintptr(t.bucketsize))) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 252 | if !evacuated(oldb) { |
| 253 | b = oldb |
| 254 | } |
| 255 | } |
| 256 | top := uint8(hash >> (ptrSize*8 - 8)) |
| 257 | if top < minTopHash { |
| 258 | top += minTopHash |
| 259 | } |
| 260 | for { |
| 261 | for i := uintptr(0); i < bucketCnt; i++ { |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 262 | x := *((*uint8)(add(unsafe.Pointer(b), i))) // b.topbits[i] without the bounds check |
| 263 | if x != top { |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 264 | continue |
| 265 | } |
| 266 | k := (*stringStruct)(add(unsafe.Pointer(b), dataOffset+i*2*ptrSize)) |
| 267 | if k.len != key.len { |
| 268 | continue |
| 269 | } |
Keith Randall | 7aa4e5a | 2014-08-07 14:52:55 -0700 | [diff] [blame] | 270 | if k.str == key.str || memeq(k.str, key.str, uintptr(key.len)) { |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 271 | return add(unsafe.Pointer(b), dataOffset+bucketCnt*2*ptrSize+i*uintptr(t.valuesize)) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 272 | } |
| 273 | } |
Keith Randall | fbc56cf | 2014-12-19 20:44:18 -0800 | [diff] [blame] | 274 | b = b.overflow(t) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 275 | if b == nil { |
Michael Hudson-Doyle | 38519e6 | 2015-08-21 14:54:55 +1200 | [diff] [blame] | 276 | return atomicloadp(unsafe.Pointer(&zeroptr)) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | func mapaccess2_faststr(t *maptype, h *hmap, ky string) (unsafe.Pointer, bool) { |
| 282 | if raceenabled && h != nil { |
Russ Cox | d21638b | 2014-08-27 21:59:49 -0400 | [diff] [blame] | 283 | callerpc := getcallerpc(unsafe.Pointer(&t)) |
Russ Cox | 0e07f1c | 2014-09-03 11:10:38 -0400 | [diff] [blame] | 284 | racereadpc(unsafe.Pointer(h), callerpc, funcPC(mapaccess2_faststr)) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 285 | } |
| 286 | if h == nil || h.count == 0 { |
Michael Hudson-Doyle | 38519e6 | 2015-08-21 14:54:55 +1200 | [diff] [blame] | 287 | return atomicloadp(unsafe.Pointer(&zeroptr)), false |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 288 | } |
| 289 | key := (*stringStruct)(unsafe.Pointer(&ky)) |
| 290 | if h.B == 0 { |
| 291 | // One-bucket table. |
| 292 | b := (*bmap)(h.buckets) |
| 293 | if key.len < 32 { |
| 294 | // short key, doing lots of comparisons is ok |
| 295 | for i := uintptr(0); i < bucketCnt; i++ { |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 296 | x := *((*uint8)(add(unsafe.Pointer(b), i))) // b.topbits[i] without the bounds check |
| 297 | if x == empty { |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 298 | continue |
| 299 | } |
| 300 | k := (*stringStruct)(add(unsafe.Pointer(b), dataOffset+i*2*ptrSize)) |
| 301 | if k.len != key.len { |
| 302 | continue |
| 303 | } |
Keith Randall | 7aa4e5a | 2014-08-07 14:52:55 -0700 | [diff] [blame] | 304 | if k.str == key.str || memeq(k.str, key.str, uintptr(key.len)) { |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 305 | return add(unsafe.Pointer(b), dataOffset+bucketCnt*2*ptrSize+i*uintptr(t.valuesize)), true |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 306 | } |
| 307 | } |
Michael Hudson-Doyle | 38519e6 | 2015-08-21 14:54:55 +1200 | [diff] [blame] | 308 | return atomicloadp(unsafe.Pointer(&zeroptr)), false |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 309 | } |
| 310 | // long key, try not to do more comparisons than necessary |
| 311 | keymaybe := uintptr(bucketCnt) |
| 312 | for i := uintptr(0); i < bucketCnt; i++ { |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 313 | x := *((*uint8)(add(unsafe.Pointer(b), i))) // b.topbits[i] without the bounds check |
| 314 | if x == empty { |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 315 | continue |
| 316 | } |
| 317 | k := (*stringStruct)(add(unsafe.Pointer(b), dataOffset+i*2*ptrSize)) |
| 318 | if k.len != key.len { |
| 319 | continue |
| 320 | } |
| 321 | if k.str == key.str { |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 322 | return add(unsafe.Pointer(b), dataOffset+bucketCnt*2*ptrSize+i*uintptr(t.valuesize)), true |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 323 | } |
| 324 | // check first 4 bytes |
| 325 | if *((*[4]byte)(key.str)) != *((*[4]byte)(k.str)) { |
| 326 | continue |
| 327 | } |
| 328 | // check last 4 bytes |
| 329 | if *((*[4]byte)(add(key.str, uintptr(key.len)-4))) != *((*[4]byte)(add(k.str, uintptr(key.len)-4))) { |
| 330 | continue |
| 331 | } |
| 332 | if keymaybe != bucketCnt { |
| 333 | // Two keys are potential matches. Use hash to distinguish them. |
| 334 | goto dohash |
| 335 | } |
| 336 | keymaybe = i |
| 337 | } |
| 338 | if keymaybe != bucketCnt { |
| 339 | k := (*stringStruct)(add(unsafe.Pointer(b), dataOffset+keymaybe*2*ptrSize)) |
Keith Randall | 7aa4e5a | 2014-08-07 14:52:55 -0700 | [diff] [blame] | 340 | if memeq(k.str, key.str, uintptr(key.len)) { |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 341 | return add(unsafe.Pointer(b), dataOffset+bucketCnt*2*ptrSize+keymaybe*uintptr(t.valuesize)), true |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 342 | } |
| 343 | } |
Michael Hudson-Doyle | 38519e6 | 2015-08-21 14:54:55 +1200 | [diff] [blame] | 344 | return atomicloadp(unsafe.Pointer(&zeroptr)), false |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 345 | } |
| 346 | dohash: |
Keith Randall | d5e4c40 | 2015-01-06 16:42:48 -0800 | [diff] [blame] | 347 | hash := t.key.alg.hash(noescape(unsafe.Pointer(&ky)), uintptr(h.hash0)) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 348 | m := uintptr(1)<<h.B - 1 |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 349 | b := (*bmap)(add(h.buckets, (hash&m)*uintptr(t.bucketsize))) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 350 | if c := h.oldbuckets; c != nil { |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 351 | oldb := (*bmap)(add(c, (hash&(m>>1))*uintptr(t.bucketsize))) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 352 | if !evacuated(oldb) { |
| 353 | b = oldb |
| 354 | } |
| 355 | } |
| 356 | top := uint8(hash >> (ptrSize*8 - 8)) |
| 357 | if top < minTopHash { |
| 358 | top += minTopHash |
| 359 | } |
| 360 | for { |
| 361 | for i := uintptr(0); i < bucketCnt; i++ { |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 362 | x := *((*uint8)(add(unsafe.Pointer(b), i))) // b.topbits[i] without the bounds check |
| 363 | if x != top { |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 364 | continue |
| 365 | } |
| 366 | k := (*stringStruct)(add(unsafe.Pointer(b), dataOffset+i*2*ptrSize)) |
| 367 | if k.len != key.len { |
| 368 | continue |
| 369 | } |
Keith Randall | 7aa4e5a | 2014-08-07 14:52:55 -0700 | [diff] [blame] | 370 | if k.str == key.str || memeq(k.str, key.str, uintptr(key.len)) { |
Keith Randall | 668a55a | 2014-08-01 14:38:56 -0700 | [diff] [blame] | 371 | return add(unsafe.Pointer(b), dataOffset+bucketCnt*2*ptrSize+i*uintptr(t.valuesize)), true |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 372 | } |
| 373 | } |
Keith Randall | fbc56cf | 2014-12-19 20:44:18 -0800 | [diff] [blame] | 374 | b = b.overflow(t) |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 375 | if b == nil { |
Michael Hudson-Doyle | 38519e6 | 2015-08-21 14:54:55 +1200 | [diff] [blame] | 376 | return atomicloadp(unsafe.Pointer(&zeroptr)), false |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | } |