perf: round 11 — StoragePath joined-only, classifier fusion, memoized bodies, query-shape pack, SPA fine-grained stars
Backend (each change benchmark-gated with BEFORE replicas + equivalence gates; see examples/bench_round11_micro.rs, bench_round11_queries.rs, bench_log_writer.rs and benches/ROUND11.md — final numbers land in the follow-up doc commit): - StoragePath re-representation: single canonical joined String, segments derived on demand; File/Folder drop the duplicated path_string field (4000→1000 allocs per 500-row listing page) - Display classifier fusion: classify_display shares one stack-lowered extension across the three decision trees; call sites in FileDto, folder/favorites/recent handlers, trash, path-resolver (+ interning where Arc::from was still used) - /status.php and /openapi.json memoized into OnceLock<Bytes> (openapi rebuilt a 171 KiB spec per request: 2.8 ms → 18 ns) - NC upload-session PROPFIND: write! + pre-sized body + stack RFC2822 dates (2.3-2.6x, 2582→772 allocs at 256 chunks) - REST download: dead FileDto clone removed (capture mime/size + move) - CalendarEventDto/TrashedItem into_parts moves (11 KiB ical_data memcpy gone per CalDAV row); CardDAV getlastmodified stack render - 4xx path: borrowed ErrorResponse serialize, ErrorKind::as_str, not_found/already_exists clone kill - vCard emit via write!; search page moved out with into_iter skip/take; content-hit UUIDs parsed once; group last-user check via HashSet - RateLimiter: lock-free get + insert (and_upsert_with variant REJECTED by benchmark); CSRF token borrow-compare + borrowed cookie extraction - Thumbnail/preview ETags built from as_str (Debug-identical bytes) - Encrypted backend: encrypt_in_place_detached single-buffer write path, chunk-sized reserve in collect_stream; retry labels made lazy - PG: deferred upload registration 3→1 round-trips (persist_file CTE template); direct_grant_cache for Calendar/AddressBook/Playlist authz (single-flight + set_role/clear_role invalidation); expand_user tokio::join!; geo clusters min(uuid)::text; recluster face assignment batched into one UNNEST update - People recluster cosine: norms precomputed once (bit-identical gate) - NC capabilities poll logs demoted to debug; tracing-appender dep added for the log-writer benchmark Frontend: - ResourceList.selectedEntries O(N)-per-toggle → id-index projection O(k log k); favorites/recent consume the batchToolbar snippet param and drop their duplicate filter + dead selectedIds mirror - Recent: star state via new favoriteIds prop — a star click no longer rebuilds all N entries - admin timeAgo >30d fallback uses the cached Intl.DateTimeFormat - vitest gates in src/lib/components/round11.bench.test.ts Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ABhTEHuGujvwoodh67Kga7
This commit is contained in:
@@ -23,17 +23,30 @@ use crate::common::errors::DomainError;
|
||||
use crate::domain::entities::face::Person;
|
||||
use crate::infrastructure::repositories::pg::FacePgRepository;
|
||||
|
||||
/// Cosine similarity of two equal-length vectors. Embeddings are produced
|
||||
/// L2-normalized, so this is ~a dot product; we normalize anyway for safety.
|
||||
fn cosine(a: &[f32], b: &[f32]) -> f32 {
|
||||
/// Squared L2 norm, accumulated in the same order `cosine` used to, so
|
||||
/// the precomputed-norm path is bit-identical to the old per-pair one.
|
||||
fn norm_sq(v: &[f32]) -> f32 {
|
||||
let mut n = 0.0f32;
|
||||
for &x in v {
|
||||
n += x * x;
|
||||
}
|
||||
n
|
||||
}
|
||||
|
||||
/// Cosine similarity of two equal-length vectors given their precomputed
|
||||
/// squared norms. Embeddings are produced L2-normalized, so this is ~a dot
|
||||
/// product; we normalize anyway for safety. The O(N²) recluster pair loop
|
||||
/// used to re-accumulate BOTH norms on every pair — precomputing them once
|
||||
/// per face keeps only the dot product in the hot loop while the final
|
||||
/// `dot / (√na · √nb)` expression (and the zero guards) stay exactly as
|
||||
/// before, so results are bit-identical (benches/ROUND11.md §17).
|
||||
fn cosine_with_norms(a: &[f32], b: &[f32], na: f32, nb: f32) -> f32 {
|
||||
if a.len() != b.len() || a.is_empty() {
|
||||
return 0.0;
|
||||
}
|
||||
let (mut dot, mut na, mut nb) = (0.0f32, 0.0f32, 0.0f32);
|
||||
let mut dot = 0.0f32;
|
||||
for (&x, &y) in a.iter().zip(b.iter()) {
|
||||
dot += x * y;
|
||||
na += x * x;
|
||||
nb += y * y;
|
||||
}
|
||||
if na == 0.0 || nb == 0.0 {
|
||||
return 0.0;
|
||||
@@ -102,10 +115,13 @@ impl PeopleService {
|
||||
return Ok(0);
|
||||
}
|
||||
|
||||
let norms: Vec<f32> = faces.iter().map(|f| norm_sq(&f.embedding)).collect();
|
||||
let mut uf = UnionFind::new(n);
|
||||
for i in 0..n {
|
||||
for j in (i + 1)..n {
|
||||
if cosine(&faces[i].embedding, &faces[j].embedding) >= self.cluster_threshold {
|
||||
if cosine_with_norms(&faces[i].embedding, &faces[j].embedding, norms[i], norms[j])
|
||||
>= self.cluster_threshold
|
||||
{
|
||||
uf.union(i, j);
|
||||
}
|
||||
}
|
||||
@@ -117,13 +133,19 @@ impl PeopleService {
|
||||
groups.entry(root).or_default().push(i);
|
||||
}
|
||||
|
||||
// Accumulate every (face, person) change and apply them in ONE
|
||||
// UNNEST batch at the end — the old per-face `assign_person` loop
|
||||
// issued up to F sequential UPDATE round-trips per recluster
|
||||
// (benches/ROUND11.md §Q5; the ROUND10 `save_faces` pattern). The
|
||||
// final column state is identical.
|
||||
let mut assignments: Vec<(Uuid, Option<Uuid>)> = Vec::new();
|
||||
let mut created = 0usize;
|
||||
for idxs in groups.into_values() {
|
||||
if idxs.len() < self.min_faces {
|
||||
// Too small to be a person — leave/reset these faces unassigned.
|
||||
for &i in &idxs {
|
||||
if faces[i].person_id.is_some() {
|
||||
self.repo.assign_person(faces[i].id, None).await?;
|
||||
assignments.push((faces[i].id, None));
|
||||
}
|
||||
}
|
||||
continue;
|
||||
@@ -151,9 +173,7 @@ impl PeopleService {
|
||||
};
|
||||
for &i in &idxs {
|
||||
if faces[i].person_id != Some(person_id) {
|
||||
self.repo
|
||||
.assign_person(faces[i].id, Some(person_id))
|
||||
.await?;
|
||||
assignments.push((faces[i].id, Some(person_id)));
|
||||
}
|
||||
}
|
||||
let _ = self
|
||||
@@ -161,6 +181,7 @@ impl PeopleService {
|
||||
.set_person_cover(person_id, faces[idxs[0]].id)
|
||||
.await;
|
||||
}
|
||||
self.repo.assign_person_batch(&assignments).await?;
|
||||
|
||||
Ok(created)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user