Repository navigation
Build the budget standings statement once, not on every read (CHOO-3667) - #1250
Closed
abeldantas wants to merge 1 commit into
Closed
abeldantas wants to merge 1 commit into
abeldantas wants to merge 1 commit into
Conversation
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 01:38
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The implementation has no actionable findings, includes regression coverage for statement reuse, and the only remaining comments are non-blocking wording nits.
No open issues 🎉
What changed in this PR
This pull request reduces per-message overhead by constructing budget standings SQL statements once at import time and reusing them with bind parameters, while preserving existing filtering and spend-calculation behavior.
Changes:
- Reuses tenant-wide and agent-specific standings statements.
- Updates
BudgetStore.standingsto execute parameterized statements. - Adds regression coverage and documents the optimization in the changelog.
This pull request is ready to be approved.
| File | Summary | Findings |
|---|---|---|
core/tests/switch_core/db/stores/test_budget_store.py |
Adds coverage confirming standings statements are reused. | Nit: change “cost” to “costs” in the new docstring. (1 vote) |
core/switch_core/db/stores/budget_store.py |
Builds and reuses the parameterized budget standings statements. | No actionable findings. |
CHANGELOG.md |
Documents the query-construction performance improvement. | Nit: change “cost” to “costs” in the changelog entry. (1 vote) |
Review effort: Lite
Every message an agent posts reads its budget standings twice: once to check the agent may post, and once as the router decides the row. Each read built its statement again, a correlated subquery and a dozen comparisons, and in a CPU profile of a load test building it was about a tenth of the server's time, more than running it. The two statements (every budget of a tenant, the budgets covering one agent) are now built at import and run with bind parameters.
abeldantas
force-pushed
the
budget-standings-once
branch
from
November 1, 2026 01:43
0ed4cbe to
bbf1238
Compare
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.
Every message an agent posts reads its budget standings twice: once to check the agent may post, and once as the router decides the row. Each read built its statement again, a correlated subquery and a dozen comparisons, and building it took longer than running it.
In this PR we build the two statements (every budget of a workspace, the budgets covering one agent) once, at import, and run them with bind parameters (
BudgetStore.standings).In a local run of the load harness (400 agents behind 20 controllers, 2 processes, 20 msg/s), CPU per message went from 19.8 to 19.0 ms.