Skip to content

perf(api-v1): ?expand=assignees sends two statements per distinct assignee #123

Open

Description

@aRustyDev

What happens

GET /api/v1/.../work-items/?expand=assignees looks each distinct assignee up one at a time:

// crates/thrust-api-v1/src/work_items.rs:206-213 (as of 1991731)
if wanted.contains("assignees") {
    let people: BTreeSet<UserId> = rows.iter().flat_map(|r| r.attrs.assignee_ids.iter().copied()).collect();
    let mut briefs = HashMap::new();
    for user in people {
        briefs.insert(user, user_brief(ctx, user).await?);
    }
    x.assignees = Some(briefs);
}

user_brief (:165) awaits user_by_id and then profile, so each assignee costs two statements, one after the other.
A list costs its own statements plus 2 x (distinct assignees on the page).

The cost is bounded only by the page and the project: a page holds up to 1000 items (MAX_PER_PAGE, also the default), and the
distinct assignees on it are at most the project's members. A page spread over 10 people sends the list's 4 statements plus 20.
Each statement is one round trip from the Worker to the database, which FR-08 item 2 budgets at 5 for a list.

The single-item reads (shown, :218) pay the same for that one item's assignees.

Why it is open

The budget test from #108 holds the default shape of each list and leaves expand out (the test's header and G-24 in
docs/src/testing/status.md record it as a known gap). It has not been measured with assignees; the figure above is from reading the code.

Probably also over budget (not measured: read from the code)

?expand=state and ?expand=labels each add one statement (project_states, project_labels). /api/v1 work items are at 4
by default, so ?expand=state,labels should be 6, one over the budget, with no assignees at all. Measure before fixing.

Proposal

  1. Measure first. Extend crates/thrust-contract-tests/tests/budget/mod.rs over every subset of expand
    (state, labels, assignees: eight cases) and over 0, 1 and 5 distinct assignees, on a list seeded with six or more items.
    The property that fails today: the count does not depend on how many assignees there are. Run it on main first and record
    the red result (expected: assignees 1 vs 5 differ by 8).
  2. Batch the lookup. A port method that reads the users and profiles of a set of ids in one statement
    (WHERE id = ANY($1), profiles joined), in both adapters; expansion calls it once. An empty set sends nothing.
  3. Decide what the budget means for expanded lists. Even batched, state,labels,assignees is 4 + 3 = 7. Either the three
    expansions fold into fewer statements (one query for all of them), or the budget for an expanded list is stated as its own
    number. That is the owner's call under FR-08; the issue should not pick it silently.
  4. pg_adapter's counting tests get one for the new method: exactly 1 statement for any set size.
  5. When it holds: drop the gap from G-24, the header of the budget test, docs/src/flows/app.md and
    docs/src/testing/techniques/contract-suite.md.

Acceptance

  • The budget test covers the eight expand subsets on /api/v1 work items, with several distinct assignees, on native-pg and worker-cf.
  • It failed on main before the change (the red run is in the PR description) and passes after it.
  • The count of a list does not grow with the number of distinct assignees.
  • The budget for an expanded list is written down (in status.md G-24) with the owner's decision.

Refs: #39

Activity

  1. added
    area:app
    on Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:app

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions