Skip to content

Commit b3e1974

Browse files
dgryskiaykevl
authored andcommitted
runtime: add maps.clone
Fixes #4382
1 parent fa12450 commit b3e1974

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/runtime/hashmap.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,13 +281,26 @@ func hashmapInsertIntoNewBucket(m *hashmap, key, value unsafe.Pointer, tophash u
281281
}
282282

283283
func hashmapGrow(m *hashmap) {
284+
// allocate our new buckets twice as big
285+
n := hashmapCopy(m, m.bucketBits+1)
286+
*m = n
287+
}
288+
289+
//go:linkname hashmapClone maps.clone
290+
func hashmapClone(intf _interface) _interface {
291+
typ, val := decomposeInterface(intf)
292+
m := (*hashmap)(val)
293+
n := hashmapCopy(m, m.bucketBits)
294+
return composeInterface(typ, unsafe.Pointer(&n))
295+
}
296+
297+
func hashmapCopy(m *hashmap, sizeBits uint8) hashmap {
284298
// clone map as empty
285299
n := *m
286300
n.count = 0
287301
n.seed = uintptr(fastrand())
288302

289-
// allocate our new buckets twice as big
290-
n.bucketBits = m.bucketBits + 1
303+
n.bucketBits = sizeBits
291304
numBuckets := uintptr(1) << n.bucketBits
292305
bucketBufSize := hashmapBucketSize(m)
293306
n.buckets = alloc(bucketBufSize*numBuckets, nil)
@@ -303,7 +316,7 @@ func hashmapGrow(m *hashmap) {
303316
hashmapSet(&n, key, value, h)
304317
}
305318

306-
*m = n
319+
return n
307320
}
308321

309322
// Get the value of a specified key, or zero the value if not found.

0 commit comments

Comments
 (0)