aba89c4f5d
Every change is benchmark-verified (harness + before/after numbers in benches/, measured on this branch; reproduction commands in each doc): DAV / sync-client hot paths - PROPFIND dead-properties: one = ANY($1) query per 500-child page instead of one sequential query per child, and indexable `=` predicates instead of IS NOT DISTINCT FROM (seq scans). 2,000-child folder: 1.07-4.54 s of DB chatter -> 4-6 ms (258-773x). Applied to native + NC PROPFIND and both NC REPORT handlers. [benches/DEAD-PROPS.md] - Folder paging: keyset cursor (name > $last) + new partial index (folder_id, name) replaces LIMIT/OFFSET full-folder rescan per page. Full 20k-file walk: 1266 ms -> 77 ms (16.5x). New migration 20260917000000. [benches/PROPFIND-PAGING.md] - NC chroot / default-drive resolution: moka caches (30 s TTL, explicit invalidation on drive mutations) for find_default_for_user and the markerless chroot FolderDto. 2 uncached queries + 2 pool checkouts per NC/WebDAV/WOPI request -> sub-us moka hit (p50 0.7-3.6 ms -> ~1 us). [benches/CHROOT-CACHE.md] - Quota: PROPFINDs whose prop list never names a quota prop skip the 2-query resolution entirely (wants_quota()); the remaining lookups read 2 columns instead of the full auth.users row with its <=512 KiB avatar (11-16x, p50 3.4 ms -> 0.29 ms). Same narrow read now gates every upload quota check. [benches/QUOTA-PATH.md] CPU on the request path - ZIP exports (folder download, share ZIP, batch download): entries whose MIME says already-compressed (JPEG/MP4/zip/pdf/...) are Stored instead of Deflate - deflate ran inline on the tokio writer task at ~41 MB/s for ~0% size gain. Mixed media corpus: 4.31x wall and CPU, archive size unchanged. Shared predicate in common::mime_detect. [benches/ZIP-MEDIA.md] - Compression layers: tower-http's default maps to Brotli QUALITY 11 (verified in brotli-8.0.2 source and empirically: 90 ms per 64 KiB JSON response, 1.3 s per 700 KiB bundle). Both layers pinned to Precise(4): 99x less CPU for ~15% more bytes. SPA assets are now precompressed at build time (scripts/precompress.mjs, 77% smaller) and served via ServeDir::precompressed_br/gzip: 2016x less per-request work, and clients get the better q11 bytes. [benches/STATIC-PRECOMPRESSED.md] Batched / cached backend paths [benches/NPLUS1-AND-CACHES.md] - Content-search ReBAC re-verification: new AuthorizationEngine::check_files_read_batch (default = old loop; PgAclEngine override batches drive resolution + reuses role cache). 200 sequential point SELECTs per search -> 1-2 queries. - Batch-ZIP subtree downloads: drop per-file re-authz + per-file Recent recording (2 writes/file) for subtree entries already authorized at the root - mirrors the native folder-download path. ~6,000 statements removed from a 2,000-file archive. - CDC chunk manifests: immutable by content address, now moka-cached (weight-bounded 32 MiB, 60 s TTL, positive-only, invalidated on delete) - removes one manifest query (p50 0.44-4.4 ms) from every stream, range and full blob read. - People tab: grouped COUNT + batched cover lookup instead of dragging every face row with its 2 KiB embedding (10k faces: 30.4 ms & 21 MB -> 3.8 ms & 1.3 KB, 8.1x); merge() is one set-based UPDATE. [benches/PEOPLE-LIST.md] - Photos timeline cursor: raw timestamptz comparison instead of EXTRACT(EPOCH ...) wrapper + IS NULL OR disjunction - cursor is an index boundary again, deep scroll stops re-scanning skipped rows. - Public share landing: one atomic UPDATE ... access_count + 1 (was SELECT + full-row write-back: racy, lost updates, clobbered concurrent owner edits) - 3 round-trips -> 2 per visit. - move_to_trash: dead full-entity SELECT feeding a documented no-op removed from both branches; dead fields dropped from TrashService. - NFC normalization: is_nfc_quick fast path skips the decompose/recompose state machine for the ~100% already-NFC case (every row loaded from PG). Frontend - Large folders paint after page one (~200 items) via fetchFolderListing's new onPage hook instead of waiting for every sequential page. - Tested-and-reverted (kept for the record): cached Intl.Collator for name sorts - vitest showed it 2x SLOWER than V8's argument-less localeCompare fast path (5.6 ms vs 12.1 ms / 5k names). Sort order untouched. New bench harnesses under examples/ (bench feature): zip_media, dead_props, chroot_cache, quota_path, people_list, propfind_paging, static_precompress. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CBK1RdtzyP6759Muqe1K1w
287 lines
10 KiB
Rust
287 lines
10 KiB
Rust
//! WebDAV dead-properties fetch benchmark — per-child N+1 vs batched ANY($1).
|
|
//!
|
|
//! The streaming PROPFIND walker (`webdav_handler.rs`) fetches dead properties
|
|
//! ONE CHILD AT A TIME, sequentially, for every Depth:1 listing page:
|
|
//!
|
|
//! for file in &batch { file_deads.push(store.get_all(File(id)).await) }
|
|
//!
|
|
//! and `DeadPropertyStore::get_all` filters with
|
|
//! `folder_id IS NOT DISTINCT FROM $1 AND file_id IS NOT DISTINCT FROM $2`,
|
|
//! which PostgreSQL cannot serve from a B-tree index (IS NOT DISTINCT FROM is
|
|
//! not an indexable operator) — so each of the N sequential round-trips also
|
|
//! degrades to a seq scan as the table grows.
|
|
//!
|
|
//! This bench isolates exactly the dead-prop portion of a Depth:1 PROPFIND of
|
|
//! a folder with N children, comparing the three query shapes:
|
|
//!
|
|
//! OLD — N sequential `IS NOT DISTINCT FROM` queries (production today)
|
|
//! EQ — N sequential plain `file_id = $1` queries (indexable, still N+1)
|
|
//! BATCH — ⌈N/500⌉ `file_id = ANY($1)` queries (one per PROPFIND page)
|
|
//!
|
|
//! Two table sizes are measured: the seeded-children-only table and one with
|
|
//! extra noise rows (dead props on other resources), which is where the
|
|
//! seq-scan cost of OLD shows up.
|
|
//!
|
|
//! Run (needs Postgres up; reads DATABASE_URL from .env):
|
|
//! cargo run --release --features bench --example bench_dead_props
|
|
//! Tunables (env): BENCH_CHILDREN (2000), BENCH_PAGE (500 = PROPFIND_BATCH_SIZE),
|
|
//! BENCH_NOISE_ROWS (20000), BENCH_REPS (5).
|
|
|
|
use std::env;
|
|
use std::time::Instant;
|
|
|
|
use sqlx::postgres::PgPoolOptions;
|
|
use sqlx::{PgPool, Row};
|
|
use uuid::Uuid;
|
|
|
|
fn env_or<T: std::str::FromStr>(key: &str, default: T) -> T {
|
|
env::var(key)
|
|
.ok()
|
|
.and_then(|v| v.parse().ok())
|
|
.unwrap_or(default)
|
|
}
|
|
|
|
struct Seeded {
|
|
drive_id: Uuid,
|
|
file_ids: Vec<Uuid>,
|
|
}
|
|
|
|
async fn seed(pool: &PgPool, children: usize, noise: usize) -> Seeded {
|
|
// Drive (kind 'shared' needs no user FK) → root folder → N files → props.
|
|
// The root folder + drive.root_folder_id must land in ONE transaction:
|
|
// trg_no_orphan_root_folder is INITIALLY DEFERRED and checks at commit.
|
|
let mut tx = pool.begin().await.expect("begin seed tx");
|
|
let drive_id: Uuid = sqlx::query_scalar(
|
|
"INSERT INTO storage.drives (kind, quota_bytes) VALUES ('shared', NULL) RETURNING id",
|
|
)
|
|
.fetch_one(&mut *tx)
|
|
.await
|
|
.expect("seed drive");
|
|
|
|
let folder_id: Uuid = sqlx::query_scalar(
|
|
"INSERT INTO storage.folders (name, path, lpath, drive_id)
|
|
VALUES ('bench_dead_props', '/bench_dead_props', 'bench_dead_props', $1)
|
|
RETURNING id",
|
|
)
|
|
.bind(drive_id)
|
|
.fetch_one(&mut *tx)
|
|
.await
|
|
.expect("seed folder");
|
|
|
|
sqlx::query("UPDATE storage.drives SET root_folder_id = $1 WHERE id = $2")
|
|
.bind(folder_id)
|
|
.bind(drive_id)
|
|
.execute(&mut *tx)
|
|
.await
|
|
.expect("stamp root_folder_id");
|
|
tx.commit().await.expect("commit seed tx");
|
|
|
|
// Children of the PROPFIND'd folder, one dead prop each.
|
|
let file_ids: Vec<Uuid> = sqlx::query_scalar(
|
|
"INSERT INTO storage.files (name, folder_id, blob_hash, size, mime_type, drive_id)
|
|
SELECT 'f' || i, $1, 'benchdead000000000000000000000000000000000000000000000000000000',
|
|
1024, 'image/jpeg', $2
|
|
FROM generate_series(1, $3) AS i
|
|
RETURNING id",
|
|
)
|
|
.bind(folder_id)
|
|
.bind(drive_id)
|
|
.bind(children as i32)
|
|
.fetch_all(pool)
|
|
.await
|
|
.expect("seed files");
|
|
|
|
sqlx::query(
|
|
"INSERT INTO storage.webdav_dead_properties (file_id, namespace, local_name, value)
|
|
SELECT id, 'urn:bench', 'displayname', 'bench value'
|
|
FROM storage.files WHERE folder_id = $1",
|
|
)
|
|
.bind(folder_id)
|
|
.execute(pool)
|
|
.await
|
|
.expect("seed dead props");
|
|
|
|
// Noise: dead props attached to OTHER files (a second folder) so the
|
|
// table has realistic volume — this is what OLD's seq scans pay for.
|
|
if noise > 0 {
|
|
// Child of the main folder — root folders need the deferred
|
|
// four-write dance, children don't.
|
|
let noise_folder: Uuid = sqlx::query_scalar(
|
|
"INSERT INTO storage.folders (name, parent_id, path, lpath, drive_id)
|
|
VALUES ('noise', $2, '/bench_dead_props/noise', 'bench_dead_props.noise', $1)
|
|
RETURNING id",
|
|
)
|
|
.bind(drive_id)
|
|
.bind(folder_id)
|
|
.fetch_one(pool)
|
|
.await
|
|
.expect("seed noise folder");
|
|
sqlx::query(
|
|
"WITH f AS (
|
|
INSERT INTO storage.files (name, folder_id, blob_hash, size, mime_type, drive_id)
|
|
SELECT 'n' || i, $1, 'benchdead000000000000000000000000000000000000000000000000000000',
|
|
1024, 'image/jpeg', $2
|
|
FROM generate_series(1, $3) AS i
|
|
RETURNING id
|
|
)
|
|
INSERT INTO storage.webdav_dead_properties (file_id, namespace, local_name, value)
|
|
SELECT id, 'urn:bench', 'noise', 'x' FROM f",
|
|
)
|
|
.bind(noise_folder)
|
|
.bind(drive_id)
|
|
.bind(noise as i32)
|
|
.execute(pool)
|
|
.await
|
|
.expect("seed noise props");
|
|
}
|
|
|
|
sqlx::query("ANALYZE storage.webdav_dead_properties")
|
|
.execute(pool)
|
|
.await
|
|
.ok();
|
|
|
|
Seeded { drive_id, file_ids }
|
|
}
|
|
|
|
async fn cleanup(pool: &PgPool, drive_id: Uuid) {
|
|
// drives → folders/files → dead props all cascade.
|
|
let _ = sqlx::query("DELETE FROM storage.drives WHERE id = $1")
|
|
.bind(drive_id)
|
|
.execute(pool)
|
|
.await;
|
|
}
|
|
|
|
/// OLD: production `get_all` shape — sequential, IS NOT DISTINCT FROM.
|
|
async fn run_old(pool: &PgPool, ids: &[Uuid]) -> usize {
|
|
let mut rows_seen = 0;
|
|
for id in ids {
|
|
let rows = sqlx::query(
|
|
"SELECT namespace, local_name, value
|
|
FROM storage.webdav_dead_properties
|
|
WHERE folder_id IS NOT DISTINCT FROM $1
|
|
AND file_id IS NOT DISTINCT FROM $2",
|
|
)
|
|
.bind(Option::<Uuid>::None)
|
|
.bind(Some(*id))
|
|
.fetch_all(pool)
|
|
.await
|
|
.expect("old get_all");
|
|
rows_seen += rows.len();
|
|
}
|
|
rows_seen
|
|
}
|
|
|
|
/// EQ: still N sequential round-trips, but with an indexable `=` predicate.
|
|
async fn run_eq(pool: &PgPool, ids: &[Uuid]) -> usize {
|
|
let mut rows_seen = 0;
|
|
for id in ids {
|
|
let rows = sqlx::query(
|
|
"SELECT namespace, local_name, value
|
|
FROM storage.webdav_dead_properties
|
|
WHERE file_id = $1",
|
|
)
|
|
.bind(*id)
|
|
.fetch_all(pool)
|
|
.await
|
|
.expect("eq get_all");
|
|
rows_seen += rows.len();
|
|
}
|
|
rows_seen
|
|
}
|
|
|
|
/// BATCH: one `= ANY($1)` query per PROPFIND page of 500 children.
|
|
async fn run_batch(pool: &PgPool, ids: &[Uuid], page: usize) -> usize {
|
|
let mut rows_seen = 0;
|
|
for chunk in ids.chunks(page) {
|
|
let rows = sqlx::query(
|
|
"SELECT file_id, namespace, local_name, value
|
|
FROM storage.webdav_dead_properties
|
|
WHERE file_id = ANY($1)",
|
|
)
|
|
.bind(chunk)
|
|
.fetch_all(pool)
|
|
.await
|
|
.expect("batch get_all");
|
|
// Decode file_id like the real batched store method will (map key).
|
|
for row in &rows {
|
|
let _: Uuid = row.get("file_id");
|
|
}
|
|
rows_seen += rows.len();
|
|
}
|
|
rows_seen
|
|
}
|
|
|
|
fn median(mut xs: Vec<f64>) -> f64 {
|
|
xs.sort_by(|a, b| a.partial_cmp(b).unwrap());
|
|
xs[xs.len() / 2]
|
|
}
|
|
|
|
#[tokio::main(flavor = "multi_thread")]
|
|
async fn main() {
|
|
dotenvy::dotenv().ok();
|
|
let url = env::var("DATABASE_URL")
|
|
.or_else(|_| env::var("OXICLOUD_DB_CONNECTION_STRING"))
|
|
.expect("set DATABASE_URL — the dev Postgres URL");
|
|
|
|
let children: usize = env_or("BENCH_CHILDREN", 2000);
|
|
let page: usize = env_or("BENCH_PAGE", 500);
|
|
let noise: usize = env_or("BENCH_NOISE_ROWS", 20_000);
|
|
let reps: usize = env_or("BENCH_REPS", 5);
|
|
|
|
let pool = PgPoolOptions::new()
|
|
.max_connections(5)
|
|
.min_connections(5)
|
|
.connect(&url)
|
|
.await
|
|
.expect("connect Postgres");
|
|
|
|
for &with_noise in &[false, true] {
|
|
let n = if with_noise { noise } else { 0 };
|
|
let seeded = seed(&pool, children, n).await;
|
|
let total_rows: i64 =
|
|
sqlx::query_scalar("SELECT count(*) FROM storage.webdav_dead_properties")
|
|
.fetch_one(&pool)
|
|
.await
|
|
.unwrap_or(0);
|
|
|
|
println!("\n== folder with {children} children, dead-props table = {total_rows} rows ==");
|
|
println!(
|
|
"{:<28} {:>10} {:>12} {:>9}",
|
|
"mode", "queries", "total ms", "vs OLD"
|
|
);
|
|
|
|
let mut base = None;
|
|
for (label, queries) in [
|
|
("OLD seq, IS NOT DISTINCT", children),
|
|
("EQ seq, file_id = $1", children),
|
|
("BATCH file_id = ANY, /page", children.div_ceil(page)),
|
|
] {
|
|
let mut times = Vec::with_capacity(reps);
|
|
let mut rows = 0;
|
|
for _ in 0..reps {
|
|
let t = Instant::now();
|
|
rows = match label.split_whitespace().next().unwrap() {
|
|
"OLD" => run_old(&pool, &seeded.file_ids).await,
|
|
"EQ" => run_eq(&pool, &seeded.file_ids).await,
|
|
_ => run_batch(&pool, &seeded.file_ids, page).await,
|
|
};
|
|
times.push(t.elapsed().as_secs_f64() * 1000.0);
|
|
}
|
|
assert_eq!(rows, children, "each child has exactly 1 dead prop");
|
|
let ms = median(times);
|
|
let speedup = base
|
|
.map(|b: f64| format!("{:.1}x", b / ms))
|
|
.unwrap_or_else(|| "1.0x".into());
|
|
if base.is_none() {
|
|
base = Some(ms);
|
|
}
|
|
println!("{label:<28} {queries:>10} {ms:>12.2} {speedup:>9}");
|
|
}
|
|
|
|
cleanup(&pool, seeded.drive_id).await;
|
|
}
|
|
|
|
println!("\n(total ms = the dead-prop portion of one Depth:1 PROPFIND of the folder,");
|
|
println!(" i.e. what the walker adds on top of the file/folder listing queries)");
|
|
}
|