internal/aggregate: optimize path registry for read-heavy workloads
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: Ibc477830b471fe09838b7477fe73ffa56a6a6964
This commit is contained in:
parent
ffb4ab2295
commit
bb56df6423
1 changed files with 13 additions and 4 deletions
|
|
@ -26,10 +26,19 @@ func NewPathRegistry(maxPaths int) *PathRegistry {
|
||||||
// Returns true if the path was accepted: either already existed or was added,
|
// Returns true if the path was accepted: either already existed or was added,
|
||||||
// false if rejected due to reaching the limit.
|
// false if rejected due to reaching the limit.
|
||||||
func (r *PathRegistry) Add(path string) bool {
|
func (r *PathRegistry) Add(path string) bool {
|
||||||
|
// Fast path: check with read lock first
|
||||||
|
r.mu.RLock()
|
||||||
|
if _, exists := r.paths[path]; exists {
|
||||||
|
r.mu.RUnlock()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
r.mu.RUnlock()
|
||||||
|
|
||||||
|
// Slow path: acquire write lock to add
|
||||||
r.mu.Lock()
|
r.mu.Lock()
|
||||||
defer r.mu.Unlock()
|
defer r.mu.Unlock()
|
||||||
|
|
||||||
// If path already exists, accept it
|
// Double-check after acquiring write lock
|
||||||
if _, exists := r.paths[path]; exists {
|
if _, exists := r.paths[path]; exists {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
@ -40,7 +49,7 @@ func (r *PathRegistry) Add(path string) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Limit reached - reject and increment overflow
|
// Limit reached, reject and increment overflow
|
||||||
r.overflowCount++
|
r.overflowCount++
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
@ -62,8 +71,8 @@ func (r *PathRegistry) Count() int {
|
||||||
return len(r.paths)
|
return len(r.paths)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the number of paths that were rejected
|
// Returns the number of paths that were rejected due to the registry being at
|
||||||
// due to the registry being at capacity.
|
// capacity.
|
||||||
func (r *PathRegistry) OverflowCount() int {
|
func (r *PathRegistry) OverflowCount() int {
|
||||||
r.mu.RLock()
|
r.mu.RLock()
|
||||||
defer r.mu.RUnlock()
|
defer r.mu.RUnlock()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue