Repository navigation
Build an agent's event read once per shape, not on every read (CHOO-3667) - #1254
Closed
abeldantas wants to merge 1 commit into
Closed
abeldantas wants to merge 1 commit into
abeldantas wants to merge 1 commit into
Conversation
…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.
abeldantas
requested review from
agent-runner353,
amaudruz,
christian-mcdermott,
lbangalosbt,
massimo-sandboxaq,
niall-sandboxaq,
petr-sandbox,
raullopez-sandbox and
simonflk-sb
as code owners
November 1, 2026 02:35
Contributor
There was a problem hiding this comment.
🟡 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.""" |
This was referenced Nov 1, 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 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)