Files
bl/common/utils/concurrent-swiss-map
昔念 d74652373c ```
refactor(socket): 更新广播和退出逻辑中的类型引用

更新socket服务器中广播功能和退出功能的代码,
将player.Player类型替换为player.ClientData类型,
并相应调整方法调用以适应新的数据结构。

feat(map): 添加LoadOrStore方法支持

在并发安全的swiss map中新增LoadOrStore方法,
提供原子性的加载或存储功能,增强map的操作能力。

refactor(login): 优化登录逻辑中的玩家获取方式

重构登录控制器中获取玩家对象的方式,
直接从
2026-03-16 22:30:12 +08:00
..
```
2026-03-16 22:30:12 +08:00

Concurrent Swiss Map GoDoc Build Status Coverage Status Go Report Card

Concurrent Swiss Map is an open-source Go library that provides a high-performance, thread-safe generic concurrent hash map implementation designed to handle concurrent access efficiently. It's built with a focus on simplicity, speed, and reliability, making it a solid choice for scenarios where concurrent access to a hash map is crucial.

Uses dolthub/swiss map implementation under the hood.

Installation

Supports 1.18+ Go versions because of Go Generics

go get github.com/mhmtszr/concurrent-swiss-map

Usage

New functions will be added soon...

package main

import (
	"hash/fnv"

	csmap "github.com/mhmtszr/concurrent-swiss-map"
)

func main() {
	myMap := csmap.New[string, int](
		// set the number of map shards. the default value is 32.
		csmap.WithShardCount[string, int](32),

		// if don't set custom hasher, use the built-in maphash.
		csmap.WithCustomHasher[string, int](func(key string) uint64 {
			hash := fnv.New64a()
			hash.Write([]byte(key))
			return hash.Sum64()
		}),

		// set the total capacity, every shard map has total capacity/shard count capacity. the default value is 0.
		csmap.WithSize[string, int](1000),
	)

	key := "swiss-map"
	myMap.Store(key, 10)

	val, ok := myMap.Load(key)
	println("load val:", val, "exists:", ok)

	deleted := myMap.Delete(key)
	println("deleted:", deleted)

	ok = myMap.Has(key)
	println("has:", ok)

	empty := myMap.IsEmpty()
	println("empty:", empty)

	myMap.SetIfAbsent(key, 11)

	myMap.Range(func(key string, value int) (stop bool) {
		println("range:", key, value)
		return true
	})

	count := myMap.Count()
	println("count:", count)

	// Output:
	// load val: 10 exists: true
	// deleted: true
	// has: false
	// empty: true
	// range: swiss-map 11
	// count: 1
}

Basic Architecture

img.png

Benchmark Test

Benchmark was made on:

  • Apple M1 Max
  • 32 GB memory

Benchmark test results can be obtained by running this file on local computers.

benchmark.png

Benchmark Results

  • Memory usage of the concurrent swiss map is better than other map implementations in all checked test scenarios.
  • In high concurrent systems, the concurrent swiss map is faster, but in systems containing few concurrent operations, it works similarly to RWMutexMap.