Skip to content

perf: avoid redundant scans in BalancedPool dispatcher selection#5146

Merged
metcoder95 merged 3 commits intonodejs:mainfrom
trivikr:avoid-balanced-pool-redundant-scans
May 1, 2026
Merged

perf: avoid redundant scans in BalancedPool dispatcher selection#5146
metcoder95 merged 3 commits intonodejs:mainfrom
trivikr:avoid-balanced-pool-redundant-scans

Conversation

@trivikr
Copy link
Copy Markdown
Member

@trivikr trivikr commented May 1, 2026

This relates to...

Performance cleanup in BalancedPool dispatcher selection.

Rationale

BalancedPool[kGetDispatcher] previously scanned the client list multiple times per dispatch: once to find an available dispatcher, again to check whether all clients were busy, then again to find a non-draining fallback index before entering the weighted selection loop.

The busy check was redundant after an available dispatcher had already been found, and the fallback selection can be tracked during the weighted loop.

Changes

Collapse dispatcher availability checks and fallback max-weight tracking into the existing weighted round-robin loop.

This keeps per-dispatch complexity at O(n) but removes up to three extra client-array scans. The improvement should be measurable with a targeted kGetDispatcher() microbenchmark, especially as upstream count grows; it may be hard to see in network-heavy benchmarks.

Features

N/A

Bug Fixes

N/A

Breaking Changes and Deprecations

N/A

Status

Assisted-by: openai:gpt-5.5

Assisted-by: openai:gpt-5.5
Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com>
Comment thread lib/dispatcher/balanced-pool.js Outdated
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 1, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.21%. Comparing base (96fd5e9) to head (dca2afc).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5146   +/-   ##
=======================================
  Coverage   93.21%   93.21%           
=======================================
  Files         110      110           
  Lines       36247    36245    -2     
=======================================
  Hits        33787    33787           
+ Misses       2460     2458    -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@metcoder95 metcoder95 merged commit 7af90e9 into nodejs:main May 1, 2026
35 checks passed
@trivikr trivikr deleted the avoid-balanced-pool-redundant-scans branch May 1, 2026 14:24
@github-actions github-actions Bot mentioned this pull request May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants