Skip to content

Commit 7e05efa

Browse files
dgryskideadprogram
authored andcommitted
src/runtime: first darft of map growth code
Fixes #1553
1 parent b02cea0 commit 7e05efa

File tree

2 files changed

+156
-28
lines changed

2 files changed

+156
-28
lines changed

compiler/map.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,41 @@ import (
1010
"tinygo.org/x/go-llvm"
1111
)
1212

13+
// constants for hashmap algorithms; must match src/runtime/hashmap.go
14+
const (
15+
hashmapAlgorithmBinary = iota
16+
hashmapAlgorithmString
17+
hashmapAlgorithmInterface
18+
)
19+
1320
// createMakeMap creates a new map object (runtime.hashmap) by allocating and
1421
// initializing an appropriately sized object.
1522
func (b *builder) createMakeMap(expr *ssa.MakeMap) (llvm.Value, error) {
1623
mapType := expr.Type().Underlying().(*types.Map)
1724
keyType := mapType.Key().Underlying()
1825
llvmValueType := b.getLLVMType(mapType.Elem().Underlying())
1926
var llvmKeyType llvm.Type
27+
var alg uint64 // must match values in src/runtime/hashmap.go
2028
if t, ok := keyType.(*types.Basic); ok && t.Info()&types.IsString != 0 {
2129
// String keys.
2230
llvmKeyType = b.getLLVMType(keyType)
31+
alg = hashmapAlgorithmString
2332
} else if hashmapIsBinaryKey(keyType) {
2433
// Trivially comparable keys.
2534
llvmKeyType = b.getLLVMType(keyType)
35+
alg = hashmapAlgorithmBinary
2636
} else {
2737
// All other keys. Implemented as map[interface{}]valueType for ease of
2838
// implementation.
2939
llvmKeyType = b.getLLVMRuntimeType("_interface")
40+
alg = hashmapAlgorithmInterface
3041
}
3142
keySize := b.targetData.TypeAllocSize(llvmKeyType)
3243
valueSize := b.targetData.TypeAllocSize(llvmValueType)
3344
llvmKeySize := llvm.ConstInt(b.ctx.Int8Type(), keySize, false)
3445
llvmValueSize := llvm.ConstInt(b.ctx.Int8Type(), valueSize, false)
3546
sizeHint := llvm.ConstInt(b.uintptrType, 8, false)
47+
algEnum := llvm.ConstInt(b.ctx.Int8Type(), alg, false)
3648
if expr.Reserve != nil {
3749
sizeHint = b.getValue(expr.Reserve)
3850
var err error
@@ -41,7 +53,7 @@ func (b *builder) createMakeMap(expr *ssa.MakeMap) (llvm.Value, error) {
4153
return llvm.Value{}, err
4254
}
4355
}
44-
hashmap := b.createRuntimeCall("hashmapMake", []llvm.Value{llvmKeySize, llvmValueSize, sizeHint}, "")
56+
hashmap := b.createRuntimeCall("hashmapMake", []llvm.Value{llvmKeySize, llvmValueSize, sizeHint, algEnum}, "")
4557
return hashmap, nil
4658
}
4759

0 commit comments

Comments
 (0)