Skip to content

Commit

Permalink
runtime: add maps.clone
Browse files Browse the repository at this point in the history
  • Loading branch information
dgryski authored and aykevl committed Oct 2, 2024
1 parent fa12450 commit b3e1974
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/runtime/hashmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,26 @@ func hashmapInsertIntoNewBucket(m *hashmap, key, value unsafe.Pointer, tophash u
}

func hashmapGrow(m *hashmap) {
// allocate our new buckets twice as big
n := hashmapCopy(m, m.bucketBits+1)
*m = n
}

//go:linkname hashmapClone maps.clone
func hashmapClone(intf _interface) _interface {
typ, val := decomposeInterface(intf)
m := (*hashmap)(val)
n := hashmapCopy(m, m.bucketBits)
return composeInterface(typ, unsafe.Pointer(&n))
}

func hashmapCopy(m *hashmap, sizeBits uint8) hashmap {
// clone map as empty
n := *m
n.count = 0
n.seed = uintptr(fastrand())

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

*m = n
return n
}

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

0 comments on commit b3e1974

Please sign in to comment.