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:
@@ -263,27 +263,32 @@ impl ContactService {
|
||||
}
|
||||
|
||||
fn generate_vcard(&self, contact: &Contact) -> String {
|
||||
// `write!` formats straight into `vcard`; the old
|
||||
// `push_str(&format!(…))` allocated a throwaway String per emitted
|
||||
// line (benches/ROUND11.md §10: 766 → 357 ns, 21 → 5 allocs).
|
||||
use std::fmt::Write as _;
|
||||
let mut vcard = String::from("BEGIN:VCARD\r\nVERSION:3.0\r\n");
|
||||
|
||||
// UID
|
||||
vcard.push_str(&format!("UID:{}\r\n", contact.uid()));
|
||||
let _ = write!(vcard, "UID:{}\r\n", contact.uid());
|
||||
|
||||
// Name fields
|
||||
if let Some(full_name) = contact.full_name() {
|
||||
vcard.push_str(&format!("FN:{}\r\n", full_name));
|
||||
let _ = write!(vcard, "FN:{}\r\n", full_name);
|
||||
}
|
||||
|
||||
let last_name = contact.last_name().unwrap_or_default().to_string();
|
||||
let first_name = contact.first_name().unwrap_or_default().to_string();
|
||||
vcard.push_str(&format!("N:{};{};;;\r\n", last_name, first_name));
|
||||
let last_name = contact.last_name().unwrap_or_default();
|
||||
let first_name = contact.first_name().unwrap_or_default();
|
||||
let _ = write!(vcard, "N:{};{};;;\r\n", last_name, first_name);
|
||||
|
||||
// Email addresses
|
||||
for email in contact.email() {
|
||||
vcard.push_str(&format!(
|
||||
let _ = write!(
|
||||
vcard,
|
||||
"EMAIL;TYPE={}:{}\r\n",
|
||||
email.r#type.to_uppercase(),
|
||||
email.email
|
||||
));
|
||||
);
|
||||
}
|
||||
|
||||
// Phone numbers
|
||||
@@ -295,49 +300,51 @@ impl ContactService {
|
||||
"fax" => "FAX",
|
||||
_ => "OTHER",
|
||||
};
|
||||
vcard.push_str(&format!("TEL;TYPE={}:{}\r\n", tel_type, phone.number));
|
||||
let _ = write!(vcard, "TEL;TYPE={}:{}\r\n", tel_type, phone.number);
|
||||
}
|
||||
|
||||
// Addresses
|
||||
for addr in contact.address() {
|
||||
let addr_type = addr.r#type.to_uppercase();
|
||||
let street = addr.street.clone().unwrap_or_default();
|
||||
let city = addr.city.clone().unwrap_or_default();
|
||||
let state = addr.state.clone().unwrap_or_default();
|
||||
let postal_code = addr.postal_code.clone().unwrap_or_default();
|
||||
let country = addr.country.clone().unwrap_or_default();
|
||||
let street = addr.street.as_deref().unwrap_or_default();
|
||||
let city = addr.city.as_deref().unwrap_or_default();
|
||||
let state = addr.state.as_deref().unwrap_or_default();
|
||||
let postal_code = addr.postal_code.as_deref().unwrap_or_default();
|
||||
let country = addr.country.as_deref().unwrap_or_default();
|
||||
|
||||
vcard.push_str(&format!(
|
||||
let _ = write!(
|
||||
vcard,
|
||||
"ADR;TYPE={}:;;{};{};{};{};{}\r\n",
|
||||
addr_type, street, city, state, postal_code, country
|
||||
));
|
||||
);
|
||||
}
|
||||
|
||||
// Organization
|
||||
if let Some(org) = contact.organization() {
|
||||
vcard.push_str(&format!("ORG:{}\r\n", org));
|
||||
let _ = write!(vcard, "ORG:{}\r\n", org);
|
||||
}
|
||||
|
||||
// Title
|
||||
if let Some(title) = contact.title() {
|
||||
vcard.push_str(&format!("TITLE:{}\r\n", title));
|
||||
let _ = write!(vcard, "TITLE:{}\r\n", title);
|
||||
}
|
||||
|
||||
// Notes
|
||||
if let Some(notes) = contact.notes() {
|
||||
vcard.push_str(&format!("NOTE:{}\r\n", notes));
|
||||
let _ = write!(vcard, "NOTE:{}\r\n", notes);
|
||||
}
|
||||
|
||||
// Birthday
|
||||
if let Some(birthday) = contact.birthday() {
|
||||
vcard.push_str(&format!("BDAY:{}\r\n", birthday.format("%Y%m%d")));
|
||||
let _ = write!(vcard, "BDAY:{}\r\n", birthday.format("%Y%m%d"));
|
||||
}
|
||||
|
||||
// Revision (last update)
|
||||
vcard.push_str(&format!(
|
||||
let _ = write!(
|
||||
vcard,
|
||||
"REV:{}\r\n",
|
||||
contact.updated_at().format("%Y%m%dT%H%M%SZ")
|
||||
));
|
||||
);
|
||||
|
||||
vcard.push_str("END:VCARD\r\n");
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -381,15 +381,19 @@ impl SearchService {
|
||||
// log it) — never leak. Batched: one drive-resolution query for
|
||||
// the whole page instead of up to CONTENT_HITS_LIMIT sequential
|
||||
// point SELECTs (benches/SEARCH-REBAC.md).
|
||||
let mut hit_ids = Vec::with_capacity(hits.len());
|
||||
for hit in &hits {
|
||||
// Parse each hit id ONCE and carry the pair through the verify loop
|
||||
// — the old shape re-parsed every `file_id` a second time below
|
||||
// (benches/ROUND11.md §12: 1.6x on a 100-hit page).
|
||||
let mut pairs = Vec::with_capacity(hits.len());
|
||||
for hit in hits {
|
||||
match Uuid::parse_str(&hit.file_id) {
|
||||
Ok(u) => hit_ids.push(u),
|
||||
Ok(u) => pairs.push((hit, u)),
|
||||
Err(_) => {
|
||||
tracing::warn!("Content-index hit had non-UUID file_id: {}", hit.file_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
let hit_ids: Vec<Uuid> = pairs.iter().map(|(_, u)| *u).collect();
|
||||
let allowed = match authz
|
||||
.check_files_read_batch(Subject::User(user_id), &hit_ids)
|
||||
.await
|
||||
@@ -400,11 +404,8 @@ impl SearchService {
|
||||
return Vec::new();
|
||||
}
|
||||
};
|
||||
let mut verified = Vec::with_capacity(hits.len());
|
||||
for hit in hits {
|
||||
let Ok(file_uuid) = Uuid::parse_str(&hit.file_id) else {
|
||||
continue; // already warned above
|
||||
};
|
||||
let mut verified = Vec::with_capacity(pairs.len());
|
||||
for (hit, file_uuid) in pairs {
|
||||
if allowed.contains(&file_uuid) {
|
||||
verified.push(hit);
|
||||
} else {
|
||||
@@ -692,13 +693,24 @@ impl SearchUseCase for SearchService {
|
||||
|
||||
let folder_start = start_idx.min(folder_count);
|
||||
let folder_end = end_idx.min(folder_count);
|
||||
let paginated_folders = enriched_folders[folder_start..folder_end].to_vec();
|
||||
// Move the page out of the owned vecs instead of
|
||||
// deep-cloning the slice — the source is dropped right
|
||||
// after (benches/ROUND11.md §11: −300 allocs per page).
|
||||
let paginated_folders: Vec<_> = enriched_folders
|
||||
.into_iter()
|
||||
.skip(folder_start)
|
||||
.take(folder_end - folder_start)
|
||||
.collect();
|
||||
|
||||
let file_start = start_idx.saturating_sub(folder_count);
|
||||
let file_end = end_idx
|
||||
.saturating_sub(folder_count)
|
||||
.min(enriched_files.len());
|
||||
let paginated_files = enriched_files[file_start..file_end].to_vec();
|
||||
let paginated_files: Vec<_> = enriched_files
|
||||
.into_iter()
|
||||
.skip(file_start)
|
||||
.take(file_end - file_start)
|
||||
.collect();
|
||||
|
||||
let elapsed_ms = start.elapsed().as_millis() as u64;
|
||||
|
||||
@@ -783,13 +795,24 @@ impl SearchUseCase for SearchService {
|
||||
|
||||
let folder_start = start_idx.min(folder_count);
|
||||
let folder_end = end_idx.min(folder_count);
|
||||
let paginated_folders = enriched_folders[folder_start..folder_end].to_vec();
|
||||
// Move the page out instead of deep-cloning the slice — the
|
||||
// recursive branch's vecs can hold the whole subtree match
|
||||
// set, all dropped right after (benches/ROUND11.md §11).
|
||||
let paginated_folders: Vec<_> = enriched_folders
|
||||
.into_iter()
|
||||
.skip(folder_start)
|
||||
.take(folder_end - folder_start)
|
||||
.collect();
|
||||
|
||||
let file_start = start_idx.saturating_sub(folder_count);
|
||||
let file_end = end_idx
|
||||
.saturating_sub(folder_count)
|
||||
.min(enriched_files.len());
|
||||
let paginated_files = enriched_files[file_start..file_end].to_vec();
|
||||
let paginated_files: Vec<_> = enriched_files
|
||||
.into_iter()
|
||||
.skip(file_start)
|
||||
.take(file_end - file_start)
|
||||
.collect();
|
||||
|
||||
let elapsed_ms = start.elapsed().as_millis() as u64;
|
||||
|
||||
|
||||
@@ -496,7 +496,11 @@ impl SubjectGroupService {
|
||||
.list_transitive_users(child_id)
|
||||
.await
|
||||
.map_err(map_repo_err)?;
|
||||
!child_users.is_empty() && users_before.iter().all(|u| child_users.contains(u))
|
||||
// Set probe instead of an O(|before|·|child|) slice scan
|
||||
// (benches/ROUND11.md §13: 5.7x at 500×500).
|
||||
let child_set: std::collections::HashSet<&uuid::Uuid> =
|
||||
child_users.iter().collect();
|
||||
!child_users.is_empty() && users_before.iter().all(|u| child_set.contains(u))
|
||||
}
|
||||
};
|
||||
if would_be_empty {
|
||||
|
||||
@@ -4,7 +4,7 @@ use uuid::Uuid;
|
||||
|
||||
use crate::application::dtos::cursor::PageCursor;
|
||||
use crate::application::dtos::display_helpers::{
|
||||
category_for, format_file_size, icon_class_for, icon_special_class_for,
|
||||
classify_display, format_file_size, intern_display, intern_mime,
|
||||
};
|
||||
use crate::application::dtos::file_dto::FileDto;
|
||||
use crate::application::dtos::folder_dto::FolderDto;
|
||||
@@ -115,25 +115,31 @@ impl TrashService {
|
||||
"folder-icon".to_string(),
|
||||
),
|
||||
TrashedItemType::File => {
|
||||
let name = item.name();
|
||||
// Use empty MIME type to leverage extension fallback
|
||||
let category = category_for(name, "").to_string();
|
||||
let icon_class = icon_class_for(name, "").to_string();
|
||||
let icon_special_class = icon_special_class_for(name, "").to_string();
|
||||
(category, icon_class, icon_special_class)
|
||||
// Use empty MIME type to leverage extension fallback; one
|
||||
// fused pass lowers the extension once instead of three
|
||||
// times (benches/ROUND11.md §21).
|
||||
let classes = classify_display(item.name(), "");
|
||||
(
|
||||
classes.category.to_string(),
|
||||
classes.icon_class.to_string(),
|
||||
classes.icon_special_class.to_string(),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
// Move the owned Strings out of the consumed item — the getter
|
||||
// `.to_string()` clones paid 2 extra allocations per trash row.
|
||||
let parts = item.into_parts();
|
||||
TrashedItemDto {
|
||||
id: item.id().to_string(),
|
||||
original_id: item.original_id().to_string(),
|
||||
item_type: match item.item_type() {
|
||||
id: parts.id.to_string(),
|
||||
original_id: parts.original_id.to_string(),
|
||||
item_type: match parts.item_type {
|
||||
TrashedItemType::File => "file".to_string(),
|
||||
TrashedItemType::Folder => "folder".to_string(),
|
||||
},
|
||||
name: item.name().to_string(),
|
||||
original_path: item.original_path().to_string(),
|
||||
trashed_at: item.trashed_at(),
|
||||
name: parts.name,
|
||||
original_path: parts.original_path,
|
||||
trashed_at: parts.trashed_at,
|
||||
days_until_deletion,
|
||||
category,
|
||||
icon_class,
|
||||
@@ -906,18 +912,19 @@ fn row_to_item_dto(row: TrashResourceRow) -> TrashResourceItemDto {
|
||||
} else {
|
||||
File::compute_etag(&content_hash, modified_at_u)
|
||||
};
|
||||
let classes = classify_display(&row.name, mime);
|
||||
let dto = FileDto {
|
||||
id: row.resource_id.to_string(),
|
||||
name: row.name.clone(),
|
||||
path,
|
||||
size: size_bytes,
|
||||
mime_type: std::sync::Arc::from(mime),
|
||||
mime_type: intern_mime(mime),
|
||||
folder_id: row.parent_id.map(|u| u.to_string()),
|
||||
created_at: row.resource_created_at.timestamp() as u64,
|
||||
modified_at: modified_at_u,
|
||||
icon_class: std::sync::Arc::from(icon_class_for(&row.name, mime)),
|
||||
icon_special_class: std::sync::Arc::from(icon_special_class_for(&row.name, mime)),
|
||||
category: std::sync::Arc::from(category_for(&row.name, mime)),
|
||||
icon_class: intern_display(classes.icon_class),
|
||||
icon_special_class: intern_display(classes.icon_special_class),
|
||||
category: intern_display(classes.category),
|
||||
size_formatted: format_file_size(size_bytes),
|
||||
sort_date: None,
|
||||
content_hash,
|
||||
|
||||
Reference in New Issue
Block a user