From 8925290cf701fc8f7ec95e4df3d6a8d423b26780 Mon Sep 17 00:00:00 2001 From: Keith Randall Date: Fri, 18 Sep 2020 14:19:22 -0700 Subject: reflect: use zero buffer to back the Value returned by Zero MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In the common case (<1KB types), no allocation is required by reflect.Zero. Also use memclr instead of memmove in Set when the source is known to be zero. Fixes #33136 Change-Id: Ic66871930fbb53328032e587153ebd12995ccf55 Reviewed-on: https://go-review.googlesource.com/c/go/+/192331 Trust: Keith Randall Run-TryBot: Keith Randall TryBot-Result: Go Bot Reviewed-by: Martin Möhrmann --- src/runtime/map.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/runtime/map.go') diff --git a/src/runtime/map.go b/src/runtime/map.go index 8be1d3991d..6f31f23d6f 100644 --- a/src/runtime/map.go +++ b/src/runtime/map.go @@ -1380,5 +1380,5 @@ func reflectlite_maplen(h *hmap) int { return h.count } -const maxZero = 1024 // must match value in cmd/compile/internal/gc/walk.go:zeroValSize +const maxZero = 1024 // must match value in reflect/value.go:maxZero cmd/compile/internal/gc/walk.go:zeroValSize var zeroVal [maxZero]byte -- cgit v1.3 From 846dce9d05f19a1f53465e62a304dea21b99f910 Mon Sep 17 00:00:00 2001 From: mengxiaodong <920432478@qq.com> Date: Mon, 28 Sep 2020 17:38:13 +0800 Subject: runtime: code cleanup about map 1.Revise ambiguous comments: "all current buckets" means buckets in hmap.buckets, actually current bucket and all the overflow buckets connected to it are full 2.All the pointer address add use src/runtime/stubs.go:add, keep the code style uniform Change-Id: Idc7224dbe6c391e1b03bf5d009c3734bc75187ce Reviewed-on: https://go-review.googlesource.com/c/go/+/257979 Reviewed-by: Austin Clements Reviewed-by: Keith Randall Run-TryBot: Austin Clements TryBot-Result: Go Bot --- src/runtime/map.go | 4 ++-- src/runtime/map_fast32.go | 8 ++++---- src/runtime/map_fast64.go | 8 ++++---- src/runtime/map_faststr.go | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) (limited to 'src/runtime/map.go') diff --git a/src/runtime/map.go b/src/runtime/map.go index 6f31f23d6f..5ac3a9958b 100644 --- a/src/runtime/map.go +++ b/src/runtime/map.go @@ -599,7 +599,7 @@ again: if h.growing() { growWork(t, h, bucket) } - b := (*bmap)(unsafe.Pointer(uintptr(h.buckets) + bucket*uintptr(t.bucketsize))) + b := (*bmap)(add(h.buckets, bucket*uintptr(t.bucketsize))) top := tophash(hash) var inserti *uint8 @@ -650,7 +650,7 @@ bucketloop: } if inserti == nil { - // all current buckets are full, allocate a new one. + // The current bucket and all the overflow buckets connected to it are full, allocate a new one. newb := h.newoverflow(t, b) inserti = &newb.tophash[0] insertk = add(unsafe.Pointer(newb), dataOffset) diff --git a/src/runtime/map_fast32.go b/src/runtime/map_fast32.go index d80f5eac78..8d52dad217 100644 --- a/src/runtime/map_fast32.go +++ b/src/runtime/map_fast32.go @@ -114,7 +114,7 @@ again: if h.growing() { growWork_fast32(t, h, bucket) } - b := (*bmap)(unsafe.Pointer(uintptr(h.buckets) + bucket*uintptr(t.bucketsize))) + b := (*bmap)(add(h.buckets, bucket*uintptr(t.bucketsize))) var insertb *bmap var inserti uintptr @@ -158,7 +158,7 @@ bucketloop: } if insertb == nil { - // all current buckets are full, allocate a new one. + // The current bucket and all the overflow buckets connected to it are full, allocate a new one. insertb = h.newoverflow(t, b) inserti = 0 // not necessary, but avoids needlessly spilling inserti } @@ -204,7 +204,7 @@ again: if h.growing() { growWork_fast32(t, h, bucket) } - b := (*bmap)(unsafe.Pointer(uintptr(h.buckets) + bucket*uintptr(t.bucketsize))) + b := (*bmap)(add(h.buckets, bucket*uintptr(t.bucketsize))) var insertb *bmap var inserti uintptr @@ -248,7 +248,7 @@ bucketloop: } if insertb == nil { - // all current buckets are full, allocate a new one. + // The current bucket and all the overflow buckets connected to it are full, allocate a new one. insertb = h.newoverflow(t, b) inserti = 0 // not necessary, but avoids needlessly spilling inserti } diff --git a/src/runtime/map_fast64.go b/src/runtime/map_fast64.go index 3bc84bbdd3..f1368dc774 100644 --- a/src/runtime/map_fast64.go +++ b/src/runtime/map_fast64.go @@ -114,7 +114,7 @@ again: if h.growing() { growWork_fast64(t, h, bucket) } - b := (*bmap)(unsafe.Pointer(uintptr(h.buckets) + bucket*uintptr(t.bucketsize))) + b := (*bmap)(add(h.buckets, bucket*uintptr(t.bucketsize))) var insertb *bmap var inserti uintptr @@ -158,7 +158,7 @@ bucketloop: } if insertb == nil { - // all current buckets are full, allocate a new one. + // The current bucket and all the overflow buckets connected to it are full, allocate a new one. insertb = h.newoverflow(t, b) inserti = 0 // not necessary, but avoids needlessly spilling inserti } @@ -204,7 +204,7 @@ again: if h.growing() { growWork_fast64(t, h, bucket) } - b := (*bmap)(unsafe.Pointer(uintptr(h.buckets) + bucket*uintptr(t.bucketsize))) + b := (*bmap)(add(h.buckets, bucket*uintptr(t.bucketsize))) var insertb *bmap var inserti uintptr @@ -248,7 +248,7 @@ bucketloop: } if insertb == nil { - // all current buckets are full, allocate a new one. + // The current bucket and all the overflow buckets connected to it are full, allocate a new one. insertb = h.newoverflow(t, b) inserti = 0 // not necessary, but avoids needlessly spilling inserti } diff --git a/src/runtime/map_faststr.go b/src/runtime/map_faststr.go index 108c502394..2d1ac762a8 100644 --- a/src/runtime/map_faststr.go +++ b/src/runtime/map_faststr.go @@ -225,7 +225,7 @@ again: if h.growing() { growWork_faststr(t, h, bucket) } - b := (*bmap)(unsafe.Pointer(uintptr(h.buckets) + bucket*uintptr(t.bucketsize))) + b := (*bmap)(add(h.buckets, bucket*uintptr(t.bucketsize))) top := tophash(hash) var insertb *bmap @@ -274,7 +274,7 @@ bucketloop: } if insertb == nil { - // all current buckets are full, allocate a new one. + // The current bucket and all the overflow buckets connected to it are full, allocate a new one. insertb = h.newoverflow(t, b) inserti = 0 // not necessary, but avoids needlessly spilling inserti } -- cgit v1.3 From db8142fb8631df3ee56983cbc13db997c16f2f6f Mon Sep 17 00:00:00 2001 From: Vee Zhang Date: Thu, 5 Nov 2020 08:27:55 +0000 Subject: runtime: fix file references in hiter's comments The file "cmd/internal/gc/range.go" does not exist, but should be "cmd/compile/internal/gc/range.go". Change-Id: I26e5560b9d0b7eea8502c6b375e45fc87aed1276 GitHub-Last-Rev: 5f19dca7e9dab942a54257c5da05bce780744b19 GitHub-Pull-Request: golang/go#42391 Reviewed-on: https://go-review.googlesource.com/c/go/+/267837 Reviewed-by: Ian Lance Taylor Trust: Emmanuel Odeke --- src/runtime/map.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/runtime/map.go') diff --git a/src/runtime/map.go b/src/runtime/map.go index 5ac3a9958b..0beff57a1a 100644 --- a/src/runtime/map.go +++ b/src/runtime/map.go @@ -162,8 +162,8 @@ type bmap struct { // If you modify hiter, also change cmd/compile/internal/gc/reflect.go to indicate // the layout of this structure. type hiter struct { - key unsafe.Pointer // Must be in first position. Write nil to indicate iteration end (see cmd/internal/gc/range.go). - elem unsafe.Pointer // Must be in second position (see cmd/internal/gc/range.go). + key unsafe.Pointer // Must be in first position. Write nil to indicate iteration end (see cmd/compile/internal/gc/range.go). + elem unsafe.Pointer // Must be in second position (see cmd/compile/internal/gc/range.go). t *maptype h *hmap buckets unsafe.Pointer // bucket ptr at hash_iter initialization time -- cgit v1.3