Skip to content

performance: optimize per-service memory allocations - #1695

Merged
howardjohn merged 1 commit into
agentgateway:mainfrom
howardjohn:perf/lb-alloc
Apr 30, 2026
Merged

howardjohn merged 1 commit into
agentgateway:mainfrom
howardjohn:perf/lb-alloc

Conversation

@howardjohn

@howardjohn howardjohn commented Apr 29, 2026 •

Copy link
Copy Markdown
Collaborator

Before we allocate

New: 320 bytes per service (87% reduction)

5k services before/after:
2026-04-29_14-56-15

Signed-off-by: John Howard john.howard@solo.io

Before we allocate
* 1312 bytes for eviction mpsc. We replace this with a lighter mpsc. see
  hyperium/hyper#4057 for more context
* 512 bytes for another part of the same mpsc; remoevd
* 512 bytes for the tokio task; we now build it lazily

New: 320 bytes per service (87% reduction)

Signed-off-by: John Howard <john.howard@solo.io>
@howardjohn
howardjohn marked this pull request as ready for review April 29, 2026 22:47
@howardjohn
howardjohn requested a review from a team as a code owner April 29, 2026 22:47
Copilot AI review requested due to automatic review settings April 29, 2026 22:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR reduces per-service memory overhead in the load balancer by replacing the eviction event channel implementation and making the eviction worker task start lazily on first eviction, instead of eagerly at EndpointSet construction.

Changes:

  • Replaced the eviction event channel from tokio::sync::mpsc to futures::channel::mpsc with a smaller buffer.
  • Introduced EvictionWorkerState and EvictionStarter to lazily spawn the eviction worker only when needed.
  • Updated eviction scheduling to ensure the worker is started before sending eviction events, and simplified maybe_sleep_until by using pending() when no timer is needed.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/agentgateway/src/types/loadbalancer.rs

@stevenctl stevenctl left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

lgtm, for my own knowledge when is it appropriate to use one mpsc impl or the other?

@howardjohn

Copy link
Copy Markdown
Collaborator Author

lgtm, for my own knowledge when is it appropriate to use one mpsc impl or the other?

Before yesterday I thought: always use tokio. But tokio has a 32xsizeof<T> cost so I suppose whenever T is large + the number of MPSC is large? The downside is that the futures one is not an intrusive linked list so it does an additional heap alloc per message; in this case we do not care about throughput though as eviction is rare.

@howardjohn
howardjohn merged commit 29bedcc into agentgateway:main Apr 30, 2026
14 checks passed
alan-bentley-pgh pushed a commit to solo-io/agentgateway-new-ui that referenced this pull request May 19, 2026
Before we allocate
* 1312 bytes for eviction mpsc. We replace this with a lighter mpsc. see
  hyperium/hyper#4057 for more context
* 512 bytes for another part of the same mpsc; remoevd
* 512 bytes for the tokio task; we now build it lazily

New: 320 bytes per service (87% reduction)

5k services before/after: 
<img width="1885" height="760" alt="2026-04-29_14-56-15"
src="https://github.com/user-attachments/assets/4cf1c233-507c-4c76-9778-9f7fb9cf8248"
/>


Signed-off-by: John Howard <john.howard@solo.io>

Signed-off-by: John Howard <john.howard@solo.io>
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.

3 participants