mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00: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>
This commit is contained in:
parent
671bcb5966
commit
36f30ba289
19 changed files with 541 additions and 349 deletions
|
|
@ -825,29 +825,21 @@ func typeptrdata(t *types.Type) int64 {
|
|||
// reflect/type.go
|
||||
// runtime/type.go
|
||||
const (
|
||||
tflagUncommon = 1 << 0
|
||||
tflagExtraStar = 1 << 1
|
||||
tflagNamed = 1 << 2
|
||||
tflagUncommon = 1 << 0
|
||||
tflagExtraStar = 1 << 1
|
||||
tflagNamed = 1 << 2
|
||||
tflagRegularMemory = 1 << 3
|
||||
)
|
||||
|
||||
var (
|
||||
algarray *obj.LSym
|
||||
memhashvarlen *obj.LSym
|
||||
memequalvarlen *obj.LSym
|
||||
)
|
||||
|
||||
// dcommontype dumps the contents of a reflect.rtype (runtime._type).
|
||||
func dcommontype(lsym *obj.LSym, t *types.Type) int {
|
||||
sizeofAlg := 2 * Widthptr
|
||||
if algarray == nil {
|
||||
algarray = sysvar("algarray")
|
||||
}
|
||||
dowidth(t)
|
||||
alg := algtype(t)
|
||||
var algsym *obj.LSym
|
||||
if alg == ASPECIAL || alg == AMEM {
|
||||
algsym = dalgsym(t)
|
||||
}
|
||||
eqfunc := geneq(t)
|
||||
|
||||
sptrWeak := true
|
||||
var sptr *obj.LSym
|
||||
|
|
@ -871,7 +863,7 @@ func dcommontype(lsym *obj.LSym, t *types.Type) int {
|
|||
// align uint8
|
||||
// fieldAlign uint8
|
||||
// kind uint8
|
||||
// alg *typeAlg
|
||||
// equal func(unsafe.Pointer, unsafe.Pointer) bool
|
||||
// gcdata *byte
|
||||
// str nameOff
|
||||
// ptrToThis typeOff
|
||||
|
|
@ -888,6 +880,9 @@ func dcommontype(lsym *obj.LSym, t *types.Type) int {
|
|||
if t.Sym != nil && t.Sym.Name != "" {
|
||||
tflag |= tflagNamed
|
||||
}
|
||||
if IsRegularMemory(t) {
|
||||
tflag |= tflagRegularMemory
|
||||
}
|
||||
|
||||
exported := false
|
||||
p := t.LongString()
|
||||
|
|
@ -930,10 +925,10 @@ func dcommontype(lsym *obj.LSym, t *types.Type) int {
|
|||
i |= objabi.KindGCProg
|
||||
}
|
||||
ot = duint8(lsym, ot, uint8(i)) // kind
|
||||
if algsym == nil {
|
||||
ot = dsymptr(lsym, ot, algarray, int(alg)*sizeofAlg)
|
||||
if eqfunc != nil {
|
||||
ot = dsymptr(lsym, ot, eqfunc, 0) // equality function
|
||||
} else {
|
||||
ot = dsymptr(lsym, ot, algsym, 0)
|
||||
ot = duintptr(lsym, ot, 0) // type we can't do == with
|
||||
}
|
||||
ot = dsymptr(lsym, ot, gcsym, 0) // gcdata
|
||||
|
||||
|
|
@ -1311,10 +1306,13 @@ func dtypesym(t *types.Type) *obj.LSym {
|
|||
s1 := dtypesym(t.Key())
|
||||
s2 := dtypesym(t.Elem())
|
||||
s3 := dtypesym(bmap(t))
|
||||
hasher := genhash(t.Key())
|
||||
|
||||
ot = dcommontype(lsym, t)
|
||||
ot = dsymptr(lsym, ot, s1, 0)
|
||||
ot = dsymptr(lsym, ot, s2, 0)
|
||||
ot = dsymptr(lsym, ot, s3, 0)
|
||||
ot = dsymptr(lsym, ot, hasher, 0)
|
||||
var flags uint32
|
||||
// Note: flags must match maptype accessors in ../../../../runtime/type.go
|
||||
// and maptype builder in ../../../../reflect/type.go:MapOf.
|
||||
|
|
@ -1673,78 +1671,6 @@ func (a typesByString) Less(i, j int) bool {
|
|||
}
|
||||
func (a typesByString) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
|
||||
func dalgsym(t *types.Type) *obj.LSym {
|
||||
var lsym *obj.LSym
|
||||
var hashfunc *obj.LSym
|
||||
var eqfunc *obj.LSym
|
||||
|
||||
// dalgsym is only called for a type that needs an algorithm table,
|
||||
// which implies that the type is comparable (or else it would use ANOEQ).
|
||||
|
||||
if algtype(t) == AMEM {
|
||||
// we use one algorithm table for all AMEM types of a given size
|
||||
p := fmt.Sprintf(".alg%d", t.Width)
|
||||
|
||||
s := typeLookup(p)
|
||||
lsym = s.Linksym()
|
||||
if s.AlgGen() {
|
||||
return lsym
|
||||
}
|
||||
s.SetAlgGen(true)
|
||||
|
||||
if memhashvarlen == nil {
|
||||
memhashvarlen = sysfunc("memhash_varlen")
|
||||
memequalvarlen = sysvar("memequal_varlen") // asm func
|
||||
}
|
||||
|
||||
// make hash closure
|
||||
p = fmt.Sprintf(".hashfunc%d", t.Width)
|
||||
|
||||
hashfunc = typeLookup(p).Linksym()
|
||||
|
||||
ot := 0
|
||||
ot = dsymptr(hashfunc, ot, memhashvarlen, 0)
|
||||
ot = duintptr(hashfunc, ot, uint64(t.Width)) // size encoded in closure
|
||||
ggloblsym(hashfunc, int32(ot), obj.DUPOK|obj.RODATA)
|
||||
|
||||
// make equality closure
|
||||
p = fmt.Sprintf(".eqfunc%d", t.Width)
|
||||
|
||||
eqfunc = typeLookup(p).Linksym()
|
||||
|
||||
ot = 0
|
||||
ot = dsymptr(eqfunc, ot, memequalvarlen, 0)
|
||||
ot = duintptr(eqfunc, ot, uint64(t.Width))
|
||||
ggloblsym(eqfunc, int32(ot), obj.DUPOK|obj.RODATA)
|
||||
} else {
|
||||
// generate an alg table specific to this type
|
||||
s := typesymprefix(".alg", t)
|
||||
lsym = s.Linksym()
|
||||
|
||||
hash := typesymprefix(".hash", t)
|
||||
eq := typesymprefix(".eq", t)
|
||||
hashfunc = typesymprefix(".hashfunc", t).Linksym()
|
||||
eqfunc = typesymprefix(".eqfunc", t).Linksym()
|
||||
|
||||
genhash(hash, t)
|
||||
geneq(eq, t)
|
||||
|
||||
// make Go funcs (closures) for calling hash and equal from Go
|
||||
dsymptr(hashfunc, 0, hash.Linksym(), 0)
|
||||
ggloblsym(hashfunc, int32(Widthptr), obj.DUPOK|obj.RODATA)
|
||||
dsymptr(eqfunc, 0, eq.Linksym(), 0)
|
||||
ggloblsym(eqfunc, int32(Widthptr), obj.DUPOK|obj.RODATA)
|
||||
}
|
||||
|
||||
// ../../../../runtime/alg.go:/typeAlg
|
||||
ot := 0
|
||||
|
||||
ot = dsymptr(lsym, ot, hashfunc, 0)
|
||||
ot = dsymptr(lsym, ot, eqfunc, 0)
|
||||
ggloblsym(lsym, int32(ot), obj.DUPOK|obj.RODATA)
|
||||
return lsym
|
||||
}
|
||||
|
||||
// maxPtrmaskBytes is the maximum length of a GC ptrmask bitmap,
|
||||
// which holds 1-bit entries describing where pointers are in a given type.
|
||||
// Above this length, the GC information is recorded as a GC program,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue