ChannelPool::enforce_hint_cap is written as an O((victims) x n) loop that recomputes non_configured via a full keys().filter().count() and re-scans the whole map for the min_by_key victim on every iteration, all under the synchronous channels mutex. Because the sweep fires on each insert it almost always evicts at most one entry, so the loop body runs once — but it still pays two full scans per pass inside the lock critical section, and the adversarial hint-churn workload the cap defends against (#341) is exactly what drives frequent inserts and thus frequent sweeps. This is avoidable lock-hold-time amplification.
Scope
crates/tsoracle-client/src/channel_pool.rs:510-540 — rewrite enforce_hint_cap to a single pass: compute non_configured once; if it exceeds MAX_HINT_CHANNELS, collect the non-configured, non-protected (endpoint, last_used) slots and select_nth_unstable to evict exactly non_configured - MAX_HINT_CHANNELS victims. No loop, one scan.
Exit criteria
cargo test -p tsoracle-client passes unchanged (the cap-enforcement and LRU-victim-selection tests still green).
enforce_hint_cap performs a single scan of the map per invocation.
- The lock is still never held across an
.await (unchanged; it already isn't).
Explicit non-goal
Changing the cap value or the LRU eviction policy — this is a complexity/lock-hold reduction that preserves which entry gets evicted.
Parent:
ChannelPool::enforce_hint_capis written as an O((victims) x n) loop that recomputesnon_configuredvia a fullkeys().filter().count()and re-scans the whole map for themin_by_keyvictim on every iteration, all under the synchronouschannelsmutex. Because the sweep fires on each insert it almost always evicts at most one entry, so the loop body runs once — but it still pays two full scans per pass inside the lock critical section, and the adversarial hint-churn workload the cap defends against (#341) is exactly what drives frequent inserts and thus frequent sweeps. This is avoidable lock-hold-time amplification.Scope
crates/tsoracle-client/src/channel_pool.rs:510-540— rewriteenforce_hint_capto a single pass: computenon_configuredonce; if it exceedsMAX_HINT_CHANNELS, collect the non-configured, non-protected(endpoint, last_used)slots andselect_nth_unstableto evict exactlynon_configured - MAX_HINT_CHANNELSvictims. No loop, one scan.Exit criteria
cargo test -p tsoracle-clientpasses unchanged (the cap-enforcement and LRU-victim-selection tests still green).enforce_hint_capperforms a single scan of the map per invocation..await(unchanged; it already isn't).Explicit non-goal
Changing the cap value or the LRU eviction policy — this is a complexity/lock-hold reduction that preserves which entry gets evicted.
Parent: