Skip to content

client: enforce_hint_cap does two full map scans per pass under lock; make it a single pass #625

Description

@SebastianThiebaud

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    refactorImprovements to structure, readability, or maintainability while preserving external behaviortech debtCleanup or hardening work that addresses shortcuts or deferred engineering quality issues

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions