Repository navigation
Run the server on uvloop, and stop scanning every actor to find the one sending (CHOO-3667) - #1223
Closed
abeldantas wants to merge 1 commit into
Closed
abeldantas wants to merge 1 commit into
abeldantas wants to merge 1 commit into
Conversation
…ne sending (CHOO-3667) A CPU profile of the pods under message load put the event loop's own work (scheduling, socket reads and writes) at about 15% of a pod's CPU, and finding the sending agent's actor, a scan of every client's actor on every send, at about 3%. The server now runs on uvloop where it is installed (not Windows, where it keeps asyncio's own loop), keeps the client found for each agent and checks it again on each use, and caches compiled mention patterns per name instead of relying on re's 512-pattern cache.
abeldantas
requested review from
agent-runner353,
amaudruz,
christian-mcdermott,
lbangalosbt,
massimo-sandboxaq,
niall-sandboxaq,
petr-sandbox,
raullopez-sandbox and
simonflk-sb
as code owners
October 31, 2026 13:40
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
A moderate memory-leak issue remains because stale agent mappings are not removed during client cleanup, alongside two smaller consistency and naming issues that should be addressed.
3 open issues
What changed in this PR
This pull request reduces server CPU usage by using uvloop where available, caching agent-to-client lookups, and caching compiled mention patterns.
Changes:
- Add conditional
uvloopsupport with an asyncio fallback on Windows or when unavailable. - Cache validated agent-to-client mappings to avoid scanning every client on each send.
- Cache compiled mention patterns and add targeted tests.
- Update dependencies, lockfiles, and the changelog.
| File | Summary |
|---|---|
core/uv.lock |
Locks the conditional uvloop dependency and platform-specific artifacts. |
core/tests/switch_core/test_event_loop_choice.py |
Tests event-loop selection and fallback behavior. Nit: Rename the test containing the asyncios typo. |
core/tests/switch_core/clients/test_client_lifecycle_agent_lookup.py |
Tests cached lookups, actor replacement, movement, and removal behavior. |
core/switch_core/main.py |
Selects uvloop or asyncio’s default event loop. Nit: Move the guarded optional import to module scope. |
core/switch_core/clients/mentions.py |
Caches compiled mention patterns per name. |
core/switch_core/clients/client_lifecycle_service.py |
Adds validated agent-to-client lookup caching. Moderate: Remove stale cache entries whenever clients stop or crash, and clear the cache in stop_all, to prevent unbounded growth. |
core/pyproject.toml |
Declares uvloop for non-Windows installations. |
CHANGELOG.md |
Documents the CPU and lookup optimizations. |
Review effort: Lite
Comment on lines
+111
to
+112
| # Agent id to the id of its client, for `get_by_agent_id`. | ||
| self._client_of_agent: dict[str, str] = {} |
Comment on lines
+2314
to
+2316
| try: | ||
| import uvloop | ||
| except ImportError: |
| assert main._event_loop_factory() is uvloop.new_event_loop | ||
|
|
||
|
|
||
| def test_asyncios_own_loop_where_it_is_not(monkeypatch: pytest.MonkeyPatch) -> None: |
This was referenced Oct 31, 2026
Collaborator
Author
|
Combined into #1307 with the other scaling PRs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


A CPU profile of the pods under message load put the event loop's own work (scheduling, socket reads and writes) at about 15% of a pod's CPU, and finding the sending agent's actor, a scan of every client's actor on every send, at about 3%.
In this PR we:
rekeeps only 512