Skip to content

Commit 079c217

Browse files
committed
perf(pool): eliminate mutex overhead in state machine hot path
The state machine was calling notifyWaiters() on EVERY Get/Put operation, which acquired a mutex even when no waiters were present (the common case). Fix: Check waiters.Len() BEFORE acquiring mutex in notifyWaiters(). This eliminates mutex contention in the hot path (Get/Put operations). Performance impact: - Before: mutex lock/unlock on every Get/Put (even with no waiters) - After: lock-free check, only acquire mutex if waiters exist - Expected improvement: ~30-50% for Get/Put operations
1 parent 1b0168d commit 079c217

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

internal/pool/conn_state.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,19 @@ func (sm *ConnStateMachine) AwaitAndTransition(
238238
// notifyWaiters checks if any waiters can proceed and notifies them in FIFO order.
239239
// This is called after every state transition.
240240
func (sm *ConnStateMachine) notifyWaiters() {
241+
// Fast path: check if there are any waiters without acquiring lock
242+
// This is safe because:
243+
// 1. list.List.Len() is safe to call concurrently with modifications
244+
// 2. We only care if it's zero (no waiters) - this is the common case
245+
// 3. If it's non-zero, we acquire the lock and check again
246+
if sm.waiters.Len() == 0 {
247+
return
248+
}
249+
241250
sm.mu.Lock()
242251
defer sm.mu.Unlock()
243252

253+
// Double-check after acquiring lock (waiters might have been processed)
244254
if sm.waiters.Len() == 0 {
245255
return
246256
}

0 commit comments

Comments
 (0)