Skip to content

Answer GitHub mention searches from an index with a limit of 8 - #8219

Merged
ymichael merged 2 commits into
get-bb:mainfrom
lanjak:perf-github-mention-limit
Nov 30, 2026
Merged

ymichael merged 2 commits into
get-bb:mainfrom
lanjak:perf-github-mention-limit

Conversation

@lanjak

@lanjak lanjak commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Human comments

What was wrong

The GitHub plugin's issue and pr mention providers run mentionItems on every debounced @ or # keystroke (

bb/plugins/github/server.ts

Lines 1579 to 1587 in 2a21dd5

function mentionItems(kind: "issue" | "pr", query: string) {
return listCachedItems({ kind, query, state: "open" })
.slice(0, 8)
.map((item) => ({
id: `${item.repo}#${item.number}`,
title: `#${item.number} ${item.title}`,
subtitle: item.repo,
}));
}
). mentionItems called listCachedItems({ kind, query, state: "open" }) and then .slice(0, 8). That query is SELECT * FROM items ... ORDER BY updated_at DESC and has no LIMIT (

bb/plugins/github/server.ts

Lines 762 to 801 in 2a21dd5

function listCachedItems(options: {
kind?: "issue" | "pr";
repo?: string;
query?: string;
state?: "open" | "closed";
assignee?: string;
}): CachedItem[] {
const clauses: string[] = [];
const params: unknown[] = [];
if (options.kind !== undefined) {
clauses.push("kind = ?");
params.push(options.kind);
}
if (options.repo !== undefined) {
clauses.push("repo = ?");
params.push(options.repo);
}
if (options.state === "open") {
clauses.push("state = 'OPEN'");
} else if (options.state === "closed") {
clauses.push("state != 'OPEN'");
}
if (options.assignee !== undefined) {
clauses.push("assignees LIKE ?");
params.push(`%${JSON.stringify(options.assignee)}%`);
}
const query = options.query?.trim() ?? "";
if (query.length > 0) {
clauses.push(
"(title LIKE ? OR CAST(number AS TEXT) LIKE ? OR repo LIKE ?)",
);
const like = `%${query.replace(/^#/, "")}%`;
params.push(like, like, like);
}
const where = clauses.length > 0 ? `WHERE ${clauses.join(" AND ")}` : "";
const rows = db
.prepare(`SELECT * FROM items ${where} ORDER BY updated_at DESC`)
.all(...params) as Record<string, unknown>[];
return rows.map(rowToItem);
}
), and the table has no index besides its primary key (repo, kind, number). So SQLite read every cached row, also closed rows and 3.5 KB bodies. It sorted all matches in a temporary B-tree (SCAN items | USE TEMP B-TREE FOR ORDER BY). The plugin then parsed labels and assignees for each match and kept 8. The sync keeps up to 230 rows per tracked repository. One keystroke cost 5.65 ms at 10 repositories and 33.58 ms at 40. This work runs synchronously in the server process. I measured the real search path before changing anything (see the table below).

What changed

  • plugins/github/server.ts: a new storage migration adds CREATE INDEX items_kind_state_updated_number ON items (kind, state, updated_at, number), and listings order by updated_at DESC, number DESC. SQLite runs the mention query as SEARCH items USING INDEX items_kind_state_updated_number (kind=? AND state=?). It walks the open rows of one kind newest first and does no sort. The number tie-breaker keeps rows with equal updated_at in a fixed order; without it the index walk reversed their previous order (seen on a real cache: #835 listed 8355 before 8354 on main, 8354 first with an updated_at-only index).
  • listCachedItems takes an optional limit and binds it as LIMIT ?. mentionItems passes limit: 8 instead of slicing, so the walk stops after 8 matches. The bb github issues, bb github prs and listItems RPC listings pass no limit and return the same rows as before. The new index can change only their plan, not their result.
  • No wire, CLI, RPC or protocol changes.

Deviation from the issue's first idea (LIMIT 8 and only repo, number, title): neither change alone removes the scan. The body is in the same page as the row, so the query must read every row page to find the newest 8. Measured at 40 repositories: LIMIT 8 alone is 11-16 ms, and three columns with LIMIT 8 is about 9 ms. With the index and LIMIT 8, only the 8 result rows are read. Then SELECT * of 8 rows costs nothing measurable, so the mention path keeps the one shared query and does not get a second column-specific copy of it.

How you verified

New test in plugins/github/server.batching.test.ts: "answers a mention search with the newest 8 open matches through the index". It syncs 10 matching open issues (shuffled updatedAt, two sharing one), a closed issue and an open pull request through the fake GraphQL response. The test then checks that the issue provider returns exactly the 8 newest issues in order, and that SQLite plans the statement of the search as an index search with no scan and no temporary B-tree.

  • On origin/main (unfixed), it fails:
    AssertionError: expected [ 'SCAN items', …(1) ] to deeply equal [ StringMatching{…} ]
    -   StringMatching /^SEARCH items USING INDEX /,
    +   "SCAN items",
    +   "USE TEMP B-TREE FOR ORDER BY",
    
  • With LIMIT 8 and no index, it fails on the same plan assertion.
  • With the index and no LIMIT, it fails: expected [ { id: 'acme/one#9', …(2) }, …(8) ] to deeply equal [ { id: 'acme/one#9', …(2) }, …(7) ] (9 results).
  • With the fix, it passes.

Measurements: a temporary Vitest file (not committed) loads the real plugin with createFakePluginHost. Like the host, it uses a file-backed SQLite database with the default page cache. The file inserts 230 rows per repository (100 open and 50 closed issues, 50 open and 30 merged pull requests, 3.5 KB bodies) and times both providers' search calls for one keystroke. Each value is the median of 200 runs after 20 warm-up runs.

Repos (rows) Query Before After
1 (230) "" 0.38 ms 0.11 ms
1 (230) "a" 0.33 ms 0.09 ms
1 (230) "zzz" (no match) 0.09 ms 0.12 ms
10 (2,300) "" 5.65 ms 0.07 ms
10 (2,300) "a" 3.74 ms 0.11 ms
10 (2,300) "fix" 0.80 ms 0.12 ms
10 (2,300) "zzz" (no match) 0.43 ms 0.41 ms
40 (9,200) "" 33.58 ms 0.06 ms
40 (9,200) "a" 32.34 ms 0.07 ms
40 (9,200) "fix" 18.35 ms 0.10 ms
40 (9,200) "item 17" (matches only in older rows) 16.76 ms 7.31 ms
40 (9,200) "zzz" (no match) 16.02 ms 6.95 ms

The walk stops at the eighth match. A query with fewer than 8 matches, or with matches only in older rows, still reads most open rows of that kind. It no longer reads closed rows or the other kind, and it does not sort, so it is still about 2x faster than before.

Commands:

pnpm exec turbo run typecheck test lint lint:type-aware --continue --concurrency=6 --filter=bb-plugin-github

All 11 tasks pass (44 tests). lint reports 3 warnings in plugins/github/app.tsx (react(immutability), two react(set-state-in-effect)), which are on origin/main and not in this change.

Fixes #8218

AGENT GENERATED

The issue and pull request mention providers run on every debounced
keystroke. Each search read every cached row of the table, including
closed rows and bodies, sorted the matches in a temporary B-tree, parsed
labels and assignees for each match, and then kept 8.

Add an index on items (kind, state, updated_at) and pass LIMIT 8 to the
query, so SQLite walks the open rows of one kind newest first and stops
after 8 matches. The CLI and listItems RPC keep their full listing.
The index walk returned rows with equal updated_at in reverse insertion
order, flipping the previous order. Index (kind, state, updated_at,
number) and order by number DESC so ties stay deterministic and still
need no sort.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit 4f4da12 into get-bb:main Nov 30, 2026
32 checks passed
scowalt pushed a commit to scowalt/bb that referenced this pull request Dec 1, 2026
…b#8219)

## Human comments

## What was wrong

The GitHub plugin's `issue` and `pr` mention providers run
`mentionItems` on every debounced `@` or `#` keystroke
(https://github.com/get-bb/bb/blob/e9e0493838b52b1276625f32730c26fb908ea159/plugins/github/server.ts#L1579-L1587).
`mentionItems` called `listCachedItems({ kind, query, state: "open" })`
and then `.slice(0, 8)`. That query is `SELECT * FROM items ... ORDER BY
updated_at DESC` and has no `LIMIT`
(https://github.com/get-bb/bb/blob/e9e0493838b52b1276625f32730c26fb908ea159/plugins/github/server.ts#L762-L801),
and the table has no index besides its primary key `(repo, kind,
number)`. So SQLite read every cached row, also closed rows and 3.5 KB
bodies. It sorted all matches in a temporary B-tree (`SCAN items | USE
TEMP B-TREE FOR ORDER BY`). The plugin then parsed labels and assignees
for each match and kept 8. The sync keeps up to 230 rows per tracked
repository. One keystroke cost 5.65 ms at 10 repositories and 33.58 ms
at 40. This work runs synchronously in the server process. I measured
the real `search` path before changing anything (see the table below).

## What changed

- `plugins/github/server.ts`: a new storage migration adds `CREATE INDEX
items_kind_state_updated_number ON items (kind, state, updated_at,
number)`, and listings order by `updated_at DESC, number DESC`. SQLite
runs the mention query as `SEARCH items USING INDEX
items_kind_state_updated_number (kind=? AND state=?)`. It walks the open
rows of one kind newest first and does no sort. The `number` tie-breaker
keeps rows with equal `updated_at` in a fixed order; without it the
index walk reversed their previous order (seen on a real cache: `get-bb#835`
listed 8355 before 8354 on main, 8354 first with an `updated_at`-only
index).
- `listCachedItems` takes an optional `limit` and binds it as `LIMIT ?`.
`mentionItems` passes `limit: 8` instead of slicing, so the walk stops
after 8 matches. The `bb github issues`, `bb github prs` and `listItems`
RPC listings pass no limit and return the same rows as before. The new
index can change only their plan, not their result.
- No wire, CLI, RPC or protocol changes.

Deviation from the issue's first idea (`LIMIT 8` and only `repo, number,
title`): neither change alone removes the scan. The body is in the same
page as the row, so the query must read every row page to find the
newest 8. Measured at 40 repositories: `LIMIT 8` alone is 11-16 ms, and
three columns with `LIMIT 8` is about 9 ms. With the index and `LIMIT
8`, only the 8 result rows are read. Then `SELECT *` of 8 rows costs
nothing measurable, so the mention path keeps the one shared query and
does not get a second column-specific copy of it.

## How you verified

New test in `plugins/github/server.batching.test.ts`: "answers a mention
search with the newest 8 open matches through the index". It syncs 10
matching open issues (shuffled `updatedAt`, two sharing one), a closed
issue and an open pull request through the fake GraphQL response. The
test then checks that the issue provider returns exactly the 8 newest
issues in order, and that SQLite plans the statement of the search as an
index search with no scan and no temporary B-tree.

- On origin/main (unfixed), it fails:
  ```
AssertionError: expected [ 'SCAN items', …(1) ] to deeply equal [
StringMatching{…} ]
  -   StringMatching /^SEARCH items USING INDEX /,
  +   "SCAN items",
  +   "USE TEMP B-TREE FOR ORDER BY",
  ```
- With `LIMIT 8` and no index, it fails on the same plan assertion.
- With the index and no `LIMIT`, it fails: `expected [ { id:
'acme/one#9', …(2) }, …(8) ] to deeply equal [ { id: 'acme/one#9', …(2)
}, …(7) ]` (9 results).
- With the fix, it passes.

Measurements: a temporary Vitest file (not committed) loads the real
plugin with `createFakePluginHost`. Like the host, it uses a file-backed
SQLite database with the default page cache. The file inserts 230 rows
per repository (100 open and 50 closed issues, 50 open and 30 merged
pull requests, 3.5 KB bodies) and times both providers' `search` calls
for one keystroke. Each value is the median of 200 runs after 20 warm-up
runs.

| Repos (rows) | Query | Before | After |
|---|---|---|---|
| 1 (230) | `""` | 0.38 ms | 0.11 ms |
| 1 (230) | `"a"` | 0.33 ms | 0.09 ms |
| 1 (230) | `"zzz"` (no match) | 0.09 ms | 0.12 ms |
| 10 (2,300) | `""` | 5.65 ms | 0.07 ms |
| 10 (2,300) | `"a"` | 3.74 ms | 0.11 ms |
| 10 (2,300) | `"fix"` | 0.80 ms | 0.12 ms |
| 10 (2,300) | `"zzz"` (no match) | 0.43 ms | 0.41 ms |
| 40 (9,200) | `""` | 33.58 ms | 0.06 ms |
| 40 (9,200) | `"a"` | 32.34 ms | 0.07 ms |
| 40 (9,200) | `"fix"` | 18.35 ms | 0.10 ms |
| 40 (9,200) | `"item 17"` (matches only in older rows) | 16.76 ms |
7.31 ms |
| 40 (9,200) | `"zzz"` (no match) | 16.02 ms | 6.95 ms |

The walk stops at the eighth match. A query with fewer than 8 matches,
or with matches only in older rows, still reads most open rows of that
kind. It no longer reads closed rows or the other kind, and it does not
sort, so it is still about 2x faster than before.

Commands:

```
pnpm exec turbo run typecheck test lint lint:type-aware --continue --concurrency=6 --filter=bb-plugin-github
```

All 11 tasks pass (44 tests). `lint` reports 3 warnings in
`plugins/github/app.tsx` (`react(immutability)`, two
`react(set-state-in-effect)`), which are on origin/main and not in this
change.

Fixes get-bb#8218

> AGENT GENERATED

---------

Co-authored-by: Michael Yong <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

GitHub issue and PR mention search reads the whole cache on every keystroke

2 participants