compiler,runtime: use the size hint when creating a new map

It defaults to hint/8 number of buckets. This number may be tuned in the
future.
This commit is contained in:
Ayke van Laethem
2019-05-13 16:38:58 +02:00
committed by Ron Evans
parent 17c42810d0
commit 55fc7b904a
4 changed files with 34 additions and 5 deletions
+13
View File
@@ -47,6 +47,18 @@ func main() {
println(testMapArrayKey[arrKey])
testMapArrayKey[arrKey] = 5555
println(testMapArrayKey[arrKey])
// test preallocated map
squares := make(map[int]int, 200)
for i := 0; i < 100; i++ {
squares[i] = i*i
for j := 0; j <= i; j++ {
if v := squares[j]; v != j*j {
println("unexpected value read back from squares map:", j, v)
}
}
}
println("tested preallocated map")
}
func readMap(m map[string]int, key string) {
@@ -56,6 +68,7 @@ func readMap(m map[string]int, key string) {
println(" ", k, "=", v)
}
}
func lookup(m map[string]int, key string) {
value, ok := m[key]
println("lookup with comma-ok:", key, value, ok)
+1
View File
@@ -54,3 +54,4 @@ true false 0
42
4321
5555
tested preallocated map