]> Cypherpunks.ru repositories - gostls13.git/commitdiff
runtime: use real type size in map keys and values functions
authorIan Lance Taylor <iant@golang.org>
Fri, 13 Oct 2023 17:34:30 +0000 (10:34 -0700)
committerGopher Robot <gobot@golang.org>
Sat, 14 Oct 2023 21:46:29 +0000 (21:46 +0000)
We were using the size stored in the map, which is the smaller
of the real type size and 128.

As of CL 61538 we don't use these functions, but we expect to
use them again in the future after #61626 is resolved.

Change-Id: I7bfb4af5f0e3a56361d4019a8ed7c1ec59ff31fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/535215
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Keith Randall <khr@google.com>
src/runtime/export_test.go
src/runtime/map.go
src/runtime/map_test.go

index 6335dab41bff9f07f03de21dd91b0ce7e8b8f359..fc681b140e2729f748c598955d2d7de7a6380788 100644 (file)
@@ -53,6 +53,9 @@ var CgoCheckPointer = cgoCheckPointer
 const TracebackInnerFrames = tracebackInnerFrames
 const TracebackOuterFrames = tracebackOuterFrames
 
+var MapKeys = keys
+var MapValues = values
+
 var LockPartialOrder = lockPartialOrder
 
 type LockRank lockRank
index 5b264b07136040e6fe5083da44157325bf07529f..68ba4e44b83165d65f30567ee675a1eb64da001b 100644 (file)
@@ -1651,7 +1651,7 @@ func copyKeys(t *maptype, h *hmap, b *bmap, s *slice, offset uint8) {
                        if s.len >= s.cap {
                                fatal("concurrent map read and map write")
                        }
-                       typedmemmove(t.Key, add(s.array, uintptr(s.len)*uintptr(t.KeySize)), k)
+                       typedmemmove(t.Key, add(s.array, uintptr(s.len)*uintptr(t.Key.Size())), k)
                        s.len++
                }
                b = b.overflow(t)
@@ -1716,7 +1716,7 @@ func copyValues(t *maptype, h *hmap, b *bmap, s *slice, offset uint8) {
                        if s.len >= s.cap {
                                fatal("concurrent map read and map write")
                        }
-                       typedmemmove(t.Elem, add(s.array, uintptr(s.len)*uintptr(t.ValueSize)), ele)
+                       typedmemmove(t.Elem, add(s.array, uintptr(s.len)*uintptr(t.Elem.Size())), ele)
                        s.len++
                }
                b = b.overflow(t)
index 7e911b9fc915b05d3427b300983dc0ecc83aa3eb..2c51236f16b9f98e0d2cdf8d4bc182a49db719dd 100644 (file)
@@ -1434,3 +1434,33 @@ func TestLoadFactor(t *testing.T) {
                }
        }
 }
+
+func TestMapKeys(t *testing.T) {
+       type key struct {
+               s   string
+               pad [128]byte // sizeof(key) > abi.MapMaxKeyBytes
+       }
+       m := map[key]int{{s: "a"}: 1, {s: "b"}: 2}
+       keys := make([]key, 0, len(m))
+       runtime.MapKeys(m, unsafe.Pointer(&keys))
+       for _, k := range keys {
+               if len(k.s) != 1 {
+                       t.Errorf("len(k.s) == %d, want 1", len(k.s))
+               }
+       }
+}
+
+func TestMapValues(t *testing.T) {
+       type val struct {
+               s   string
+               pad [128]byte // sizeof(val) > abi.MapMaxElemBytes
+       }
+       m := map[int]val{1: {s: "a"}, 2: {s: "b"}}
+       vals := make([]val, 0, len(m))
+       runtime.MapValues(m, unsafe.Pointer(&vals))
+       for _, v := range vals {
+               if len(v.s) != 1 {
+                       t.Errorf("len(v.s) == %d, want 1", len(v.s))
+               }
+       }
+}