Skip to content

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
mainfrom
faster-event-loop
Closed

abeldantas wants to merge 1 commit into
mainfrom
faster-event-loop

Conversation

@abeldantas

Copy link
Copy Markdown
Collaborator

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:

  • run the server on uvloop where it is installed; Windows keeps asyncio's own loop
  • keep the client found for each agent, checked again on each use
  • cache compiled mention patterns per name, as re keeps only 512

…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.

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.

🟡 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 uvloop support 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 thread core/switch_core/main.py
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:
@abeldantas

Copy link
Copy Markdown
Collaborator Author

Combined into #1307 with the other scaling PRs.

@abeldantas abeldantas closed this Nov 2, 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.

2 participants