Files
steamcache2/vfs/locks/sharding.go
Justin Harms bfe29dea75
All checks were successful
Release Tag / release (push) Successful in 9s
Refactor caching and memory management components
- Updated the caching logic to utilize a predictive cache warmer, enhancing content prefetching based on access patterns.
- Replaced the legacy warming system with a more efficient predictive approach, allowing for better performance and resource management.
- Refactored memory management to integrate dynamic cache size adjustments based on system memory usage, improving overall efficiency.
- Simplified the VFS interface and improved concurrency handling with sharded locks for better performance in multi-threaded environments.
- Enhanced tests to validate the new caching and memory management behaviors, ensuring reliability and performance improvements.
2025-09-22 01:59:15 -05:00

29 lines
715 B
Go

package locks
import (
"sync"
)
// Number of lock shards for reducing contention
const NumLockShards = 32
// GetShardIndex returns the shard index for a given key using FNV-1a hash
func GetShardIndex(key string) int {
// Use FNV-1a hash for good distribution
var h uint32 = 2166136261 // FNV offset basis
for i := 0; i < len(key); i++ {
h ^= uint32(key[i])
h *= 16777619 // FNV prime
}
return int(h % NumLockShards)
}
// GetKeyLock returns a lock for the given key using sharding
func GetKeyLock(keyLocks []sync.Map, key string) *sync.RWMutex {
shardIndex := GetShardIndex(key)
shard := &keyLocks[shardIndex]
keyLock, _ := shard.LoadOrStore(key, &sync.RWMutex{})
return keyLock.(*sync.RWMutex)
}