compiler,runtime: use the size hint when creating a new map

It defaults to hint/8 number of buckets. This number may be tuned in the
future.
This commit is contained in:
Ayke van Laethem
2019-05-13 16:38:58 +02:00
committed by Ron Evans
parent 17c42810d0
commit 55fc7b904a
4 changed files with 34 additions and 5 deletions
+10 -4
View File
@@ -60,14 +60,20 @@ func hashmapTopHash(hash uint32) uint8 {
}
// Create a new hashmap with the given keySize and valueSize.
func hashmapMake(keySize, valueSize uint8) *hashmap {
func hashmapMake(keySize, valueSize uint8, sizeHint uintptr) *hashmap {
numBuckets := sizeHint / 8
bucketBits := uint8(0)
for numBuckets != 0 {
numBuckets /= 2
bucketBits++
}
bucketBufSize := unsafe.Sizeof(hashmapBucket{}) + uintptr(keySize)*8 + uintptr(valueSize)*8
bucket := alloc(bucketBufSize)
buckets := alloc(bucketBufSize * (1 << bucketBits))
return &hashmap{
buckets: bucket,
buckets: buckets,
keySize: keySize,
valueSize: valueSize,
bucketBits: 0,
bucketBits: bucketBits,
}
}