| 1 |
// run |
| 2 |
|
| 3 |
//go:build !goexperiment.swissmap |
| 4 |
|
| 5 |
// Copyright 2024 The Go Authors. All rights reserved. |
| 6 |
// Use of this source code is governed by a BSD-style |
| 7 |
// license that can be found in the LICENSE file. |
| 8 |
|
| 9 |
package main |
| 10 |
|
| 11 |
import ( |
| 12 |
"maps" |
| 13 |
_ "unsafe" |
| 14 |
) |
| 15 |
|
| 16 |
func main() { |
| 17 |
for i := 0; i < 100; i++ { |
| 18 |
f() |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
const NB = 4 |
| 23 |
|
| 24 |
func f() { |
| 25 |
// Make a map with NB buckets, at max capacity. |
| 26 |
// 6.5 entries/bucket. |
| 27 |
ne := NB * 13 / 2 |
| 28 |
m := map[int]int{} |
| 29 |
for i := 0; i < ne; i++ { |
| 30 |
m[i] = i |
| 31 |
} |
| 32 |
|
| 33 |
// delete/insert a lot, to hopefully get lots of overflow buckets |
| 34 |
// and trigger a same-size grow. |
| 35 |
ssg := false |
| 36 |
for i := ne; i < ne+1000; i++ { |
| 37 |
delete(m, i-ne) |
| 38 |
m[i] = i |
| 39 |
if sameSizeGrow(m) { |
| 40 |
ssg = true |
| 41 |
break |
| 42 |
} |
| 43 |
} |
| 44 |
if !ssg { |
| 45 |
return |
| 46 |
} |
| 47 |
|
| 48 |
// Insert 1 more entry, which would ordinarily trigger a growth. |
| 49 |
// We can't grow while growing, so we instead go over our |
| 50 |
// target capacity. |
| 51 |
m[-1] = -1 |
| 52 |
|
| 53 |
// Cloning in this state will make a map with a destination bucket |
| 54 |
// array twice the size of the source. |
| 55 |
_ = maps.Clone(m) |
| 56 |
} |
| 57 |
|
| 58 |
//go:linkname sameSizeGrow runtime.sameSizeGrowForIssue69110Test |
| 59 |
func sameSizeGrow(m map[int]int) bool |