perf(storage): bound Usage page queries and avoid redundant counts - #5514
Open
liuxiaocs7 wants to merge 3 commits into
Open
liuxiaocs7 wants to merge 3 commits into
liuxiaocs7 wants to merge 3 commits into
Conversation
Refs apache#5038 Generated-by: Codex
Generated-by: Codex
5 tasks
Restore the architecture document and remove the added performance report. Keep query details and benchmark evidence in PR apache#5514. Generated-by: Codex
This was referenced Sep 19, 2026
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.
Summary
Usage activity pages currently scan and project the remaining time range before selecting 50 rows. Seek on each source's existing composite index, filter and take 51 matches per source, then merge at most 153 candidates. Keep the existing ordering, cursors and revision transaction. Filtered counts select only search/status fields; unfiltered totals reuse complete statistics, and zero matches skip the activity query.
With 50,000 tool records, local second-page median/p95 falls from 53.530/60.211 ms to 0.803/0.958 ms on Node, and 59.392/76.300 ms to 0.853/1.128 ms on Electron. The performance evidence below compares explicit commits through the real Storage facade. Full statistics, exact filtered counts and sparse searches can still scan history. Statistics caching remains follow-up work; this complements #5410's frontend work.
Fixes #5515
Refs #5038
Verification
NODE_OPTIONS=--disable-warning=ExperimentalWarning; Usage: 27/27 on both Node and Electron; Host Usage: 6/6. The two search-work regressions fail on the baseline. The mixed-source oracle covers all pages, exact counts, ties, Unicode, inclusive ranges and accounting variants.npm testwas attempted and is not green: a Storage child-stderr assertion rejects Node's SQLite experimental warning (also reproduced on the baseline); unrelated Runtime Host/Eval lifecycle cases time out. The Host child-patch timeout also reproduces with the baseline Host test; the other failing cases pass focused reruns. The baseline worktree shares installed dependencies, so this is not a fully isolated environment comparison. Eval's Python suite was not reached after its JavaScript failure.Performance evidence
Query details, reproducible benchmarks and regression coverage
Query details
Each source applies its cursor and filters before selecting up to 51 matching rows. For equal timestamps, sources below the cursor source include that timestamp, sources above it exclude it, and the same source seeks by
(timestamp, stableStorageIdentity). The validated cursor replaces the redundant range upper bound so SQLite can seek on both composite-index columns. The global(ts DESC, source DESC, identity DESC)ordering, 50-row response, cursor validation and revision transaction remain unchanged.The unfiltered activity total is the model-request count plus tool calls from the complete range statistics. Filtered exact counts select only search/status fields. A zero count skips the activity query inside that same snapshot.
Reproduce
Build Core and Storage at both revisions. From the optimized checkout, run:
On macOS, also exercise the shipped SQLite runtime:
The harness calls the actual
readUsageScreenfacade at both revisions againstthe same temporary database. It alternates before/after calls in one process,
performs three warm-ups and 15 measurements, and saves raw samples, median/p95,
environment, Git revisions, SQL and query plans. Instrumented search callback
counts run outside the timing loop. First-screen timings use reopened reader
connections, not a cold OS page cache. Fixtures are persisted directly into
the production schema; fixture insertion is not timed.
Deep-page requests use known fixture positions as valid cursors. This measures
the query after a cursor is available, not the UI's sequential deep-page walk.
Host projection repair, wire serialization, IPC/network and React are excluded.
Results
Measured on 2026-09-20 against baseline
0117d76c5688475e7467ee6db83057bf65edbabdand optimized commite39d80b11a636e57d5fc7eabffc53bcdcf8d4ec2, with a clean tracked working tree.Timings are milliseconds; each cell is
median / p95. These are synthetic local measurements, not production latency
guarantees.
On Node, matching second-page searches over 50,000 tools go from 99,900 to 51
JavaScript lowercase calls; the mixed-source fixture goes from 99,900 to 153.
No-match tool searches go from 300,000 to 150,000 calls because count zero
avoids the second scan. These count function evaluations, not physical row
visits. Exact counts and sparse substring searches still depend on history size.
The continuation plan now uses the source's composite index as a seek:
The outer sort may still use a temporary B-tree; its input is at most 153
matching candidates. Per-source filtering can inspect more than 51 index
entries. Full aggregates remain range scans, explaining why a complete screen
still costs substantially more than a continuation.
Regression coverage
The Storage facade tests verify exact totals and all pages against an independent
fixture oracle across canonical, legacy and tool sources, equal timestamps,
Unicode identities, duplicate display IDs, inclusive range boundaries,
success/error/aborted status, Unicode lowercase expansion, literal
%/_, andunreadable/free/unpriced canonical rows. Existing WAL, rollback, revision and
restore tests still exercise the production reader.
Two deterministic work regressions fail on the baseline: a matching continuation
over 512 tied records performs 924 search folds, and a no-match screen over 120
tools performs 720. Their bounded-work assertions avoid hardware-sensitive
millisecond thresholds. Node and Electron both pass the optimized Usage suite.
AI use
Select exactly one:
Tool(s) and scope: Codex implemented the SQL optimization, regression tests, benchmark and PR notes, and performed automated reviews. AI-authored commits include
Generated-by: Codex; retain the trailer when squashing.Checklist
Does this PR entail a change in behavior?