Refactor caching and memory management components
All checks were successful
Release Tag / release (push) Successful in 9s
All checks were successful
Release Tag / release (push) Successful in 9s
- 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.
This commit is contained in:
151
vfs/gc/gc.go
151
vfs/gc/gc.go
@@ -5,8 +5,7 @@ import (
|
||||
"context"
|
||||
"io"
|
||||
"s1d3sw1ped/steamcache2/vfs"
|
||||
"s1d3sw1ped/steamcache2/vfs/disk"
|
||||
"s1d3sw1ped/steamcache2/vfs/memory"
|
||||
"s1d3sw1ped/steamcache2/vfs/eviction"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
@@ -38,45 +37,14 @@ func New(wrappedVFS vfs.VFS, algorithm GCAlgorithm) *GCFS {
|
||||
algorithm: algorithm,
|
||||
}
|
||||
|
||||
switch algorithm {
|
||||
case LRU:
|
||||
gcfs.gcFunc = gcLRU
|
||||
case LFU:
|
||||
gcfs.gcFunc = gcLFU
|
||||
case FIFO:
|
||||
gcfs.gcFunc = gcFIFO
|
||||
case Largest:
|
||||
gcfs.gcFunc = gcLargest
|
||||
case Smallest:
|
||||
gcfs.gcFunc = gcSmallest
|
||||
case Hybrid:
|
||||
gcfs.gcFunc = gcHybrid
|
||||
default:
|
||||
// Default to LRU
|
||||
gcfs.gcFunc = gcLRU
|
||||
}
|
||||
gcfs.gcFunc = eviction.GetEvictionFunction(eviction.EvictionStrategy(algorithm))
|
||||
|
||||
return gcfs
|
||||
}
|
||||
|
||||
// GetGCAlgorithm returns the GC function for the given algorithm
|
||||
func GetGCAlgorithm(algorithm GCAlgorithm) func(vfs.VFS, uint) uint {
|
||||
switch algorithm {
|
||||
case LRU:
|
||||
return gcLRU
|
||||
case LFU:
|
||||
return gcLFU
|
||||
case FIFO:
|
||||
return gcFIFO
|
||||
case Largest:
|
||||
return gcLargest
|
||||
case Smallest:
|
||||
return gcSmallest
|
||||
case Hybrid:
|
||||
return gcHybrid
|
||||
default:
|
||||
return gcLRU
|
||||
}
|
||||
return eviction.GetEvictionFunction(eviction.EvictionStrategy(algorithm))
|
||||
}
|
||||
|
||||
// Create wraps the underlying Create method
|
||||
@@ -125,119 +93,6 @@ type EvictionStrategy interface {
|
||||
Evict(vfs vfs.VFS, bytesNeeded uint) uint
|
||||
}
|
||||
|
||||
// GC functions
|
||||
|
||||
// gcLRU implements Least Recently Used eviction
|
||||
func gcLRU(v vfs.VFS, bytesNeeded uint) uint {
|
||||
return evictLRU(v, bytesNeeded)
|
||||
}
|
||||
|
||||
// gcLFU implements Least Frequently Used eviction
|
||||
func gcLFU(v vfs.VFS, bytesNeeded uint) uint {
|
||||
return evictLFU(v, bytesNeeded)
|
||||
}
|
||||
|
||||
// gcFIFO implements First In First Out eviction
|
||||
func gcFIFO(v vfs.VFS, bytesNeeded uint) uint {
|
||||
return evictFIFO(v, bytesNeeded)
|
||||
}
|
||||
|
||||
// gcLargest implements largest file first eviction
|
||||
func gcLargest(v vfs.VFS, bytesNeeded uint) uint {
|
||||
return evictLargest(v, bytesNeeded)
|
||||
}
|
||||
|
||||
// gcSmallest implements smallest file first eviction
|
||||
func gcSmallest(v vfs.VFS, bytesNeeded uint) uint {
|
||||
return evictSmallest(v, bytesNeeded)
|
||||
}
|
||||
|
||||
// gcHybrid implements a hybrid eviction strategy
|
||||
func gcHybrid(v vfs.VFS, bytesNeeded uint) uint {
|
||||
return evictHybrid(v, bytesNeeded)
|
||||
}
|
||||
|
||||
// evictLRU performs LRU eviction by removing least recently used files
|
||||
func evictLRU(v vfs.VFS, bytesNeeded uint) uint {
|
||||
// Try to use specific eviction methods if available
|
||||
switch fs := v.(type) {
|
||||
case *memory.MemoryFS:
|
||||
return fs.EvictLRU(bytesNeeded)
|
||||
case *disk.DiskFS:
|
||||
return fs.EvictLRU(bytesNeeded)
|
||||
default:
|
||||
// No fallback - return 0 (no eviction performed)
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// evictLFU performs LFU (Least Frequently Used) eviction
|
||||
func evictLFU(v vfs.VFS, bytesNeeded uint) uint {
|
||||
// For now, fall back to size-based eviction
|
||||
// TODO: Implement proper LFU tracking
|
||||
return evictBySize(v, bytesNeeded)
|
||||
}
|
||||
|
||||
// evictFIFO performs FIFO (First In First Out) eviction
|
||||
func evictFIFO(v vfs.VFS, bytesNeeded uint) uint {
|
||||
switch fs := v.(type) {
|
||||
case *memory.MemoryFS:
|
||||
return fs.EvictFIFO(bytesNeeded)
|
||||
case *disk.DiskFS:
|
||||
return fs.EvictFIFO(bytesNeeded)
|
||||
default:
|
||||
// No fallback - return 0 (no eviction performed)
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// evictLargest evicts largest files first
|
||||
func evictLargest(v vfs.VFS, bytesNeeded uint) uint {
|
||||
return evictBySizeDesc(v, bytesNeeded)
|
||||
}
|
||||
|
||||
// evictSmallest evicts smallest files first
|
||||
func evictSmallest(v vfs.VFS, bytesNeeded uint) uint {
|
||||
return evictBySizeAsc(v, bytesNeeded)
|
||||
}
|
||||
|
||||
// evictBySize evicts files based on size (smallest first)
|
||||
func evictBySize(v vfs.VFS, bytesNeeded uint) uint {
|
||||
return evictBySizeAsc(v, bytesNeeded)
|
||||
}
|
||||
|
||||
// evictBySizeAsc evicts smallest files first
|
||||
func evictBySizeAsc(v vfs.VFS, bytesNeeded uint) uint {
|
||||
switch fs := v.(type) {
|
||||
case *memory.MemoryFS:
|
||||
return fs.EvictBySize(bytesNeeded, true) // true = ascending (smallest first)
|
||||
case *disk.DiskFS:
|
||||
return fs.EvictBySize(bytesNeeded, true) // true = ascending (smallest first)
|
||||
default:
|
||||
// No fallback - return 0 (no eviction performed)
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// evictBySizeDesc evicts largest files first
|
||||
func evictBySizeDesc(v vfs.VFS, bytesNeeded uint) uint {
|
||||
switch fs := v.(type) {
|
||||
case *memory.MemoryFS:
|
||||
return fs.EvictBySize(bytesNeeded, false) // false = descending (largest first)
|
||||
case *disk.DiskFS:
|
||||
return fs.EvictBySize(bytesNeeded, false) // false = descending (largest first)
|
||||
default:
|
||||
// No fallback - return 0 (no eviction performed)
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// evictHybrid implements a hybrid eviction strategy
|
||||
func evictHybrid(v vfs.VFS, bytesNeeded uint) uint {
|
||||
// Use LRU as primary strategy, but consider size as tiebreaker
|
||||
return evictLRU(v, bytesNeeded)
|
||||
}
|
||||
|
||||
// AdaptivePromotionDeciderFunc is a placeholder for the adaptive promotion logic
|
||||
var AdaptivePromotionDeciderFunc = func() interface{} {
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user