Skip to content

Commit 2b0a7e3

Browse files
committed
compiler,runtime: make keySize and valueSize uintptr
1 parent ad88d56 commit 2b0a7e3

File tree

2 files changed

+40
-40
lines changed

2 files changed

+40
-40
lines changed

compiler/map.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ func (b *builder) createMakeMap(expr *ssa.MakeMap) (llvm.Value, error) {
4141
}
4242
keySize := b.targetData.TypeAllocSize(llvmKeyType)
4343
valueSize := b.targetData.TypeAllocSize(llvmValueType)
44-
llvmKeySize := llvm.ConstInt(b.ctx.Int8Type(), keySize, false)
45-
llvmValueSize := llvm.ConstInt(b.ctx.Int32Type(), valueSize, false)
44+
llvmKeySize := llvm.ConstInt(b.uintptrType, keySize, false)
45+
llvmValueSize := llvm.ConstInt(b.uintptrType, valueSize, false)
4646
sizeHint := llvm.ConstInt(b.uintptrType, 8, false)
4747
algEnum := llvm.ConstInt(b.ctx.Int8Type(), alg, false)
4848
if expr.Reserve != nil {

src/runtime/hashmap.go

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ type hashmap struct {
1515
buckets unsafe.Pointer // pointer to array of buckets
1616
seed uintptr
1717
count uintptr
18-
valueSize uint32
19-
keySize uint8 // maybe this can store the key type as well? E.g. keysize == 5 means string?
18+
keySize uintptr // maybe this can store the key type as well? E.g. keysize == 5 means string?
19+
valueSize uintptr
2020
bucketBits uint8
2121
keyEqual func(x, y unsafe.Pointer, n uintptr) bool
2222
keyHash func(key unsafe.Pointer, size, seed uintptr) uint32
@@ -60,14 +60,14 @@ func hashmapTopHash(hash uint32) uint8 {
6060
}
6161

6262
// Create a new hashmap with the given keySize and valueSize.
63-
func hashmapMake(keySize uint8, valueSize uint32, sizeHint uintptr, alg uint8) *hashmap {
63+
func hashmapMake(keySize, valueSize uintptr, sizeHint uintptr, alg uint8) *hashmap {
6464
numBuckets := sizeHint / 8
6565
bucketBits := uint8(0)
6666
for numBuckets != 0 {
6767
numBuckets /= 2
6868
bucketBits++
6969
}
70-
bucketBufSize := unsafe.Sizeof(hashmapBucket{}) + uintptr(keySize)*8 + uintptr(valueSize)*8
70+
bucketBufSize := unsafe.Sizeof(hashmapBucket{}) + keySize*8 + valueSize*8
7171
buckets := alloc(bucketBufSize*(1<<bucketBits), nil)
7272

7373
keyHash := hashmapKeyHashAlg(hashmapAlgorithm(alg))
@@ -155,14 +155,14 @@ func hashmapSet(m *hashmap, key unsafe.Pointer, value unsafe.Pointer, hash uint3
155155
if hashmapShouldGrow(m) {
156156
hashmapGrow(m)
157157
// seed changed when we grew; rehash key with new seed
158-
hash = m.keyHash(key, uintptr(m.keySize), m.seed)
158+
hash = m.keyHash(key, m.keySize, m.seed)
159159
}
160160

161161
tophash := hashmapTopHash(hash)
162162

163163
numBuckets := uintptr(1) << m.bucketBits
164164
bucketNumber := (uintptr(hash) & (numBuckets - 1))
165-
bucketSize := unsafe.Sizeof(hashmapBucket{}) + uintptr(m.keySize)*8 + uintptr(m.valueSize)*8
165+
bucketSize := unsafe.Sizeof(hashmapBucket{}) + m.keySize*8 + m.valueSize*8
166166
bucketAddr := uintptr(m.buckets) + bucketSize*bucketNumber
167167
bucket := (*hashmapBucket)(unsafe.Pointer(bucketAddr))
168168
var lastBucket *hashmapBucket
@@ -173,9 +173,9 @@ func hashmapSet(m *hashmap, key unsafe.Pointer, value unsafe.Pointer, hash uint3
173173
var emptySlotTophash *byte
174174
for bucket != nil {
175175
for i := uintptr(0); i < 8; i++ {
176-
slotKeyOffset := unsafe.Sizeof(hashmapBucket{}) + uintptr(m.keySize)*uintptr(i)
176+
slotKeyOffset := unsafe.Sizeof(hashmapBucket{}) + m.keySize*uintptr(i)
177177
slotKey := unsafe.Pointer(uintptr(unsafe.Pointer(bucket)) + slotKeyOffset)
178-
slotValueOffset := unsafe.Sizeof(hashmapBucket{}) + uintptr(m.keySize)*8 + uintptr(m.valueSize)*uintptr(i)
178+
slotValueOffset := unsafe.Sizeof(hashmapBucket{}) + m.keySize*8 + m.valueSize*uintptr(i)
179179
slotValue := unsafe.Pointer(uintptr(unsafe.Pointer(bucket)) + slotValueOffset)
180180
if bucket.tophash[i] == 0 && emptySlotKey == nil {
181181
// Found an empty slot, store it for if we couldn't find an
@@ -186,9 +186,9 @@ func hashmapSet(m *hashmap, key unsafe.Pointer, value unsafe.Pointer, hash uint3
186186
}
187187
if bucket.tophash[i] == tophash {
188188
// Could be an existing key that's the same.
189-
if m.keyEqual(key, slotKey, uintptr(m.keySize)) {
189+
if m.keyEqual(key, slotKey, m.keySize) {
190190
// found same key, replace it
191-
memcpy(slotValue, value, uintptr(m.valueSize))
191+
memcpy(slotValue, value, m.valueSize)
192192
return
193193
}
194194
}
@@ -203,24 +203,24 @@ func hashmapSet(m *hashmap, key unsafe.Pointer, value unsafe.Pointer, hash uint3
203203
return
204204
}
205205
m.count++
206-
memcpy(emptySlotKey, key, uintptr(m.keySize))
207-
memcpy(emptySlotValue, value, uintptr(m.valueSize))
206+
memcpy(emptySlotKey, key, m.keySize)
207+
memcpy(emptySlotValue, value, m.valueSize)
208208
*emptySlotTophash = tophash
209209
}
210210

211211
// hashmapInsertIntoNewBucket creates a new bucket, inserts the given key and
212212
// value into the bucket, and returns a pointer to this bucket.
213213
func hashmapInsertIntoNewBucket(m *hashmap, key, value unsafe.Pointer, tophash uint8) *hashmapBucket {
214-
bucketBufSize := unsafe.Sizeof(hashmapBucket{}) + uintptr(m.keySize)*8 + uintptr(m.valueSize)*8
214+
bucketBufSize := unsafe.Sizeof(hashmapBucket{}) + m.keySize*8 + m.valueSize*8
215215
bucketBuf := alloc(bucketBufSize, nil)
216216
// Insert into the first slot, which is empty as it has just been allocated.
217217
slotKeyOffset := unsafe.Sizeof(hashmapBucket{})
218218
slotKey := unsafe.Pointer(uintptr(bucketBuf) + slotKeyOffset)
219-
slotValueOffset := unsafe.Sizeof(hashmapBucket{}) + uintptr(m.keySize)*8
219+
slotValueOffset := unsafe.Sizeof(hashmapBucket{}) + m.keySize*8
220220
slotValue := unsafe.Pointer(uintptr(bucketBuf) + slotValueOffset)
221221
m.count++
222-
memcpy(slotKey, key, uintptr(m.keySize))
223-
memcpy(slotValue, value, uintptr(m.valueSize))
222+
memcpy(slotKey, key, m.keySize)
223+
memcpy(slotValue, value, m.valueSize)
224224
bucket := (*hashmapBucket)(bucketBuf)
225225
bucket.tophash[0] = tophash
226226
return bucket
@@ -235,14 +235,14 @@ func hashmapGrow(m *hashmap) {
235235
// allocate our new buckets twice as big
236236
n.bucketBits = m.bucketBits + 1
237237
numBuckets := uintptr(1) << n.bucketBits
238-
bucketBufSize := unsafe.Sizeof(hashmapBucket{}) + uintptr(m.keySize)*8 + uintptr(m.valueSize)*8
238+
bucketBufSize := unsafe.Sizeof(hashmapBucket{}) + m.keySize*8 + m.valueSize*8
239239
n.buckets = alloc(bucketBufSize*numBuckets, nil)
240240

241241
// use a hashmap iterator to go through the old map
242242
var it hashmapIterator
243243

244-
var key = alloc(uintptr(m.keySize), nil)
245-
var value = alloc(uintptr(m.valueSize), nil)
244+
var key = alloc(m.keySize, nil)
245+
var value = alloc(m.valueSize, nil)
246246

247247
for hashmapNext(m, &it, key, value) {
248248
h := n.keyHash(key, uintptr(n.keySize), n.seed)
@@ -265,7 +265,7 @@ func hashmapGet(m *hashmap, key, value unsafe.Pointer, valueSize uintptr, hash u
265265
}
266266
numBuckets := uintptr(1) << m.bucketBits
267267
bucketNumber := (uintptr(hash) & (numBuckets - 1))
268-
bucketSize := unsafe.Sizeof(hashmapBucket{}) + uintptr(m.keySize)*8 + uintptr(m.valueSize)*8
268+
bucketSize := unsafe.Sizeof(hashmapBucket{}) + m.keySize*8 + m.valueSize*8
269269
bucketAddr := uintptr(m.buckets) + bucketSize*bucketNumber
270270
bucket := (*hashmapBucket)(unsafe.Pointer(bucketAddr))
271271

@@ -278,15 +278,15 @@ func hashmapGet(m *hashmap, key, value unsafe.Pointer, valueSize uintptr, hash u
278278
// Try to find the key.
279279
for bucket != nil {
280280
for i := uintptr(0); i < 8; i++ {
281-
slotKeyOffset := unsafe.Sizeof(hashmapBucket{}) + uintptr(m.keySize)*uintptr(i)
281+
slotKeyOffset := unsafe.Sizeof(hashmapBucket{}) + m.keySize*uintptr(i)
282282
slotKey := unsafe.Pointer(uintptr(unsafe.Pointer(bucket)) + slotKeyOffset)
283-
slotValueOffset := unsafe.Sizeof(hashmapBucket{}) + uintptr(m.keySize)*8 + uintptr(m.valueSize)*uintptr(i)
283+
slotValueOffset := unsafe.Sizeof(hashmapBucket{}) + m.keySize*8 + m.valueSize*uintptr(i)
284284
slotValue := unsafe.Pointer(uintptr(unsafe.Pointer(bucket)) + slotValueOffset)
285285
if bucket.tophash[i] == tophash {
286286
// This could be the key we're looking for.
287-
if m.keyEqual(key, slotKey, uintptr(m.keySize)) {
287+
if m.keyEqual(key, slotKey, m.keySize) {
288288
// Found the key, copy it.
289-
memcpy(value, slotValue, uintptr(m.valueSize))
289+
memcpy(value, slotValue, m.valueSize)
290290
return true
291291
}
292292
}
@@ -295,7 +295,7 @@ func hashmapGet(m *hashmap, key, value unsafe.Pointer, valueSize uintptr, hash u
295295
}
296296

297297
// Did not find the key.
298-
memzero(value, uintptr(m.valueSize))
298+
memzero(value, m.valueSize)
299299
return false
300300
}
301301

@@ -312,7 +312,7 @@ func hashmapDelete(m *hashmap, key unsafe.Pointer, hash uint32) {
312312
}
313313
numBuckets := uintptr(1) << m.bucketBits
314314
bucketNumber := (uintptr(hash) & (numBuckets - 1))
315-
bucketSize := unsafe.Sizeof(hashmapBucket{}) + uintptr(m.keySize)*8 + uintptr(m.valueSize)*8
315+
bucketSize := unsafe.Sizeof(hashmapBucket{}) + m.keySize*8 + m.valueSize*8
316316
bucketAddr := uintptr(m.buckets) + bucketSize*bucketNumber
317317
bucket := (*hashmapBucket)(unsafe.Pointer(bucketAddr))
318318

@@ -325,11 +325,11 @@ func hashmapDelete(m *hashmap, key unsafe.Pointer, hash uint32) {
325325
// Try to find the key.
326326
for bucket != nil {
327327
for i := uintptr(0); i < 8; i++ {
328-
slotKeyOffset := unsafe.Sizeof(hashmapBucket{}) + uintptr(m.keySize)*uintptr(i)
328+
slotKeyOffset := unsafe.Sizeof(hashmapBucket{}) + m.keySize*uintptr(i)
329329
slotKey := unsafe.Pointer(uintptr(unsafe.Pointer(bucket)) + slotKeyOffset)
330330
if bucket.tophash[i] == tophash {
331331
// This could be the key we're looking for.
332-
if m.keyEqual(key, slotKey, uintptr(m.keySize)) {
332+
if m.keyEqual(key, slotKey, m.keySize) {
333333
// Found the key, delete it.
334334
bucket.tophash[i] = 0
335335
m.count--
@@ -367,7 +367,7 @@ func hashmapNext(m *hashmap, it *hashmapIterator, key, value unsafe.Pointer) boo
367367
// went through all buckets
368368
return false
369369
}
370-
bucketSize := unsafe.Sizeof(hashmapBucket{}) + uintptr(m.keySize)*8 + uintptr(m.valueSize)*8
370+
bucketSize := unsafe.Sizeof(hashmapBucket{}) + m.keySize*8 + m.valueSize*8
371371
bucketAddr := uintptr(it.buckets) + bucketSize*it.bucketNumber
372372
it.bucket = (*hashmapBucket)(unsafe.Pointer(bucketAddr))
373373
it.bucketNumber++ // next bucket
@@ -379,24 +379,24 @@ func hashmapNext(m *hashmap, it *hashmapIterator, key, value unsafe.Pointer) boo
379379
}
380380

381381
bucketAddr := uintptr(unsafe.Pointer(it.bucket))
382-
slotKeyOffset := unsafe.Sizeof(hashmapBucket{}) + uintptr(m.keySize)*uintptr(it.bucketIndex)
382+
slotKeyOffset := unsafe.Sizeof(hashmapBucket{}) + m.keySize*uintptr(it.bucketIndex)
383383
slotKey := unsafe.Pointer(bucketAddr + slotKeyOffset)
384-
memcpy(key, slotKey, uintptr(m.keySize))
384+
memcpy(key, slotKey, m.keySize)
385385

386386
if it.buckets == m.buckets {
387387
// Our view of the buckets is the same as the parent map.
388388
// Just copy the value we have
389-
slotValueOffset := unsafe.Sizeof(hashmapBucket{}) + uintptr(m.keySize)*8 + uintptr(m.valueSize)*uintptr(it.bucketIndex)
389+
slotValueOffset := unsafe.Sizeof(hashmapBucket{}) + m.keySize*8 + m.valueSize*uintptr(it.bucketIndex)
390390
slotValue := unsafe.Pointer(bucketAddr + slotValueOffset)
391-
memcpy(value, slotValue, uintptr(m.valueSize))
391+
memcpy(value, slotValue, m.valueSize)
392392
it.bucketIndex++
393393
} else {
394394
it.bucketIndex++
395395

396396
// Our view of the buckets doesn't match the parent map.
397397
// Look up the key in the new buckets and return that value if it exists
398-
hash := m.keyHash(key, uintptr(m.keySize), m.seed)
399-
ok := hashmapGet(m, key, value, uintptr(m.valueSize), hash)
398+
hash := m.keyHash(key, m.keySize, m.seed)
399+
ok := hashmapGet(m, key, value, m.valueSize, hash)
400400
if !ok {
401401
// doesn't exist in parent map; try next key
402402
continue
@@ -414,7 +414,7 @@ func hashmapBinarySet(m *hashmap, key, value unsafe.Pointer) {
414414
if m == nil {
415415
nilMapPanic()
416416
}
417-
hash := hash32(key, uintptr(m.keySize), m.seed)
417+
hash := hash32(key, m.keySize, m.seed)
418418
hashmapSet(m, key, value, hash)
419419
}
420420

@@ -423,15 +423,15 @@ func hashmapBinaryGet(m *hashmap, key, value unsafe.Pointer, valueSize uintptr)
423423
memzero(value, uintptr(valueSize))
424424
return false
425425
}
426-
hash := hash32(key, uintptr(m.keySize), m.seed)
426+
hash := hash32(key, m.keySize, m.seed)
427427
return hashmapGet(m, key, value, valueSize, hash)
428428
}
429429

430430
func hashmapBinaryDelete(m *hashmap, key unsafe.Pointer) {
431431
if m == nil {
432432
return
433433
}
434-
hash := hash32(key, uintptr(m.keySize), m.seed)
434+
hash := hash32(key, m.keySize, m.seed)
435435
hashmapDelete(m, key, hash)
436436
}
437437

0 commit comments

Comments
 (0)