perf: round 14 — faces narrow projection, auth per-request allocs, CalDAV emit buffers, frontend set churn

Benchmark-gated (benches/ROUND14.md); every change ships a BEFORE/AFTER
benchmark with an equivalence gate and is rolled back on regression (the
rule is encoded as a GATE FAIL exit / threshold expect).

Backend
- Q1 faces_for_file → narrow face_boxes_for_file(id, person_id, bbox) with the
  caller filter pushed into SQL: drops the 2 KiB embedding BYTEA + 6 unused
  columns per face. 15-face lightbox open 0.312→0.219 ms, 32 KB→840 B/req.
- A1 cookie auth uses the borrow-only extract_cookie_str (already backs CSRF)
  instead of extract_cookie_value's owned String: -1 alloc/cookie request.
- A2 compute_relevance ASCII case-fold fast path vs name.to_lowercase() per
  result row (Unicode fallback preserved): 1.40x, 12→3 allocs/page.
- A3 sub pre-parsed to Uuid at decode time (TokenClaims.sub_id) vs re-parsing
  the 36-char claim on every request incl. cache hits: 22.7→0.7 ns.
- A4 auth + NextCloud middlewares borrow request.headers() instead of taking
  axum's HeaderMap extractor (a full map clone): 2→0 allocs/authed request.
- A5 CalDAV getlastmodified via the stack rfc2822_utc (byte-identical to
  chrono) vs a per-event to_rfc2822() heap String: 5→0 allocs.
- A6 CalDAV per-event href + quoted etag written into reused page buffers vs a
  fresh format! pair per event: 3.48x, 240→6 allocs/40-event page.

Frontend
- F1 t() shares one frozen EMPTY_PARAMS for the no-interpolation call forms vs
  a throwaway {} per call: -1 alloc/call.
- F2 favorites favoriteIds is a persistent SvelteSet with per-page add (clear
  on reset) vs a brand-new set over the whole accumulated list each page:
  22.3x over a 40-page drain (O(N^2)→O(N)).

Verified: cargo check --all-targets, cargo clippy -D warnings, both bench
packs (GATE PASS), frontend npm run check + vitest (4/4). ROUND14.md also
records the investigated-but-deferred backlog (music N+1, contact vcard
over-fetch, CachedBlobBackend syscalls, ResourceList.sections builder, etc.).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PymgCdK78NzUF3oRAQCJfN
This commit is contained in:
Claude
2026-07-19 10:22:12 +00:00
parent 3d578e4fc2
commit c930f865b0
19 changed files with 1345 additions and 73 deletions
+14 -7
View File
@@ -707,15 +707,22 @@ async fn section_save_faces(pool: &Arc<PgPool>, passes: usize) {
})
.await;
// Gate: batch write round-trips identically (row content check).
// Gate: batch write round-trips identically (row content check). Read the
// probe row back with a direct full-column SELECT — the repo's narrow
// `face_boxes_for_file` (ROUND14 §Q1) no longer returns embedding/quality/
// blob_hash, so this section fetches them itself to keep the gate intact.
let probe = make_faces(3);
repo.save_faces(&probe).await.unwrap();
let stored = repo.faces_for_file(s.file).await.unwrap();
let got = stored.iter().find(|f| f.id == probe[1].id).expect("stored");
assert_eq!(got.bbox.to_array(), probe[1].bbox.to_array());
assert_eq!(got.embedding.len(), probe[1].embedding.len());
assert_eq!(got.quality, probe[1].quality);
assert_eq!(got.blob_hash, probe[1].blob_hash);
let (bbox, embedding, quality, blob_hash): (Vec<f32>, Vec<u8>, Option<f32>, Option<String>) =
sqlx::query_as("SELECT bbox, embedding, quality, blob_hash FROM faces.faces WHERE id = $1")
.bind(probe[1].id)
.fetch_one(pool.as_ref())
.await
.expect("stored");
assert_eq!(bbox, probe[1].bbox.to_array());
assert_eq!(embedding.len() / 4, probe[1].embedding.len());
assert_eq!(quality, probe[1].quality);
assert_eq!(blob_hash, probe[1].blob_hash);
println!(
" 30-face image: BEFORE loop {before_ms:.3} ms → AFTER UNNEST {after_ms:.3} ms ({:.1}x)",