diff options
author | 2024-06-21 16:35:32 +0000 | |
---|---|---|
committer | 2024-06-21 17:35:32 +0100 | |
commit | 9143ac6fb4f68f1c79e42f7adc10e68bf7f36e87 (patch) | |
tree | 73514f164dd48cfc3f902e6b3f3d2a6db8c6175c /vendor/codeberg.org/gruf/go-mutexes/hash_map.go | |
parent | [chore] update go-structr and go-mangler to no longer rely on modern-go/refle... (diff) | |
download | gotosocial-9143ac6fb4f68f1c79e42f7adc10e68bf7f36e87.tar.xz |
updates go-mutexes to no longer rely on unsafe linkname (#3027)
Diffstat (limited to 'vendor/codeberg.org/gruf/go-mutexes/hash_map.go')
-rw-r--r-- | vendor/codeberg.org/gruf/go-mutexes/hash_map.go | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/vendor/codeberg.org/gruf/go-mutexes/hash_map.go b/vendor/codeberg.org/gruf/go-mutexes/hash_map.go new file mode 100644 index 000000000..a177133b5 --- /dev/null +++ b/vendor/codeberg.org/gruf/go-mutexes/hash_map.go @@ -0,0 +1,56 @@ +package mutexes + +type hashmap struct { + m map[string]*rwmutex + n int +} + +func (m *hashmap) init(cap int) { + m.m = make(map[string]*rwmutex, cap) + m.n = cap +} + +func (m *hashmap) Get(key string) *rwmutex { return m.m[key] } + +func (m *hashmap) Put(key string, mu *rwmutex) { + m.m[key] = mu + if n := len(m.m); n > m.n { + m.n = n + } +} + +func (m *hashmap) Delete(key string) { + delete(m.m, key) +} + +func (m *hashmap) Compact() { + // Noop when hashmap size + // is too small to matter. + if m.n < 2048 { + return + } + + // Difference between maximum map + // size and the current map size. + diff := m.n - len(m.m) + + // Maximum load factor before + // runtime allocates new hmap: + // maxLoad = 13 / 16 + // + // So we apply the inverse/2, once + // $maxLoad/2 % of hmap is empty we + // compact the map to drop buckets. + if 2*16*diff > m.n*13 { + + // Create new map only as big as required. + m2 := make(map[string]*rwmutex, len(m.m)) + for k, v := range m.m { + m2[k] = v + } + + // Set new. + m.m = m2 + m.n = len(m2) + } +} |