Commit cbc4e5d9 authored by Martin Möhrmann's avatar Martin Möhrmann

cmd/compile: generate makemap calls with int arguments

Where possible generate calls to runtime makemap with int hint argument
during compile time instead of makemap with int64 hint argument.

This eliminates converting the hint argument for calls to makemap with
int64 hint argument for platforms where int64 values do not fit into
an argument of type int.

A similar optimization for makeslice was introduced in CL
golang.org/cl/27851.

386:
name         old time/op    new time/op    delta
NewEmptyMap    53.5ns ± 5%    41.9ns ± 5%  -21.56%  (p=0.000 n=10+10)
NewSmallMap     182ns ± 1%     165ns ± 1%   -8.92%  (p=0.000 n=10+10)

Change-Id: Ibd2b4c57b36f171b173bf7a0602b3a59771e6e44
Reviewed-on: https://go-review.googlesource.com/55142Reviewed-by: default avatarKeith Randall <khr@golang.org>
parent 0fb0f575
This diff is collapsed.
...@@ -92,7 +92,8 @@ func ifaceeq(tab *uintptr, x, y unsafe.Pointer) (ret bool) ...@@ -92,7 +92,8 @@ func ifaceeq(tab *uintptr, x, y unsafe.Pointer) (ret bool)
func efaceeq(typ *uintptr, x, y unsafe.Pointer) (ret bool) func efaceeq(typ *uintptr, x, y unsafe.Pointer) (ret bool)
// *byte is really *runtime.Type // *byte is really *runtime.Type
func makemap(mapType *byte, hint int64, mapbuf *any) (hmap map[any]any) func makemap64(mapType *byte, hint int64, mapbuf *any) (hmap map[any]any)
func makemap(mapType *byte, hint int, mapbuf *any) (hmap map[any]any)
func mapaccess1(mapType *byte, hmap map[any]any, key *any) (val *any) func mapaccess1(mapType *byte, hmap map[any]any, key *any) (val *any)
func mapaccess1_fast32(mapType *byte, hmap map[any]any, key any) (val *any) func mapaccess1_fast32(mapType *byte, hmap map[any]any, key any) (val *any)
func mapaccess1_fast64(mapType *byte, hmap map[any]any, key any) (val *any) func mapaccess1_fast64(mapType *byte, hmap map[any]any, key any) (val *any)
......
...@@ -1484,9 +1484,24 @@ opswitch: ...@@ -1484,9 +1484,24 @@ opswitch:
h = nodnil() h = nodnil()
} }
fn := syslook("makemap") // When hint fits into int, use makemap instead of
// makemap64, which is faster and shorter on 32 bit platforms.
hint := n.Left
fnname := "makemap64"
argtype := types.Types[TINT64]
// Type checking guarantees that TIDEAL hint is positive and fits in an int.
// See checkmake call in TMAP case of OMAKE case in OpSwitch in typecheck1 function.
// The case of hint overflow when converting TUINT or TUINTPTR to TINT
// will be handled by the negative range checks in makemap during runtime.
if hint.Type.IsKind(TIDEAL) || maxintval[hint.Type.Etype].Cmp(maxintval[TUINT]) <= 0 {
fnname = "makemap"
argtype = types.Types[TINT]
}
fn := syslook(fnname)
fn = substArgTypes(fn, hmapType, t.Key(), t.Val()) fn = substArgTypes(fn, hmapType, t.Key(), t.Val())
n = mkcall1(fn, n.Type, init, typename(n.Type), conv(n.Left, types.Types[TINT64]), h) n = mkcall1(fn, n.Type, init, typename(n.Type), conv(hint, argtype), h)
case OMAKESLICE: case OMAKESLICE:
l := n.Left l := n.Left
......
...@@ -255,18 +255,25 @@ func (h *hmap) createOverflow() { ...@@ -255,18 +255,25 @@ func (h *hmap) createOverflow() {
} }
} }
func makemap64(t *maptype, hint int64, h *hmap, bucket unsafe.Pointer) *hmap {
if int64(int(hint)) != hint {
hint = 0
}
return makemap(t, int(hint), h)
}
// makemap implements a Go map creation make(map[k]v, hint) // makemap implements a Go map creation make(map[k]v, hint)
// If the compiler has determined that the map or the first bucket // If the compiler has determined that the map or the first bucket
// can be created on the stack, h and/or bucket may be non-nil. // can be created on the stack, h and/or bucket may be non-nil.
// If h != nil, the map can be created directly in h. // If h != nil, the map can be created directly in h.
// If h.buckets != nil, bucket pointed to can be used as the first bucket. // If h.buckets != nil, bucket pointed to can be used as the first bucket.
func makemap(t *maptype, hint int64, h *hmap) *hmap { func makemap(t *maptype, hint int, h *hmap) *hmap {
if sz := unsafe.Sizeof(hmap{}); sz > 48 || sz != t.hmap.size { if sz := unsafe.Sizeof(hmap{}); sz > 48 || sz != t.hmap.size {
println("runtime: sizeof(hmap) =", sz, ", t.hmap.size =", t.hmap.size) println("runtime: sizeof(hmap) =", sz, ", t.hmap.size =", t.hmap.size)
throw("bad hmap size") throw("bad hmap size")
} }
if hint < 0 || hint > int64(maxSliceCap(t.bucket.size)) { if hint < 0 || hint > int(maxSliceCap(t.bucket.size)) {
hint = 0 hint = 0
} }
...@@ -589,7 +596,7 @@ again: ...@@ -589,7 +596,7 @@ again:
// If we hit the max load factor or we have too many overflow buckets, // 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. // and we're not already in the middle of growing, start growing.
if !h.growing() && (overLoadFactor(int64(h.count), h.B) || tooManyOverflowBuckets(h.noverflow, h.B)) { if !h.growing() && (overLoadFactor(h.count, h.B) || tooManyOverflowBuckets(h.noverflow, h.B)) {
hashGrow(t, h) hashGrow(t, h)
goto again // Growing the table invalidates everything, so try again goto again // Growing the table invalidates everything, so try again
} }
...@@ -920,7 +927,7 @@ func hashGrow(t *maptype, h *hmap) { ...@@ -920,7 +927,7 @@ func hashGrow(t *maptype, h *hmap) {
// Otherwise, there are too many overflow buckets, // Otherwise, there are too many overflow buckets,
// so keep the same number of buckets and "grow" laterally. // so keep the same number of buckets and "grow" laterally.
bigger := uint8(1) bigger := uint8(1)
if !overLoadFactor(int64(h.count), h.B) { if !overLoadFactor(h.count, h.B) {
bigger = 0 bigger = 0
h.flags |= sameSizeGrow h.flags |= sameSizeGrow
} }
...@@ -959,7 +966,7 @@ func hashGrow(t *maptype, h *hmap) { ...@@ -959,7 +966,7 @@ func hashGrow(t *maptype, h *hmap) {
} }
// overLoadFactor reports whether count items placed in 1<<B buckets is over loadFactor. // overLoadFactor reports whether count items placed in 1<<B buckets is over loadFactor.
func overLoadFactor(count int64, B uint8) bool { func overLoadFactor(count int, B uint8) bool {
return count >= bucketCnt && uint64(count) >= loadFactorNum*((uint64(1)<<B)/loadFactorDen) return count >= bucketCnt && uint64(count) >= loadFactorNum*((uint64(1)<<B)/loadFactorDen)
} }
...@@ -1168,7 +1175,7 @@ func ismapkey(t *_type) bool { ...@@ -1168,7 +1175,7 @@ func ismapkey(t *_type) bool {
//go:linkname reflect_makemap reflect.makemap //go:linkname reflect_makemap reflect.makemap
func reflect_makemap(t *maptype, cap int) *hmap { func reflect_makemap(t *maptype, cap int) *hmap {
return makemap(t, int64(cap), nil) return makemap(t, cap, nil)
} }
//go:linkname reflect_mapaccess reflect.mapaccess //go:linkname reflect_mapaccess reflect.mapaccess
......
...@@ -462,7 +462,7 @@ again: ...@@ -462,7 +462,7 @@ again:
// If we hit the max load factor or we have too many overflow buckets, // 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. // and we're not already in the middle of growing, start growing.
if !h.growing() && (overLoadFactor(int64(h.count), h.B) || tooManyOverflowBuckets(h.noverflow, h.B)) { if !h.growing() && (overLoadFactor(h.count, h.B) || tooManyOverflowBuckets(h.noverflow, h.B)) {
hashGrow(t, h) hashGrow(t, h)
goto again // Growing the table invalidates everything, so try again goto again // Growing the table invalidates everything, so try again
} }
...@@ -547,7 +547,7 @@ again: ...@@ -547,7 +547,7 @@ again:
// If we hit the max load factor or we have too many overflow buckets, // 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. // and we're not already in the middle of growing, start growing.
if !h.growing() && (overLoadFactor(int64(h.count), h.B) || tooManyOverflowBuckets(h.noverflow, h.B)) { if !h.growing() && (overLoadFactor(h.count, h.B) || tooManyOverflowBuckets(h.noverflow, h.B)) {
hashGrow(t, h) hashGrow(t, h)
goto again // Growing the table invalidates everything, so try again goto again // Growing the table invalidates everything, so try again
} }
...@@ -637,7 +637,7 @@ again: ...@@ -637,7 +637,7 @@ again:
// If we hit the max load factor or we have too many overflow buckets, // 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. // and we're not already in the middle of growing, start growing.
if !h.growing() && (overLoadFactor(int64(h.count), h.B) || tooManyOverflowBuckets(h.noverflow, h.B)) { if !h.growing() && (overLoadFactor(h.count, h.B) || tooManyOverflowBuckets(h.noverflow, h.B)) {
hashGrow(t, h) hashGrow(t, h)
goto again // Growing the table invalidates everything, so try again goto again // Growing the table invalidates everything, so try again
} }
......
// errorcheck
// Copyright 2017 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.
// Ensure that typed non-integer, negative and too large
// values are not accepted as size argument in make for
// maps.
package main
type T map[int]int
var sink T
func main() {
sink = make(T, -1) // ERROR "negative size argument in make.*"
sink = make(T, uint64(1<<63)) // ERROR "size argument too large in make.*"
sink = make(T, 0.5) // ERROR "constant 0.5 truncated to integer"
sink = make(T, 1.0)
sink = make(T, float32(1.0)) // ERROR "non-integer size argument in make.*"
sink = make(T, float64(1.0)) // ERROR "non-integer size argument in make.*"
sink = make(T, 1.0)
sink = make(T, float32(1.0)) // ERROR "non-integer size argument in make.*"
sink = make(T, float64(1.0)) // ERROR "non-integer size argument in make.*"
sink = make(T, 1+0i)
sink = make(T, complex64(1+0i)) // ERROR "non-integer size argument in make.*"
sink = make(T, complex128(1+0i)) // ERROR "non-integer size argument in make.*"
sink = make(T, 1+0i)
sink = make(T, complex64(1+0i)) // ERROR "non-integer size argument in make.*"
sink = make(T, complex128(1+0i)) // ERROR "non-integer size argument in make.*"
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment