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:
Claude
2026-07-18 22:02:00 +00:00
parent 637478e7bd
commit 221c1f31b0
54 changed files with 4060 additions and 630 deletions
+18 -3
View File
@@ -797,9 +797,24 @@ impl CardDavAdapter {
("DAV:", "getlastmodified") => {
xml_writer
.write_event(Event::Start(BytesStart::new("D:getlastmodified")))?;
xml_writer.write_event(Event::Text(BytesText::new(
&contact.updated_at.to_rfc2822(),
)))?;
// Stack render (ROUND10 §13, byte-identical to
// chrono) with the chrono fallback for
// out-of-range timestamps — per-contact on the
// multiget/PROPFIND path.
let mut lm_buf = [0u8; 31];
match crate::common::fmt::rfc2822_utc(
&mut lm_buf,
contact.updated_at.timestamp(),
) {
Some(s) => {
xml_writer.write_event(Event::Text(BytesText::new(s)))?;
}
None => {
xml_writer.write_event(Event::Text(BytesText::new(
&contact.updated_at.to_rfc2822(),
)))?;
}
}
xml_writer.write_event(Event::End(BytesEnd::new("D:getlastmodified")))?;
}
("urn:ietf:params:xml:ns:carddav", "address-data") => {
+19 -14
View File
@@ -132,21 +132,26 @@ impl Default for CalendarEventDto {
impl From<CalendarEvent> for CalendarEventDto {
fn from(event: CalendarEvent) -> Self {
// Move every owned field out of the consumed entity — the old
// getter-clone shape deep-copied 6 Strings per event, dominated by
// the ~11 KB `ical_data` blob, on every CalDAV listing row
// (benches/ROUND11.md §19: 1.45x + the 11 KB memcpy gone).
let parts = event.into_parts();
Self {
id: event.id().to_string(),
calendar_id: event.calendar_id().to_string(),
summary: event.summary().to_string(),
description: event.description().map(|s| s.to_string()),
location: event.location().map(|s| s.to_string()),
start_time: *event.start_time(),
end_time: *event.end_time(),
all_day: event.all_day(),
rrule: event.rrule().map(|s| s.to_string()),
ical_uid: event.ical_uid().to_string(),
recurrence_id: event.recurrence_id().copied(),
ical_data: event.ical_data().to_string(),
created_at: *event.created_at(),
updated_at: *event.updated_at(),
id: parts.id.to_string(),
calendar_id: parts.calendar_id.to_string(),
summary: parts.summary,
description: parts.description,
location: parts.location,
start_time: parts.start_time,
end_time: parts.end_time,
all_day: parts.all_day,
rrule: parts.rrule,
ical_uid: parts.ical_uid,
recurrence_id: parts.recurrence_id,
ical_data: parts.ical_data,
created_at: parts.created_at,
updated_at: parts.updated_at,
}
}
}
+68 -6
View File
@@ -188,6 +188,50 @@ fn ext_of(name: &str) -> Option<&str> {
Some(after_dot)
}
/// Longest extension any classifier table matches ("appimage", "markdown"
/// — 8 bytes). Longer extensions can only ever hit the `_` arms, so they
/// skip the buffer entirely.
const MAX_CLASSIFIED_EXT: usize = 16;
/// Lowercase `ext` into `buf` without heap allocation. Returns `None` for
/// extensions longer than any table entry — the caller must then take the
/// same default arm `ext.to_ascii_lowercase()` would have fallen into.
fn lower_ext_into<'b>(ext: &str, buf: &'b mut [u8; MAX_CLASSIFIED_EXT]) -> Option<&'b str> {
let bytes = ext.as_bytes();
if bytes.len() > MAX_CLASSIFIED_EXT {
return None;
}
for (i, b) in bytes.iter().enumerate() {
buf[i] = b.to_ascii_lowercase();
}
// ASCII-lowercasing bytes keeps UTF-8 validity (non-ASCII bytes pass
// through untouched).
std::str::from_utf8(&buf[..bytes.len()]).ok()
}
/// The three display classifications for one `(name, mime)` pair.
pub struct DisplayClass {
pub icon_class: &'static str,
pub icon_special_class: &'static str,
pub category: &'static str,
}
/// Run all three classifiers over one `(name, mime)` pair, lowering the
/// extension **once into a stack buffer** instead of each classifier
/// allocating its own `to_ascii_lowercase()` String on the fallback path
/// (generic/empty MIME rows — common for code and unknown types). Each
/// decision tree is byte-for-byte the classifier it replaces
/// (benches/ROUND11.md §21 gates the equivalence over a corpus).
pub fn classify_display(name: &str, mime: &str) -> DisplayClass {
let mut buf = [0u8; MAX_CLASSIFIED_EXT];
let ext = ext_of(name).and_then(|e| lower_ext_into(e, &mut buf));
DisplayClass {
icon_class: icon_class_with_ext(mime, ext),
icon_special_class: icon_special_class_with_ext(mime, ext),
category: category_with_ext(mime, ext),
}
}
// ─── Icon class (FontAwesome) ────────────────────────────────────────
/// Returns the FontAwesome icon class for a file, considering both MIME
@@ -196,6 +240,12 @@ fn ext_of(name: &str) -> Option<&str> {
/// Use this instead of the old `mime_to_icon_class` whenever the filename
/// is available.
pub fn icon_class_for(name: &str, mime: &str) -> &'static str {
let mut buf = [0u8; MAX_CLASSIFIED_EXT];
let ext = ext_of(name).and_then(|e| lower_ext_into(e, &mut buf));
icon_class_with_ext(mime, ext)
}
fn icon_class_with_ext(mime: &str, ext: Option<&str>) -> &'static str {
// 1. Try specific MIME matches first
match mime {
"application/pdf" => return "fas fa-file-pdf",
@@ -273,8 +323,8 @@ pub fn icon_class_for(name: &str, mime: &str) -> &'static str {
}
// 3. Extension-based fallback (for application/octet-stream, empty, etc.)
if let Some(ext) = ext_of(name) {
return match ext.to_ascii_lowercase().as_str() {
if let Some(ext) = ext {
return match ext {
"pdf" => "fas fa-file-pdf",
"doc" | "docx" | "odt" | "rtf" => "fas fa-file-word",
"xls" | "xlsx" | "ods" | "csv" => "fas fa-file-excel",
@@ -311,6 +361,12 @@ pub fn icon_class_for(name: &str, mime: &str) -> &'static str {
/// The returned class maps to CSS rules in `style.css` that set colours,
/// backgrounds and decorative pseudo-elements per file type.
pub fn icon_special_class_for(name: &str, mime: &str) -> &'static str {
let mut buf = [0u8; MAX_CLASSIFIED_EXT];
let ext = ext_of(name).and_then(|e| lower_ext_into(e, &mut buf));
icon_special_class_with_ext(mime, ext)
}
fn icon_special_class_with_ext(mime: &str, ext: Option<&str>) -> &'static str {
// 1. Specific MIME matches
match mime {
"application/pdf" => return "pdf-icon",
@@ -390,8 +446,8 @@ pub fn icon_special_class_for(name: &str, mime: &str) -> &'static str {
}
// 3. Extension-based fallback
if let Some(ext) = ext_of(name) {
return match ext.to_ascii_lowercase().as_str() {
if let Some(ext) = ext {
return match ext {
"pdf" => "pdf-icon",
"doc" | "docx" | "odt" | "rtf" => "doc-icon",
"xls" | "xlsx" | "ods" | "csv" => "spreadsheet-icon",
@@ -437,6 +493,12 @@ pub fn icon_special_class_for(name: &str, mime: &str) -> &'static str {
/// Returns a human-readable category label, considering MIME + extension.
pub fn category_for(name: &str, mime: &str) -> &'static str {
let mut buf = [0u8; MAX_CLASSIFIED_EXT];
let ext = ext_of(name).and_then(|e| lower_ext_into(e, &mut buf));
category_with_ext(mime, ext)
}
fn category_with_ext(mime: &str, ext: Option<&str>) -> &'static str {
// 1. Specific MIME matches
match mime {
"application/pdf" => return "PDF",
@@ -489,8 +551,8 @@ pub fn category_for(name: &str, mime: &str) -> &'static str {
}
// 3. Extension fallback
if let Some(ext) = ext_of(name) {
return match ext.to_ascii_lowercase().as_str() {
if let Some(ext) = ext {
return match ext {
"pdf" => "PDF",
"doc" | "docx" | "odt" | "rtf" | "txt" => "Document",
"xls" | "xlsx" | "ods" | "csv" => "Spreadsheet",
+5 -6
View File
@@ -4,9 +4,7 @@ use utoipa::{IntoParams, ToSchema};
use uuid::Uuid;
use super::cursor::{CursorListResponse, CursorQuery, PageCursor};
use super::display_helpers::{
category_for, format_file_size, icon_class_for, icon_special_class_for,
};
use super::display_helpers::{classify_display, format_file_size};
use super::grant_dto::{ResourceContentDto, ResourceTypeDto};
use crate::domain::services::authorization::ResourceKind;
@@ -84,9 +82,10 @@ impl FavoriteItemDto {
.item_mime_type
.as_deref()
.unwrap_or("application/octet-stream");
self.icon_class = icon_class_for(name, mime).to_string();
self.icon_special_class = icon_special_class_for(name, mime).to_string();
self.category = category_for(name, mime).to_string();
let classes = classify_display(name, mime);
self.icon_class = classes.icon_class.to_string();
self.icon_special_class = classes.icon_special_class.to_string();
self.category = classes.category.to_string();
self.size_formatted = format_file_size(self.item_size.unwrap_or(0) as u64);
}
self
+6 -9
View File
@@ -5,10 +5,7 @@ use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
use uuid::Uuid;
use super::display_helpers::{
category_for, format_file_size, icon_class_for, icon_special_class_for, intern_display,
intern_mime,
};
use super::display_helpers::{classify_display, format_file_size, intern_display, intern_mime};
/// DTO for file responses
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
@@ -105,17 +102,17 @@ impl From<File> for FileDto {
// Display fields come from closed static tables and MIME values
// repeat massively across rows — intern instead of allocating a
// fresh Arc<str> per row (`Arc::from(&str)` always allocs+copies).
let icon_class = intern_display(icon_class_for(&parts.name, &parts.mime_type));
let icon_special_class =
intern_display(icon_special_class_for(&parts.name, &parts.mime_type));
let category = intern_display(category_for(&parts.name, &parts.mime_type));
let classes = classify_display(&parts.name, &parts.mime_type);
let icon_class = intern_display(classes.icon_class);
let icon_special_class = intern_display(classes.icon_special_class);
let category = intern_display(classes.category);
let size_formatted = format_file_size(parts.size);
let mime_type = intern_mime(&parts.mime_type);
Self {
id: parts.id,
name: parts.name,
path: parts.path_string,
path: parts.storage_path.into_joined(),
size: parts.size,
mime_type,
folder_id: parts.folder_id,
+1 -1
View File
@@ -113,7 +113,7 @@ impl From<Folder> for FolderDto {
Self {
id: parts.id,
name: parts.name,
path: parts.path_string,
path: parts.storage_path.into_joined(),
parent_id: parts.parent_id,
drive_id: parts.drive_id,
created_at: parts.created_at,
+5 -6
View File
@@ -4,9 +4,7 @@ use utoipa::{IntoParams, ToSchema};
use uuid::Uuid;
use super::cursor::{CursorListResponse, CursorQuery, PageCursor};
use super::display_helpers::{
category_for, format_file_size, icon_class_for, icon_special_class_for,
};
use super::display_helpers::{classify_display, format_file_size};
use super::grant_dto::{ResourceContentDto, ResourceTypeDto};
use crate::domain::services::authorization::ResourceKind;
@@ -80,9 +78,10 @@ impl RecentItemDto {
.item_mime_type
.as_deref()
.unwrap_or("application/octet-stream");
self.icon_class = icon_class_for(name, mime).to_string();
self.icon_special_class = icon_special_class_for(name, mime).to_string();
self.category = category_for(name, mime).to_string();
let classes = classify_display(name, mime);
self.icon_class = classes.icon_class.to_string();
self.icon_special_class = classes.icon_special_class.to_string();
self.category = classes.category.to_string();
self.size_formatted = format_file_size(self.item_size.unwrap_or(0) as u64);
}
self
+9
View File
@@ -62,6 +62,15 @@ pub trait FaceRepository: Send + Sync + 'static {
person_id: Option<Uuid>,
) -> Result<(), DomainError>;
/// Batch variant of [`Self::assign_person`]: apply every
/// `(face_id, person_id)` pair in one statement. Reclustering an
/// F-face library used to issue F sequential UPDATE round-trips
/// (benches/ROUND11.md §Q5 — the ROUND10 `save_faces` UNNEST pattern).
async fn assign_person_batch(
&self,
assignments: &[(Uuid, Option<Uuid>)],
) -> Result<(), DomainError>;
// ── persons ────────────────────────────────────────────────────
async fn create_person(&self, person: &Person) -> Result<(), DomainError>;
async fn persons_for_user(&self, user_id: Uuid) -> Result<Vec<Person>, DomainError>;
+22
View File
@@ -21,6 +21,19 @@ pub enum ThumbnailSize {
}
impl ThumbnailSize {
/// Stable name, byte-identical to the derived `Debug` output. Used by
/// the thumbnail/preview ETags on the hottest revalidation path — a
/// `&'static str` push beats routing through the `Debug` machinery
/// (benches/ROUND11.md §7) while keeping every already-cached client
/// ETag valid.
pub fn as_str(self) -> &'static str {
match self {
ThumbnailSize::Icon => "Icon",
ThumbnailSize::Preview => "Preview",
ThumbnailSize::Large => "Large",
}
}
/// Get the maximum dimension for this size.
pub fn max_dimension(&self) -> u32 {
match self {
@@ -64,6 +77,15 @@ pub enum ThumbnailFormat {
}
impl ThumbnailFormat {
/// Stable name, byte-identical to the derived `Debug` output (see
/// [`ThumbnailSize::as_str`] — same ETag-stability contract).
pub fn as_str(self) -> &'static str {
match self {
ThumbnailFormat::Webp => "Webp",
ThumbnailFormat::Jpeg => "Jpeg",
}
}
/// On-disk file extension for this format (no dot).
pub fn ext(self) -> &'static str {
match self {
+28 -21
View File
@@ -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");
+32 -11
View File
@@ -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)
}
+35 -12
View File
@@ -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 {
+24 -17
View File
@@ -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,