2018-01-27 12:38:13 +01:00
|
|
|
// Copyright 2018 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package runtime
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"runtime/internal/sys"
|
|
|
|
|
"unsafe"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func mapaccess1_faststr(t *maptype, h *hmap, ky string) unsafe.Pointer {
|
|
|
|
|
if raceenabled && h != nil {
|
|
|
|
|
callerpc := getcallerpc()
|
|
|
|
|
racereadpc(unsafe.Pointer(h), callerpc, funcPC(mapaccess1_faststr))
|
|
|
|
|
}
|
|
|
|
|
if h == nil || h.count == 0 {
|
|
|
|
|
return unsafe.Pointer(&zeroVal[0])
|
|
|
|
|
}
|
|
|
|
|
if h.flags&hashWriting != 0 {
|
|
|
|
|
throw("concurrent map read and map write")
|
|
|
|
|
}
|
|
|
|
|
key := stringStructOf(&ky)
|
|
|
|
|
if h.B == 0 {
|
|
|
|
|
// One-bucket table.
|
|
|
|
|
b := (*bmap)(h.buckets)
|
|
|
|
|
if key.len < 32 {
|
|
|
|
|
// short key, doing lots of comparisons is ok
|
|
|
|
|
for i, kptr := uintptr(0), b.keys(); i < bucketCnt; i, kptr = i+1, add(kptr, 2*sys.PtrSize) {
|
|
|
|
|
k := (*stringStruct)(kptr)
|
2018-10-15 15:14:48 -07:00
|
|
|
if k.len != key.len || isEmpty(b.tophash[i]) {
|
|
|
|
|
if b.tophash[i] == emptyRest {
|
|
|
|
|
break
|
|
|
|
|
}
|
2018-01-27 12:38:13 +01:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if k.str == key.str || memequal(k.str, key.str, uintptr(key.len)) {
|
2019-04-22 13:37:08 -07:00
|
|
|
return add(unsafe.Pointer(b), dataOffset+bucketCnt*2*sys.PtrSize+i*uintptr(t.elemsize))
|
2018-01-27 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return unsafe.Pointer(&zeroVal[0])
|
|
|
|
|
}
|
|
|
|
|
// long key, try not to do more comparisons than necessary
|
|
|
|
|
keymaybe := uintptr(bucketCnt)
|
|
|
|
|
for i, kptr := uintptr(0), b.keys(); i < bucketCnt; i, kptr = i+1, add(kptr, 2*sys.PtrSize) {
|
|
|
|
|
k := (*stringStruct)(kptr)
|
2018-10-15 15:14:48 -07:00
|
|
|
if k.len != key.len || isEmpty(b.tophash[i]) {
|
|
|
|
|
if b.tophash[i] == emptyRest {
|
|
|
|
|
break
|
|
|
|
|
}
|
2018-01-27 12:38:13 +01:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if k.str == key.str {
|
2019-04-22 13:37:08 -07:00
|
|
|
return add(unsafe.Pointer(b), dataOffset+bucketCnt*2*sys.PtrSize+i*uintptr(t.elemsize))
|
2018-01-27 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
// check first 4 bytes
|
|
|
|
|
if *((*[4]byte)(key.str)) != *((*[4]byte)(k.str)) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
// check last 4 bytes
|
|
|
|
|
if *((*[4]byte)(add(key.str, uintptr(key.len)-4))) != *((*[4]byte)(add(k.str, uintptr(key.len)-4))) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if keymaybe != bucketCnt {
|
|
|
|
|
// Two keys are potential matches. Use hash to distinguish them.
|
|
|
|
|
goto dohash
|
|
|
|
|
}
|
|
|
|
|
keymaybe = i
|
|
|
|
|
}
|
|
|
|
|
if keymaybe != bucketCnt {
|
|
|
|
|
k := (*stringStruct)(add(unsafe.Pointer(b), dataOffset+keymaybe*2*sys.PtrSize))
|
|
|
|
|
if memequal(k.str, key.str, uintptr(key.len)) {
|
2019-04-22 13:37:08 -07:00
|
|
|
return add(unsafe.Pointer(b), dataOffset+bucketCnt*2*sys.PtrSize+keymaybe*uintptr(t.elemsize))
|
2018-01-27 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return unsafe.Pointer(&zeroVal[0])
|
|
|
|
|
}
|
|
|
|
|
dohash:
|
cmd/compile,runtime: generate hash functions only for types which are map keys
Right now we generate hash functions for all types, just in case they
are used as map keys. That's a lot of wasted effort and binary size
for types which will never be used as a map key. Instead, generate
hash functions only for types that we know are map keys.
Just doing that is a bit too simple, since maps with an interface type
as a key might have to hash any concrete key type that implements that
interface. So for that case, implement hashing of such types at
runtime (instead of with generated code). It will be slower, but only
for maps with interface types as keys, and maybe only a bit slower as
the aeshash time probably dominates the dispatch time.
Reorg where we keep the equals and hash functions. Move the hash function
from the key type to the map type, saving a field in every non-map type.
That leaves only one function in the alg structure, so get rid of that and
just keep the equal function in the type descriptor itself.
cmd/go now has 10 generated hash functions, instead of 504. Makes
cmd/go 1.0% smaller. Update #6853.
Speed on non-interface keys is unchanged. Speed on interface keys
is ~20% slower:
name old time/op new time/op delta
MapInterfaceString-8 23.0ns ±21% 27.6ns ±14% +20.01% (p=0.002 n=10+10)
MapInterfacePtr-8 19.4ns ±16% 23.7ns ± 7% +22.48% (p=0.000 n=10+8)
Change-Id: I7c2e42292a46b5d4e288aaec4029bdbb01089263
Reviewed-on: https://go-review.googlesource.com/c/go/+/191198
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Martin Möhrmann <moehrmann@google.com>
2019-08-06 15:22:51 -07:00
|
|
|
hash := t.hasher(noescape(unsafe.Pointer(&ky)), uintptr(h.hash0))
|
2018-01-27 12:38:13 +01:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
top := tophash(hash)
|
|
|
|
|
for ; b != nil; b = b.overflow(t) {
|
|
|
|
|
for i, kptr := uintptr(0), b.keys(); i < bucketCnt; i, kptr = i+1, add(kptr, 2*sys.PtrSize) {
|
|
|
|
|
k := (*stringStruct)(kptr)
|
|
|
|
|
if k.len != key.len || b.tophash[i] != top {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if k.str == key.str || memequal(k.str, key.str, uintptr(key.len)) {
|
2019-04-22 13:37:08 -07:00
|
|
|
return add(unsafe.Pointer(b), dataOffset+bucketCnt*2*sys.PtrSize+i*uintptr(t.elemsize))
|
2018-01-27 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return unsafe.Pointer(&zeroVal[0])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mapaccess2_faststr(t *maptype, h *hmap, ky string) (unsafe.Pointer, bool) {
|
|
|
|
|
if raceenabled && h != nil {
|
|
|
|
|
callerpc := getcallerpc()
|
|
|
|
|
racereadpc(unsafe.Pointer(h), callerpc, funcPC(mapaccess2_faststr))
|
|
|
|
|
}
|
|
|
|
|
if h == nil || h.count == 0 {
|
|
|
|
|
return unsafe.Pointer(&zeroVal[0]), false
|
|
|
|
|
}
|
|
|
|
|
if h.flags&hashWriting != 0 {
|
|
|
|
|
throw("concurrent map read and map write")
|
|
|
|
|
}
|
|
|
|
|
key := stringStructOf(&ky)
|
|
|
|
|
if h.B == 0 {
|
|
|
|
|
// One-bucket table.
|
|
|
|
|
b := (*bmap)(h.buckets)
|
|
|
|
|
if key.len < 32 {
|
|
|
|
|
// short key, doing lots of comparisons is ok
|
|
|
|
|
for i, kptr := uintptr(0), b.keys(); i < bucketCnt; i, kptr = i+1, add(kptr, 2*sys.PtrSize) {
|
|
|
|
|
k := (*stringStruct)(kptr)
|
2018-10-15 15:14:48 -07:00
|
|
|
if k.len != key.len || isEmpty(b.tophash[i]) {
|
|
|
|
|
if b.tophash[i] == emptyRest {
|
|
|
|
|
break
|
|
|
|
|
}
|
2018-01-27 12:38:13 +01:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if k.str == key.str || memequal(k.str, key.str, uintptr(key.len)) {
|
2019-04-22 13:37:08 -07:00
|
|
|
return add(unsafe.Pointer(b), dataOffset+bucketCnt*2*sys.PtrSize+i*uintptr(t.elemsize)), true
|
2018-01-27 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return unsafe.Pointer(&zeroVal[0]), false
|
|
|
|
|
}
|
|
|
|
|
// long key, try not to do more comparisons than necessary
|
|
|
|
|
keymaybe := uintptr(bucketCnt)
|
|
|
|
|
for i, kptr := uintptr(0), b.keys(); i < bucketCnt; i, kptr = i+1, add(kptr, 2*sys.PtrSize) {
|
|
|
|
|
k := (*stringStruct)(kptr)
|
2018-10-15 15:14:48 -07:00
|
|
|
if k.len != key.len || isEmpty(b.tophash[i]) {
|
|
|
|
|
if b.tophash[i] == emptyRest {
|
|
|
|
|
break
|
|
|
|
|
}
|
2018-01-27 12:38:13 +01:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if k.str == key.str {
|
2019-04-22 13:37:08 -07:00
|
|
|
return add(unsafe.Pointer(b), dataOffset+bucketCnt*2*sys.PtrSize+i*uintptr(t.elemsize)), true
|
2018-01-27 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
// check first 4 bytes
|
|
|
|
|
if *((*[4]byte)(key.str)) != *((*[4]byte)(k.str)) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
// check last 4 bytes
|
|
|
|
|
if *((*[4]byte)(add(key.str, uintptr(key.len)-4))) != *((*[4]byte)(add(k.str, uintptr(key.len)-4))) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if keymaybe != bucketCnt {
|
|
|
|
|
// Two keys are potential matches. Use hash to distinguish them.
|
|
|
|
|
goto dohash
|
|
|
|
|
}
|
|
|
|
|
keymaybe = i
|
|
|
|
|
}
|
|
|
|
|
if keymaybe != bucketCnt {
|
|
|
|
|
k := (*stringStruct)(add(unsafe.Pointer(b), dataOffset+keymaybe*2*sys.PtrSize))
|
|
|
|
|
if memequal(k.str, key.str, uintptr(key.len)) {
|
2019-04-22 13:37:08 -07:00
|
|
|
return add(unsafe.Pointer(b), dataOffset+bucketCnt*2*sys.PtrSize+keymaybe*uintptr(t.elemsize)), true
|
2018-01-27 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return unsafe.Pointer(&zeroVal[0]), false
|
|
|
|
|
}
|
|
|
|
|
dohash:
|
cmd/compile,runtime: generate hash functions only for types which are map keys
Right now we generate hash functions for all types, just in case they
are used as map keys. That's a lot of wasted effort and binary size
for types which will never be used as a map key. Instead, generate
hash functions only for types that we know are map keys.
Just doing that is a bit too simple, since maps with an interface type
as a key might have to hash any concrete key type that implements that
interface. So for that case, implement hashing of such types at
runtime (instead of with generated code). It will be slower, but only
for maps with interface types as keys, and maybe only a bit slower as
the aeshash time probably dominates the dispatch time.
Reorg where we keep the equals and hash functions. Move the hash function
from the key type to the map type, saving a field in every non-map type.
That leaves only one function in the alg structure, so get rid of that and
just keep the equal function in the type descriptor itself.
cmd/go now has 10 generated hash functions, instead of 504. Makes
cmd/go 1.0% smaller. Update #6853.
Speed on non-interface keys is unchanged. Speed on interface keys
is ~20% slower:
name old time/op new time/op delta
MapInterfaceString-8 23.0ns ±21% 27.6ns ±14% +20.01% (p=0.002 n=10+10)
MapInterfacePtr-8 19.4ns ±16% 23.7ns ± 7% +22.48% (p=0.000 n=10+8)
Change-Id: I7c2e42292a46b5d4e288aaec4029bdbb01089263
Reviewed-on: https://go-review.googlesource.com/c/go/+/191198
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Martin Möhrmann <moehrmann@google.com>
2019-08-06 15:22:51 -07:00
|
|
|
hash := t.hasher(noescape(unsafe.Pointer(&ky)), uintptr(h.hash0))
|
2018-01-27 12:38:13 +01:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
top := tophash(hash)
|
|
|
|
|
for ; b != nil; b = b.overflow(t) {
|
|
|
|
|
for i, kptr := uintptr(0), b.keys(); i < bucketCnt; i, kptr = i+1, add(kptr, 2*sys.PtrSize) {
|
|
|
|
|
k := (*stringStruct)(kptr)
|
|
|
|
|
if k.len != key.len || b.tophash[i] != top {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if k.str == key.str || memequal(k.str, key.str, uintptr(key.len)) {
|
2019-04-22 13:37:08 -07:00
|
|
|
return add(unsafe.Pointer(b), dataOffset+bucketCnt*2*sys.PtrSize+i*uintptr(t.elemsize)), true
|
2018-01-27 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return unsafe.Pointer(&zeroVal[0]), false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mapassign_faststr(t *maptype, h *hmap, s string) unsafe.Pointer {
|
|
|
|
|
if h == nil {
|
|
|
|
|
panic(plainError("assignment to entry in nil map"))
|
|
|
|
|
}
|
|
|
|
|
if raceenabled {
|
|
|
|
|
callerpc := getcallerpc()
|
|
|
|
|
racewritepc(unsafe.Pointer(h), callerpc, funcPC(mapassign_faststr))
|
|
|
|
|
}
|
|
|
|
|
if h.flags&hashWriting != 0 {
|
|
|
|
|
throw("concurrent map writes")
|
|
|
|
|
}
|
|
|
|
|
key := stringStructOf(&s)
|
cmd/compile,runtime: generate hash functions only for types which are map keys
Right now we generate hash functions for all types, just in case they
are used as map keys. That's a lot of wasted effort and binary size
for types which will never be used as a map key. Instead, generate
hash functions only for types that we know are map keys.
Just doing that is a bit too simple, since maps with an interface type
as a key might have to hash any concrete key type that implements that
interface. So for that case, implement hashing of such types at
runtime (instead of with generated code). It will be slower, but only
for maps with interface types as keys, and maybe only a bit slower as
the aeshash time probably dominates the dispatch time.
Reorg where we keep the equals and hash functions. Move the hash function
from the key type to the map type, saving a field in every non-map type.
That leaves only one function in the alg structure, so get rid of that and
just keep the equal function in the type descriptor itself.
cmd/go now has 10 generated hash functions, instead of 504. Makes
cmd/go 1.0% smaller. Update #6853.
Speed on non-interface keys is unchanged. Speed on interface keys
is ~20% slower:
name old time/op new time/op delta
MapInterfaceString-8 23.0ns ±21% 27.6ns ±14% +20.01% (p=0.002 n=10+10)
MapInterfacePtr-8 19.4ns ±16% 23.7ns ± 7% +22.48% (p=0.000 n=10+8)
Change-Id: I7c2e42292a46b5d4e288aaec4029bdbb01089263
Reviewed-on: https://go-review.googlesource.com/c/go/+/191198
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Martin Möhrmann <moehrmann@google.com>
2019-08-06 15:22:51 -07:00
|
|
|
hash := t.hasher(noescape(unsafe.Pointer(&s)), uintptr(h.hash0))
|
2018-01-27 12:38:13 +01:00
|
|
|
|
cmd/compile,runtime: generate hash functions only for types which are map keys
Right now we generate hash functions for all types, just in case they
are used as map keys. That's a lot of wasted effort and binary size
for types which will never be used as a map key. Instead, generate
hash functions only for types that we know are map keys.
Just doing that is a bit too simple, since maps with an interface type
as a key might have to hash any concrete key type that implements that
interface. So for that case, implement hashing of such types at
runtime (instead of with generated code). It will be slower, but only
for maps with interface types as keys, and maybe only a bit slower as
the aeshash time probably dominates the dispatch time.
Reorg where we keep the equals and hash functions. Move the hash function
from the key type to the map type, saving a field in every non-map type.
That leaves only one function in the alg structure, so get rid of that and
just keep the equal function in the type descriptor itself.
cmd/go now has 10 generated hash functions, instead of 504. Makes
cmd/go 1.0% smaller. Update #6853.
Speed on non-interface keys is unchanged. Speed on interface keys
is ~20% slower:
name old time/op new time/op delta
MapInterfaceString-8 23.0ns ±21% 27.6ns ±14% +20.01% (p=0.002 n=10+10)
MapInterfacePtr-8 19.4ns ±16% 23.7ns ± 7% +22.48% (p=0.000 n=10+8)
Change-Id: I7c2e42292a46b5d4e288aaec4029bdbb01089263
Reviewed-on: https://go-review.googlesource.com/c/go/+/191198
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Martin Möhrmann <moehrmann@google.com>
2019-08-06 15:22:51 -07:00
|
|
|
// Set hashWriting after calling t.hasher for consistency with mapassign.
|
2018-07-31 11:24:37 -07:00
|
|
|
h.flags ^= hashWriting
|
2018-01-27 12:38:13 +01:00
|
|
|
|
|
|
|
|
if h.buckets == nil {
|
|
|
|
|
h.buckets = newobject(t.bucket) // newarray(t.bucket, 1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
again:
|
|
|
|
|
bucket := hash & bucketMask(h.B)
|
|
|
|
|
if h.growing() {
|
|
|
|
|
growWork_faststr(t, h, bucket)
|
|
|
|
|
}
|
2020-09-28 17:38:13 +08:00
|
|
|
b := (*bmap)(add(h.buckets, bucket*uintptr(t.bucketsize)))
|
2018-01-27 12:38:13 +01:00
|
|
|
top := tophash(hash)
|
|
|
|
|
|
|
|
|
|
var insertb *bmap
|
|
|
|
|
var inserti uintptr
|
|
|
|
|
var insertk unsafe.Pointer
|
|
|
|
|
|
2018-10-15 15:14:48 -07:00
|
|
|
bucketloop:
|
2018-01-27 12:38:13 +01:00
|
|
|
for {
|
|
|
|
|
for i := uintptr(0); i < bucketCnt; i++ {
|
|
|
|
|
if b.tophash[i] != top {
|
2018-10-15 15:14:48 -07:00
|
|
|
if isEmpty(b.tophash[i]) && insertb == nil {
|
2018-01-27 12:38:13 +01:00
|
|
|
insertb = b
|
|
|
|
|
inserti = i
|
|
|
|
|
}
|
2018-10-15 15:14:48 -07:00
|
|
|
if b.tophash[i] == emptyRest {
|
|
|
|
|
break bucketloop
|
|
|
|
|
}
|
2018-01-27 12:38:13 +01:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
k := (*stringStruct)(add(unsafe.Pointer(b), dataOffset+i*2*sys.PtrSize))
|
|
|
|
|
if k.len != key.len {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if k.str != key.str && !memequal(k.str, key.str, uintptr(key.len)) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
// already have a mapping for key. Update it.
|
|
|
|
|
inserti = i
|
|
|
|
|
insertb = b
|
2021-03-26 23:29:25 +07:00
|
|
|
// Overwrite existing key, so it can be garbage collected.
|
|
|
|
|
// The size is already guaranteed to be set correctly.
|
|
|
|
|
k.str = key.str
|
2018-01-27 12:38:13 +01:00
|
|
|
goto done
|
|
|
|
|
}
|
|
|
|
|
ovf := b.overflow(t)
|
|
|
|
|
if ovf == nil {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
b = ovf
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 insertb == nil {
|
2020-09-28 17:38:13 +08:00
|
|
|
// The current bucket and all the overflow buckets connected to it are full, allocate a new one.
|
2018-01-27 12:38:13 +01:00
|
|
|
insertb = h.newoverflow(t, b)
|
|
|
|
|
inserti = 0 // not necessary, but avoids needlessly spilling inserti
|
|
|
|
|
}
|
|
|
|
|
insertb.tophash[inserti&(bucketCnt-1)] = top // mask inserti to avoid bounds checks
|
|
|
|
|
|
|
|
|
|
insertk = add(unsafe.Pointer(insertb), dataOffset+inserti*2*sys.PtrSize)
|
|
|
|
|
// store new key at insert position
|
|
|
|
|
*((*stringStruct)(insertk)) = *key
|
|
|
|
|
h.count++
|
|
|
|
|
|
|
|
|
|
done:
|
2019-04-22 13:37:08 -07:00
|
|
|
elem := add(unsafe.Pointer(insertb), dataOffset+bucketCnt*2*sys.PtrSize+inserti*uintptr(t.elemsize))
|
2018-01-27 12:38:13 +01:00
|
|
|
if h.flags&hashWriting == 0 {
|
|
|
|
|
throw("concurrent map writes")
|
|
|
|
|
}
|
|
|
|
|
h.flags &^= hashWriting
|
2019-04-22 13:37:08 -07:00
|
|
|
return elem
|
2018-01-27 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mapdelete_faststr(t *maptype, h *hmap, ky string) {
|
|
|
|
|
if raceenabled && h != nil {
|
|
|
|
|
callerpc := getcallerpc()
|
|
|
|
|
racewritepc(unsafe.Pointer(h), callerpc, funcPC(mapdelete_faststr))
|
|
|
|
|
}
|
|
|
|
|
if h == nil || h.count == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if h.flags&hashWriting != 0 {
|
|
|
|
|
throw("concurrent map writes")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key := stringStructOf(&ky)
|
cmd/compile,runtime: generate hash functions only for types which are map keys
Right now we generate hash functions for all types, just in case they
are used as map keys. That's a lot of wasted effort and binary size
for types which will never be used as a map key. Instead, generate
hash functions only for types that we know are map keys.
Just doing that is a bit too simple, since maps with an interface type
as a key might have to hash any concrete key type that implements that
interface. So for that case, implement hashing of such types at
runtime (instead of with generated code). It will be slower, but only
for maps with interface types as keys, and maybe only a bit slower as
the aeshash time probably dominates the dispatch time.
Reorg where we keep the equals and hash functions. Move the hash function
from the key type to the map type, saving a field in every non-map type.
That leaves only one function in the alg structure, so get rid of that and
just keep the equal function in the type descriptor itself.
cmd/go now has 10 generated hash functions, instead of 504. Makes
cmd/go 1.0% smaller. Update #6853.
Speed on non-interface keys is unchanged. Speed on interface keys
is ~20% slower:
name old time/op new time/op delta
MapInterfaceString-8 23.0ns ±21% 27.6ns ±14% +20.01% (p=0.002 n=10+10)
MapInterfacePtr-8 19.4ns ±16% 23.7ns ± 7% +22.48% (p=0.000 n=10+8)
Change-Id: I7c2e42292a46b5d4e288aaec4029bdbb01089263
Reviewed-on: https://go-review.googlesource.com/c/go/+/191198
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Martin Möhrmann <moehrmann@google.com>
2019-08-06 15:22:51 -07:00
|
|
|
hash := t.hasher(noescape(unsafe.Pointer(&ky)), uintptr(h.hash0))
|
2018-01-27 12:38:13 +01:00
|
|
|
|
cmd/compile,runtime: generate hash functions only for types which are map keys
Right now we generate hash functions for all types, just in case they
are used as map keys. That's a lot of wasted effort and binary size
for types which will never be used as a map key. Instead, generate
hash functions only for types that we know are map keys.
Just doing that is a bit too simple, since maps with an interface type
as a key might have to hash any concrete key type that implements that
interface. So for that case, implement hashing of such types at
runtime (instead of with generated code). It will be slower, but only
for maps with interface types as keys, and maybe only a bit slower as
the aeshash time probably dominates the dispatch time.
Reorg where we keep the equals and hash functions. Move the hash function
from the key type to the map type, saving a field in every non-map type.
That leaves only one function in the alg structure, so get rid of that and
just keep the equal function in the type descriptor itself.
cmd/go now has 10 generated hash functions, instead of 504. Makes
cmd/go 1.0% smaller. Update #6853.
Speed on non-interface keys is unchanged. Speed on interface keys
is ~20% slower:
name old time/op new time/op delta
MapInterfaceString-8 23.0ns ±21% 27.6ns ±14% +20.01% (p=0.002 n=10+10)
MapInterfacePtr-8 19.4ns ±16% 23.7ns ± 7% +22.48% (p=0.000 n=10+8)
Change-Id: I7c2e42292a46b5d4e288aaec4029bdbb01089263
Reviewed-on: https://go-review.googlesource.com/c/go/+/191198
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Martin Möhrmann <moehrmann@google.com>
2019-08-06 15:22:51 -07:00
|
|
|
// Set hashWriting after calling t.hasher for consistency with mapdelete
|
2018-07-31 11:24:37 -07:00
|
|
|
h.flags ^= hashWriting
|
2018-01-27 12:38:13 +01:00
|
|
|
|
|
|
|
|
bucket := hash & bucketMask(h.B)
|
|
|
|
|
if h.growing() {
|
|
|
|
|
growWork_faststr(t, h, bucket)
|
|
|
|
|
}
|
|
|
|
|
b := (*bmap)(add(h.buckets, bucket*uintptr(t.bucketsize)))
|
2018-10-15 17:24:21 -07:00
|
|
|
bOrig := b
|
2018-01-27 12:38:13 +01:00
|
|
|
top := tophash(hash)
|
|
|
|
|
search:
|
|
|
|
|
for ; b != nil; b = b.overflow(t) {
|
|
|
|
|
for i, kptr := uintptr(0), b.keys(); i < bucketCnt; i, kptr = i+1, add(kptr, 2*sys.PtrSize) {
|
|
|
|
|
k := (*stringStruct)(kptr)
|
|
|
|
|
if k.len != key.len || b.tophash[i] != top {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if k.str != key.str && !memequal(k.str, key.str, uintptr(key.len)) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
// Clear key's pointer.
|
|
|
|
|
k.str = nil
|
2019-04-22 13:37:08 -07:00
|
|
|
e := add(unsafe.Pointer(b), dataOffset+bucketCnt*2*sys.PtrSize+i*uintptr(t.elemsize))
|
2019-03-25 12:34:27 -07:00
|
|
|
if t.elem.ptrdata != 0 {
|
2019-04-22 13:37:08 -07:00
|
|
|
memclrHasPointers(e, t.elem.size)
|
2018-06-20 22:19:56 -07:00
|
|
|
} else {
|
2019-04-22 13:37:08 -07:00
|
|
|
memclrNoHeapPointers(e, t.elem.size)
|
2018-01-27 12:38:13 +01:00
|
|
|
}
|
2018-10-15 15:14:48 -07:00
|
|
|
b.tophash[i] = emptyOne
|
2018-10-15 17:24:21 -07:00
|
|
|
// If the bucket now ends in a bunch of emptyOne states,
|
|
|
|
|
// change those to emptyRest states.
|
|
|
|
|
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:
|
2018-01-27 12:38:13 +01:00
|
|
|
h.count--
|
2020-09-04 17:47:44 +07:00
|
|
|
// Reset the hash seed to make it more difficult for attackers to
|
|
|
|
|
// repeatedly trigger hash collisions. See issue 25237.
|
|
|
|
|
if h.count == 0 {
|
|
|
|
|
h.hash0 = fastrand()
|
|
|
|
|
}
|
2018-01-27 12:38:13 +01:00
|
|
|
break search
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if h.flags&hashWriting == 0 {
|
|
|
|
|
throw("concurrent map writes")
|
|
|
|
|
}
|
|
|
|
|
h.flags &^= hashWriting
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func growWork_faststr(t *maptype, h *hmap, bucket uintptr) {
|
|
|
|
|
// make sure we evacuate the oldbucket corresponding
|
|
|
|
|
// to the bucket we're about to use
|
|
|
|
|
evacuate_faststr(t, h, bucket&h.oldbucketmask())
|
|
|
|
|
|
|
|
|
|
// evacuate one more oldbucket to make progress on growing
|
|
|
|
|
if h.growing() {
|
|
|
|
|
evacuate_faststr(t, h, h.nevacuate)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func evacuate_faststr(t *maptype, h *hmap, oldbucket uintptr) {
|
|
|
|
|
b := (*bmap)(add(h.oldbuckets, oldbucket*uintptr(t.bucketsize)))
|
|
|
|
|
newbit := h.noldbuckets()
|
|
|
|
|
if !evacuated(b) {
|
|
|
|
|
// 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)
|
2019-04-22 13:37:08 -07:00
|
|
|
x.e = add(x.k, bucketCnt*2*sys.PtrSize)
|
2018-01-27 12:38:13 +01:00
|
|
|
|
|
|
|
|
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)
|
2019-04-22 13:37:08 -07:00
|
|
|
y.e = add(y.k, bucketCnt*2*sys.PtrSize)
|
2018-01-27 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for ; b != nil; b = b.overflow(t) {
|
|
|
|
|
k := add(unsafe.Pointer(b), dataOffset)
|
2019-04-22 13:37:08 -07:00
|
|
|
e := add(k, bucketCnt*2*sys.PtrSize)
|
|
|
|
|
for i := 0; i < bucketCnt; i, k, e = i+1, add(k, 2*sys.PtrSize), add(e, uintptr(t.elemsize)) {
|
2018-01-27 12:38:13 +01:00
|
|
|
top := b.tophash[i]
|
2018-10-15 15:14:48 -07:00
|
|
|
if isEmpty(top) {
|
2018-01-27 12:38:13 +01:00
|
|
|
b.tophash[i] = evacuatedEmpty
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if top < minTopHash {
|
|
|
|
|
throw("bad map state")
|
|
|
|
|
}
|
|
|
|
|
var useY uint8
|
|
|
|
|
if !h.sameSizeGrow() {
|
|
|
|
|
// Compute hash to make our evacuation decision (whether we need
|
2019-04-22 13:37:08 -07:00
|
|
|
// to send this key/elem to bucket x or bucket y).
|
cmd/compile,runtime: generate hash functions only for types which are map keys
Right now we generate hash functions for all types, just in case they
are used as map keys. That's a lot of wasted effort and binary size
for types which will never be used as a map key. Instead, generate
hash functions only for types that we know are map keys.
Just doing that is a bit too simple, since maps with an interface type
as a key might have to hash any concrete key type that implements that
interface. So for that case, implement hashing of such types at
runtime (instead of with generated code). It will be slower, but only
for maps with interface types as keys, and maybe only a bit slower as
the aeshash time probably dominates the dispatch time.
Reorg where we keep the equals and hash functions. Move the hash function
from the key type to the map type, saving a field in every non-map type.
That leaves only one function in the alg structure, so get rid of that and
just keep the equal function in the type descriptor itself.
cmd/go now has 10 generated hash functions, instead of 504. Makes
cmd/go 1.0% smaller. Update #6853.
Speed on non-interface keys is unchanged. Speed on interface keys
is ~20% slower:
name old time/op new time/op delta
MapInterfaceString-8 23.0ns ±21% 27.6ns ±14% +20.01% (p=0.002 n=10+10)
MapInterfacePtr-8 19.4ns ±16% 23.7ns ± 7% +22.48% (p=0.000 n=10+8)
Change-Id: I7c2e42292a46b5d4e288aaec4029bdbb01089263
Reviewed-on: https://go-review.googlesource.com/c/go/+/191198
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Martin Möhrmann <moehrmann@google.com>
2019-08-06 15:22:51 -07:00
|
|
|
hash := t.hasher(k, uintptr(h.hash0))
|
2018-01-27 12:38:13 +01:00
|
|
|
if hash&newbit != 0 {
|
|
|
|
|
useY = 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b.tophash[i] = evacuatedX + useY // evacuatedX + 1 == evacuatedY, enforced in makemap
|
|
|
|
|
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)
|
2019-04-22 13:37:08 -07:00
|
|
|
dst.e = add(dst.k, bucketCnt*2*sys.PtrSize)
|
2018-01-27 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
dst.b.tophash[dst.i&(bucketCnt-1)] = top // mask dst.i as an optimization, to avoid a bounds check
|
|
|
|
|
|
|
|
|
|
// Copy key.
|
|
|
|
|
*(*string)(dst.k) = *(*string)(k)
|
|
|
|
|
|
2019-04-22 13:37:08 -07:00
|
|
|
typedmemmove(t.elem, dst.e, e)
|
2018-01-27 12:38:13 +01:00
|
|
|
dst.i++
|
|
|
|
|
// These updates might push these pointers past the end of the
|
2019-04-22 13:37:08 -07:00
|
|
|
// key or elem arrays. That's ok, as we have the overflow pointer
|
2018-01-27 12:38:13 +01:00
|
|
|
// at the end of the bucket to protect against pointing past the
|
|
|
|
|
// end of the bucket.
|
|
|
|
|
dst.k = add(dst.k, 2*sys.PtrSize)
|
2019-04-22 13:37:08 -07:00
|
|
|
dst.e = add(dst.e, uintptr(t.elemsize))
|
2018-01-27 12:38:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-22 13:37:08 -07:00
|
|
|
// Unlink the overflow buckets & clear key/elem to help GC.
|
2019-03-25 12:34:27 -07:00
|
|
|
if h.flags&oldIterator == 0 && t.bucket.ptrdata != 0 {
|
2018-01-27 12:38:13 +01:00
|
|
|
b := add(h.oldbuckets, oldbucket*uintptr(t.bucketsize))
|
|
|
|
|
// Preserve b.tophash because the evacuation
|
|
|
|
|
// state is maintained there.
|
|
|
|
|
ptr := add(b, dataOffset)
|
|
|
|
|
n := uintptr(t.bucketsize) - dataOffset
|
|
|
|
|
memclrHasPointers(ptr, n)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if oldbucket == h.nevacuate {
|
|
|
|
|
advanceEvacuationMark(h, t, newbit)
|
|
|
|
|
}
|
|
|
|
|
}
|