1
0
mirror of https://github.com/golang/go synced 2024-11-22 16:25:07 -07:00
go/test/recover2.go
Keith Randall 36f30ba289 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-09-03 20:41:29 +00:00

86 lines
1.2 KiB
Go

// run
// Copyright 2010 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.
// Test of recover for run-time errors.
// TODO(rsc):
// null pointer accesses
package main
import "strings"
var x = make([]byte, 10)
func main() {
test1()
test2()
test3()
test4()
test5()
test6()
test7()
}
func mustRecover(s string) {
v := recover()
if v == nil {
panic("expected panic")
}
if e := v.(error).Error(); strings.Index(e, s) < 0 {
panic("want: " + s + "; have: " + e)
}
}
func test1() {
defer mustRecover("index")
println(x[123])
}
func test2() {
defer mustRecover("slice")
println(x[5:15])
}
func test3() {
defer mustRecover("slice")
var lo = 11
var hi = 9
println(x[lo:hi])
}
func test4() {
defer mustRecover("interface")
var x interface{} = 1
println(x.(float32))
}
type T struct {
a, b int
c []int
}
func test5() {
defer mustRecover("uncomparable")
var x T
var z interface{} = x
println(z != z)
}
func test6() {
defer mustRecover("unhashable type main.T")
var x T
var z interface{} = x
m := make(map[interface{}]int)
m[z] = 1
}
func test7() {
defer mustRecover("divide by zero")
var x, y int
println(x / y)
}