mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-16 10:43:29 +00:00
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:
committed by
Ron Evans
parent
17c42810d0
commit
55fc7b904a
+10
-1
@@ -1425,7 +1425,16 @@ func (c *Compiler) parseExpr(frame *Frame, expr ssa.Value) (llvm.Value, error) {
|
|||||||
valueSize := c.targetData.TypeAllocSize(llvmValueType)
|
valueSize := c.targetData.TypeAllocSize(llvmValueType)
|
||||||
llvmKeySize := llvm.ConstInt(c.ctx.Int8Type(), keySize, false)
|
llvmKeySize := llvm.ConstInt(c.ctx.Int8Type(), keySize, false)
|
||||||
llvmValueSize := llvm.ConstInt(c.ctx.Int8Type(), valueSize, false)
|
llvmValueSize := llvm.ConstInt(c.ctx.Int8Type(), valueSize, false)
|
||||||
hashmap := c.createRuntimeCall("hashmapMake", []llvm.Value{llvmKeySize, llvmValueSize}, "")
|
sizeHint := llvm.ConstInt(c.uintptrType, 8, false)
|
||||||
|
if expr.Reserve != nil {
|
||||||
|
sizeHint = c.getValue(frame, expr.Reserve)
|
||||||
|
var err error
|
||||||
|
sizeHint, err = c.parseConvert(expr.Reserve.Type(), types.Typ[types.Uintptr], sizeHint, expr.Pos())
|
||||||
|
if err != nil {
|
||||||
|
return llvm.Value{}, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
hashmap := c.createRuntimeCall("hashmapMake", []llvm.Value{llvmKeySize, llvmValueSize, sizeHint}, "")
|
||||||
return hashmap, nil
|
return hashmap, nil
|
||||||
case *ssa.MakeSlice:
|
case *ssa.MakeSlice:
|
||||||
sliceLen := c.getValue(frame, expr.Len)
|
sliceLen := c.getValue(frame, expr.Len)
|
||||||
|
|||||||
+10
-4
@@ -60,14 +60,20 @@ func hashmapTopHash(hash uint32) uint8 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create a new hashmap with the given keySize and valueSize.
|
// 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
|
bucketBufSize := unsafe.Sizeof(hashmapBucket{}) + uintptr(keySize)*8 + uintptr(valueSize)*8
|
||||||
bucket := alloc(bucketBufSize)
|
buckets := alloc(bucketBufSize * (1 << bucketBits))
|
||||||
return &hashmap{
|
return &hashmap{
|
||||||
buckets: bucket,
|
buckets: buckets,
|
||||||
keySize: keySize,
|
keySize: keySize,
|
||||||
valueSize: valueSize,
|
valueSize: valueSize,
|
||||||
bucketBits: 0,
|
bucketBits: bucketBits,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Vendored
+13
@@ -47,6 +47,18 @@ func main() {
|
|||||||
println(testMapArrayKey[arrKey])
|
println(testMapArrayKey[arrKey])
|
||||||
testMapArrayKey[arrKey] = 5555
|
testMapArrayKey[arrKey] = 5555
|
||||||
println(testMapArrayKey[arrKey])
|
println(testMapArrayKey[arrKey])
|
||||||
|
|
||||||
|
// test preallocated map
|
||||||
|
squares := make(map[int]int, 200)
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
squares[i] = i*i
|
||||||
|
for j := 0; j <= i; j++ {
|
||||||
|
if v := squares[j]; v != j*j {
|
||||||
|
println("unexpected value read back from squares map:", j, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println("tested preallocated map")
|
||||||
}
|
}
|
||||||
|
|
||||||
func readMap(m map[string]int, key string) {
|
func readMap(m map[string]int, key string) {
|
||||||
@@ -56,6 +68,7 @@ func readMap(m map[string]int, key string) {
|
|||||||
println(" ", k, "=", v)
|
println(" ", k, "=", v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func lookup(m map[string]int, key string) {
|
func lookup(m map[string]int, key string) {
|
||||||
value, ok := m[key]
|
value, ok := m[key]
|
||||||
println("lookup with comma-ok:", key, value, ok)
|
println("lookup with comma-ok:", key, value, ok)
|
||||||
|
|||||||
Vendored
+1
@@ -54,3 +54,4 @@ true false 0
|
|||||||
42
|
42
|
||||||
4321
|
4321
|
||||||
5555
|
5555
|
||||||
|
tested preallocated map
|
||||||
|
|||||||
Reference in New Issue
Block a user