Skip to content

Build an agent's event read once per shape, not on every read (CHOO-3667) - #1254

Closed
abeldantas wants to merge 1 commit into
one-stream-bellfrom
read-shapes-once
Closed

abeldantas wants to merge 1 commit into
one-stream-bellfrom
read-shapes-once

Conversation

@abeldantas

Copy link
Copy Markdown
Collaborator

A stream reads an agent's own events for every event it delivers, and in a CPU profile of a load test building that statement, with the check of the rooms dropped after the agent's cursor, was the largest of what the server spent building statements.

In this PR each of the read's sixteen shapes (rooms or not, notifiable only or not, a bound or not, a limit or not) is built once and run with bind parameters (_own_events_read), and so is the check (_ROOMS_DROPPED_AFTER).

Together with #1253, in a local run of the load harness (400 agents behind 20 controllers, 2 processes, 20 msg/s), CPU per message went from 14.8 to 14.1 ms.

Stack (merge order)

  1. Make idle controller agents and startup cheap on the database as deployments grow (CHOO-3667) #1200
  2. Read a controller's agents' heads once per delivery, not three times (CHOO-3667) #1220
  3. Try a controller's delivery pass again when the pool has no connection, instead of closing its socket (CHOO-3667) #1221
  4. Take an agent's head from the notification of its new event (CHOO-3667) #1246
  5. Deliver an agent's event in three statements, not seven (CHOO-3667) #1251
  6. Store a message's own events together, in two statements (CHOO-3667) #1252
  7. Have a controller's stream wait on one bell, not one per agent (CHOO-3667) #1253
  8. This PR ← you are here

…667)

A stream reads an agent's own events for every event it delivers, and a
CPU profile of a load test put building that statement, and the check
of the rooms dropped after the agent's cursor, at the top of what the
server spent building statements. Each of the read's sixteen shapes
(rooms or not, notifiable only or not, a bound or not, a limit or not)
is now built once and run with bind parameters, and so is the check.

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

Two open findings remain, both nit-level docstring issues in _own_events_read and its test that explain the change's performance motive instead of the behavior, which conflicts with the repository's comment guidance.

2 open issues
What changed in this PR

This PR speeds up how Switch reads an agent's own events. A stream reads those events for every event it delivers, and building the SQL statement each time was a large share of server CPU in a load test. Each shape of the read is now built once, cached, and run with bind parameters. The check for rooms dropped after the agent's cursor is built once the same way.

Changes:

  • The agent event read is built once for each of its 16 shapes (rooms, notifiable-only, through-sequence, and limit flags) and run with bind parameters.
  • The rooms-dropped-after check is a module-level statement that takes bind parameters.
  • A new test confirms that a repeated read shape does not rebuild its statement, and the changelog gets an entry for the change.
File Description
core/​tests/​switch_core/​bridges/​agent/​protocol/​test_event_buffer_cost.py Adds a test that a repeated read shape reuses its cached statement instead of building it again.
core/​switch_core/​bridges/​agent/​protocol/​event_buffer.py Caches the per-shape event read with functools.cache, moves the rooms-dropped check to a bound module-level statement, and calls both with bind parameters.
CHANGELOG.md Adds a changelog entry noting that reading an agent's own events no longer rebuilds its query on each read.

Review effort: Lite

Comment on lines +2528 to +2534
"""The statement `EventBuffer.read_from` runs for one shape of read, built
once per shape (there are sixteen) and run with bind parameters: the
agent's own events after `:after_seq`, oldest first, each with the rooms
in which `expire` deleted own events of the agent numbered after
`:after_seq` (NULL for none), so the two are read in one statement. A
stream reads an agent's events for every event it delivers, and building
the statement each time cost more than running it.
Comment on lines +147 to +149
"""A stream reads an agent's events for every event it delivers, and
building the statement each time cost more than running it, so each shape
of read is built once and run with bind parameters."""
@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