Skip to content

Commit cd1b479

Browse files
author
juju
committed
新增设置shard大小的方法
1 parent 1ae14ad commit cd1b479

3 files changed

Lines changed: 38 additions & 37 deletions

File tree

syncmapx_bench_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func (i Integer) String() string {
1313
}
1414

1515
func BenchmarkItems(b *testing.B) {
16-
m := New[Animal]()
16+
m := New[Animal](1)
1717

1818
// Insert 100 elements.
1919
for i := 0; i < 10000; i++ {
@@ -25,7 +25,7 @@ func BenchmarkItems(b *testing.B) {
2525
}
2626

2727
func BenchmarkItemsInteger(b *testing.B) {
28-
m := NewStringer[Integer, Animal]()
28+
m := NewStringer[Integer, Animal](1)
2929

3030
// Insert 100 elements.
3131
for i := 0; i < 10000; i++ {
@@ -52,7 +52,7 @@ func BenchmarkItemsInt(b *testing.B) {
5252
}
5353

5454
func BenchmarkMarshalJson(b *testing.B) {
55-
m := New[Animal]()
55+
m := New[Animal](1)
5656

5757
// Insert 100 elements.
5858
for i := 0; i < 10000; i++ {
@@ -73,7 +73,7 @@ func BenchmarkStrconv(b *testing.B) {
7373
}
7474

7575
func BenchmarkSingleInsertAbsent(b *testing.B) {
76-
m := New[string]()
76+
m := New[string](1)
7777
b.ResetTimer()
7878
for i := 0; i < b.N; i++ {
7979
m.Set(strconv.Itoa(i), "value")
@@ -89,7 +89,7 @@ func BenchmarkSingleInsertAbsentSyncMap(b *testing.B) {
8989
}
9090

9191
func BenchmarkSingleInsertPresent(b *testing.B) {
92-
m := New[string]()
92+
m := New[string](1)
9393
m.Set("key", "value")
9494
b.ResetTimer()
9595
for i := 0; i < b.N; i++ {
@@ -107,7 +107,7 @@ func BenchmarkSingleInsertPresentSyncMap(b *testing.B) {
107107
}
108108

109109
func benchmarkMultiInsertDifferent(b *testing.B) {
110-
m := New[string]()
110+
m := New[string](1)
111111
finished := make(chan struct{}, b.N)
112112
_, set := GetSet(m, finished)
113113
b.ResetTimer()
@@ -147,7 +147,7 @@ func BenchmarkMultiInsertDifferent_256_Shard(b *testing.B) {
147147
}
148148

149149
func BenchmarkMultiInsertSame(b *testing.B) {
150-
m := New[string]()
150+
m := New[string](1)
151151
finished := make(chan struct{}, b.N)
152152
_, set := GetSet(m, finished)
153153
m.Set("key", "value")
@@ -175,7 +175,7 @@ func BenchmarkMultiInsertSameSyncMap(b *testing.B) {
175175
}
176176

177177
func BenchmarkMultiGetSame(b *testing.B) {
178-
m := New[string]()
178+
m := New[string](1)
179179
finished := make(chan struct{}, b.N)
180180
get, _ := GetSet(m, finished)
181181
m.Set("key", "value")
@@ -203,7 +203,7 @@ func BenchmarkMultiGetSameSyncMap(b *testing.B) {
203203
}
204204

205205
func benchmarkMultiGetSetDifferent(b *testing.B) {
206-
m := New[string]()
206+
m := New[string](12)
207207
finished := make(chan struct{}, 2*b.N)
208208
get, set := GetSet(m, finished)
209209
m.Set("-1", "value")
@@ -246,7 +246,7 @@ func BenchmarkMultiGetSetDifferent_256_Shard(b *testing.B) {
246246
}
247247

248248
func benchmarkMultiGetSetBlock(b *testing.B) {
249-
m := New[string]()
249+
m := New[string](1)
250250
finished := make(chan struct{}, 2*b.N)
251251
get, set := GetSet(m, finished)
252252
for i := 0; i < b.N; i++ {
@@ -330,7 +330,7 @@ func runWithShards(bench func(b *testing.B), b *testing.B, shardsCount int) {
330330
}
331331

332332
func BenchmarkKeys(b *testing.B) {
333-
m := New[Animal]()
333+
m := New[Animal](1)
334334

335335
// Insert 100 elements.
336336
for i := 0; i < 10000; i++ {

syncmapx_test.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ type Animal struct {
1313
}
1414

1515
func TestMapCreation(t *testing.T) {
16-
m := New[string]()
16+
m := New[string](1)
1717

1818
if m.Count() != 0 {
1919
t.Error("new map should be empty.")
2020
}
2121
}
2222

2323
func TestInsert(t *testing.T) {
24-
m := New[Animal]()
24+
m := New[Animal](1)
2525
elephant := Animal{"elephant"}
2626
monkey := Animal{"monkey"}
2727

@@ -34,7 +34,7 @@ func TestInsert(t *testing.T) {
3434
}
3535

3636
func TestInsertAbsent(t *testing.T) {
37-
m := New[Animal]()
37+
m := New[Animal](1)
3838
elephant := Animal{"elephant"}
3939
monkey := Animal{"monkey"}
4040

@@ -45,7 +45,7 @@ func TestInsertAbsent(t *testing.T) {
4545
}
4646

4747
func TestGet(t *testing.T) {
48-
m := New[Animal]()
48+
m := New[Animal](1)
4949

5050
// Get a missing element.
5151
val, ok := m.Get("Money")
@@ -73,7 +73,7 @@ func TestGet(t *testing.T) {
7373
}
7474

7575
func TestHas(t *testing.T) {
76-
m := New[Animal]()
76+
m := New[Animal](1)
7777

7878
// Get a missing element.
7979
if m.Has("Money") == true {
@@ -89,7 +89,7 @@ func TestHas(t *testing.T) {
8989
}
9090

9191
func TestRemove(t *testing.T) {
92-
m := New[Animal]()
92+
m := New[Animal](1)
9393

9494
monkey := Animal{"monkey"}
9595
m.Set("monkey", monkey)
@@ -115,7 +115,7 @@ func TestRemove(t *testing.T) {
115115
}
116116

117117
func TestRemoveCb(t *testing.T) {
118-
m := New[Animal]()
118+
m := New[Animal](1)
119119

120120
monkey := Animal{"monkey"}
121121
m.Set("monkey", monkey)
@@ -203,7 +203,7 @@ func TestRemoveCb(t *testing.T) {
203203
}
204204

205205
func TestPop(t *testing.T) {
206-
m := New[Animal]()
206+
m := New[Animal](1)
207207

208208
monkey := Animal{"monkey"}
209209
m.Set("monkey", monkey)
@@ -236,7 +236,7 @@ func TestPop(t *testing.T) {
236236
}
237237

238238
func TestCount(t *testing.T) {
239-
m := New[Animal]()
239+
m := New[Animal](1)
240240
for i := 0; i < 100; i++ {
241241
m.Set(strconv.Itoa(i), Animal{strconv.Itoa(i)})
242242
}
@@ -247,7 +247,7 @@ func TestCount(t *testing.T) {
247247
}
248248

249249
func TestIsEmpty(t *testing.T) {
250-
m := New[Animal]()
250+
m := New[Animal](1)
251251

252252
if m.IsEmpty() == false {
253253
t.Error("new map should be empty")
@@ -261,7 +261,7 @@ func TestIsEmpty(t *testing.T) {
261261
}
262262

263263
func TestIterator(t *testing.T) {
264-
m := New[Animal]()
264+
m := New[Animal](1)
265265

266266
// Insert 100 elements.
267267
for i := 0; i < 100; i++ {
@@ -285,7 +285,7 @@ func TestIterator(t *testing.T) {
285285
}
286286

287287
func TestBufferedIterator(t *testing.T) {
288-
m := New[Animal]()
288+
m := New[Animal](1)
289289

290290
// Insert 100 elements.
291291
for i := 0; i < 100; i++ {
@@ -309,7 +309,7 @@ func TestBufferedIterator(t *testing.T) {
309309
}
310310

311311
func TestClear(t *testing.T) {
312-
m := New[Animal]()
312+
m := New[Animal](1)
313313

314314
// Insert 100 elements.
315315
for i := 0; i < 100; i++ {
@@ -324,7 +324,7 @@ func TestClear(t *testing.T) {
324324
}
325325

326326
func TestIterCb(t *testing.T) {
327-
m := New[Animal]()
327+
m := New[Animal](1)
328328

329329
// Insert 100 elements.
330330
for i := 0; i < 100; i++ {
@@ -342,7 +342,7 @@ func TestIterCb(t *testing.T) {
342342
}
343343

344344
func TestItems(t *testing.T) {
345-
m := New[Animal]()
345+
m := New[Animal](1)
346346

347347
// Insert 100 elements.
348348
for i := 0; i < 100; i++ {
@@ -357,7 +357,7 @@ func TestItems(t *testing.T) {
357357
}
358358

359359
func TestConcurrent(t *testing.T) {
360-
m := New[int]()
360+
m := New[int](1)
361361
ch := make(chan int)
362362
const iterations = 1000
363363
var a [iterations]int
@@ -421,7 +421,7 @@ func TestJsonMarshal(t *testing.T) {
421421
SHARD_COUNT = 32
422422
}()
423423
expected := "{\"a\":1,\"b\":2}"
424-
m := New[int]()
424+
m := New[int](1)
425425
m.Set("a", 1)
426426
m.Set("b", 2)
427427
j, err := json.Marshal(m)
@@ -436,7 +436,7 @@ func TestJsonMarshal(t *testing.T) {
436436
}
437437

438438
func TestKeys(t *testing.T) {
439-
m := New[Animal]()
439+
m := New[Animal](1)
440440

441441
// Insert 100 elements.
442442
for i := 0; i < 100; i++ {
@@ -454,7 +454,7 @@ func TestMInsert(t *testing.T) {
454454
"elephant": {"elephant"},
455455
"monkey": {"monkey"},
456456
}
457-
m := New[Animal]()
457+
m := New[Animal](1)
458458
m.MSet(animals)
459459

460460
if m.Count() != 2 {
@@ -487,7 +487,7 @@ func TestUpsert(t *testing.T) {
487487
return valueInMap
488488
}
489489

490-
m := New[Animal]()
490+
m := New[Animal](1)
491491
m.Set("marine", dolphin)
492492
m.Upsert("marine", whale, cb)
493493
m.Upsert("predator", tiger, cb)
@@ -509,7 +509,7 @@ func TestUpsert(t *testing.T) {
509509
}
510510

511511
func TestKeysWhenRemoving(t *testing.T) {
512-
m := New[Animal]()
512+
m := New[Animal](1)
513513

514514
// Insert 100 elements.
515515
Total := 100
@@ -533,7 +533,7 @@ func TestKeysWhenRemoving(t *testing.T) {
533533
}
534534

535535
func TestUnDrainedIter(t *testing.T) {
536-
m := New[Animal]()
536+
m := New[Animal](1)
537537
// Insert 100 elements.
538538
Total := 100
539539
for i := 0; i < Total; i++ {
@@ -585,7 +585,7 @@ func TestUnDrainedIter(t *testing.T) {
585585
}
586586

587587
func TestUnDrainedIterBuffered(t *testing.T) {
588-
m := New[Animal]()
588+
m := New[Animal](1)
589589
// Insert 100 elements.
590590
Total := 100
591591
for i := 0; i < Total; i++ {

synmapx.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ func create[K comparable, V any](fn32 func(key K) uint32) ConcurrentMap[K, V] {
3535
}
3636

3737
// Creates a new concurrent map.
38-
func New[V any]() ConcurrentMap[string, V] {
38+
func New[V any](count uint) ConcurrentMap[string, V] {
39+
SHARD_COUNT = int(count)
3940
return create[string, V](GetShardIndex)
4041
}
4142

4243
// Creates a new concurrent map.
43-
func NewStringer[K Stringer, V any]() ConcurrentMap[K, V] {
44-
44+
func NewStringer[K Stringer, V any](count uint) ConcurrentMap[K, V] {
45+
SHARD_COUNT = int(count)
4546
return create[K, V](strfnv32[K])
4647
}
4748

0 commit comments

Comments
 (0)