Skip to content

Commit cc47df0

Browse files
ianlancetaylorgopherbot
authored andcommitted
runtime: use real type size in map keys and values functions
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 <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent dc12cb1 commit cc47df0

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/runtime/export_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ var CgoCheckPointer = cgoCheckPointer
5353
const TracebackInnerFrames = tracebackInnerFrames
5454
const TracebackOuterFrames = tracebackOuterFrames
5555

56+
var MapKeys = keys
57+
var MapValues = values
58+
5659
var LockPartialOrder = lockPartialOrder
5760

5861
type LockRank lockRank

src/runtime/map.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,7 +1651,7 @@ func copyKeys(t *maptype, h *hmap, b *bmap, s *slice, offset uint8) {
16511651
if s.len >= s.cap {
16521652
fatal("concurrent map read and map write")
16531653
}
1654-
typedmemmove(t.Key, add(s.array, uintptr(s.len)*uintptr(t.KeySize)), k)
1654+
typedmemmove(t.Key, add(s.array, uintptr(s.len)*uintptr(t.Key.Size())), k)
16551655
s.len++
16561656
}
16571657
b = b.overflow(t)
@@ -1716,7 +1716,7 @@ func copyValues(t *maptype, h *hmap, b *bmap, s *slice, offset uint8) {
17161716
if s.len >= s.cap {
17171717
fatal("concurrent map read and map write")
17181718
}
1719-
typedmemmove(t.Elem, add(s.array, uintptr(s.len)*uintptr(t.ValueSize)), ele)
1719+
typedmemmove(t.Elem, add(s.array, uintptr(s.len)*uintptr(t.Elem.Size())), ele)
17201720
s.len++
17211721
}
17221722
b = b.overflow(t)

src/runtime/map_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,3 +1434,33 @@ func TestLoadFactor(t *testing.T) {
14341434
}
14351435
}
14361436
}
1437+
1438+
func TestMapKeys(t *testing.T) {
1439+
type key struct {
1440+
s string
1441+
pad [128]byte // sizeof(key) > abi.MapMaxKeyBytes
1442+
}
1443+
m := map[key]int{{s: "a"}: 1, {s: "b"}: 2}
1444+
keys := make([]key, 0, len(m))
1445+
runtime.MapKeys(m, unsafe.Pointer(&keys))
1446+
for _, k := range keys {
1447+
if len(k.s) != 1 {
1448+
t.Errorf("len(k.s) == %d, want 1", len(k.s))
1449+
}
1450+
}
1451+
}
1452+
1453+
func TestMapValues(t *testing.T) {
1454+
type val struct {
1455+
s string
1456+
pad [128]byte // sizeof(val) > abi.MapMaxElemBytes
1457+
}
1458+
m := map[int]val{1: {s: "a"}, 2: {s: "b"}}
1459+
vals := make([]val, 0, len(m))
1460+
runtime.MapValues(m, unsafe.Pointer(&vals))
1461+
for _, v := range vals {
1462+
if len(v.s) != 1 {
1463+
t.Errorf("len(v.s) == %d, want 1", len(v.s))
1464+
}
1465+
}
1466+
}

0 commit comments

Comments
 (0)