aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/hashmap.go
diff options
context:
space:
mode:
authorMartin Möhrmann <moehrmann@google.com>2017-08-14 10:16:21 +0200
committerMartin Möhrmann <moehrmann@google.com>2017-08-22 20:28:21 +0000
commitcbc4e5d9c4f2444c5d40ae6333b4e1f4c9cfbd41 (patch)
treeba4c93d8a8fef88cf61720332ab2426fd34c3d3f /src/runtime/hashmap.go
parent0fb0f575bcd2dc3e00a370b325e7e6d020f226b8 (diff)
downloadgo-cbc4e5d9c4f2444c5d40ae6333b4e1f4c9cfbd41.tar.xz
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/55142 Reviewed-by: Keith Randall <khr@golang.org>
Diffstat (limited to 'src/runtime/hashmap.go')
-rw-r--r--src/runtime/hashmap.go19
1 files changed, 13 insertions, 6 deletions
diff --git a/src/runtime/hashmap.go b/src/runtime/hashmap.go
index 852b65d1db..d4d02388db 100644
--- a/src/runtime/hashmap.go
+++ b/src/runtime/hashmap.go
@@ -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)
// 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.
// 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.
-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 {
println("runtime: sizeof(hmap) =", sz, ", t.hmap.size =", t.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
}
@@ -589,7 +596,7 @@ again:
// 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(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)
goto again // Growing the table invalidates everything, so try again
}
@@ -920,7 +927,7 @@ func hashGrow(t *maptype, h *hmap) {
// Otherwise, there are too many overflow buckets,
// so keep the same number of buckets and "grow" laterally.
bigger := uint8(1)
- if !overLoadFactor(int64(h.count), h.B) {
+ if !overLoadFactor(h.count, h.B) {
bigger = 0
h.flags |= sameSizeGrow
}
@@ -959,7 +966,7 @@ func hashGrow(t *maptype, h *hmap) {
}
// 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)
}
@@ -1168,7 +1175,7 @@ func ismapkey(t *_type) bool {
//go:linkname reflect_makemap reflect.makemap
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