From cc3b783ab1e4d529c9ba1dc842923e9fd3a67c2a Mon Sep 17 00:00:00 2001 From: objz Date: Mon, 24 Aug 2026 19:14:49 +0200 Subject: [PATCH] perf(content): replace manifest linear scans with binary search record() and upsert() use partition_point on the path-sorted file list and reconcile_inventory appends records in order instead of upserting each one, dropping full rescans from O(n^2) to O(n log n). --- src/instance/content/manifest.rs | 36 ++++++++++++++++++++++--------- src/instance/content/reconcile.rs | 8 ++++++- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/instance/content/manifest.rs b/src/instance/content/manifest.rs index 296aca0..e18aa08 100644 --- a/src/instance/content/manifest.rs +++ b/src/instance/content/manifest.rs @@ -1,3 +1,6 @@ +// SPDX-FileCopyrightText: 2026 Constantin Bauer +// SPDX-License-Identifier: GPL-3.0-only + use std::collections::{BTreeMap, HashMap}; use std::io::{Read, Seek}; use std::path::{Path, PathBuf}; @@ -121,10 +124,15 @@ impl ContentManifest { return Ok(Self::default()); } let bytes = std::fs::read(path)?; - let manifest: Self = serde_json::from_slice(&bytes)?; + let mut manifest: Self = serde_json::from_slice(&bytes)?; if manifest.version != MANIFEST_VERSION { return Err(ManifestError::UnsupportedVersion(manifest.version)); } + // record()/upsert() rely on files being sorted by relative_path; + // normalize here so hand-edited manifests can't break the lookups. + manifest + .files + .sort_by(|left, right| left.relative_path.cmp(&right.relative_path)); Ok(manifest) } @@ -158,23 +166,31 @@ impl ContentManifest { Ok(result) } + // binary search: files is kept sorted by relative_path (see load/upsert), + // so per-file lookups stay O(log n) even for large manifests. pub fn record(&self, relative_path: &Path) -> Option<&ContentFileRecord> { + let index = self + .files + .partition_point(|record| record.relative_path < relative_path); self.files - .iter() - .find(|record| record.relative_path == relative_path) + .get(index) + .filter(|record| record.relative_path == relative_path) } pub fn upsert(&mut self, record: ContentFileRecord) { - if let Some(existing) = self + let index = self .files - .iter_mut() - .find(|existing| existing.relative_path == record.relative_path) + .partition_point(|existing| existing.relative_path < record.relative_path); + if self + .files + .get(index) + .is_some_and(|existing| existing.relative_path == record.relative_path) { - *existing = record; + self.files[index] = record; } else { - self.files.push(record); - self.files - .sort_by(|left, right| left.relative_path.cmp(&right.relative_path)); + // insert at the sorted position instead of re-sorting the whole + // vec; bulk callers that push pre-sorted records get O(1) each. + self.files.insert(index, record); } } diff --git a/src/instance/content/reconcile.rs b/src/instance/content/reconcile.rs index c71832f..033ed74 100644 --- a/src/instance/content/reconcile.rs +++ b/src/instance/content/reconcile.rs @@ -1,3 +1,6 @@ +// SPDX-FileCopyrightText: 2026 Constantin Bauer +// SPDX-License-Identifier: GPL-3.0-only + use std::collections::{HashMap, HashSet, VecDeque}; use std::path::{Path, PathBuf}; use std::sync::{Arc, LazyLock, Mutex}; @@ -428,7 +431,10 @@ fn reconcile_inventory( fingerprint: record.fingerprint.clone(), }); } - manifest.upsert(record); + // content_files() yields paths in sorted order and stripping the + // common prefix preserves that order, so appending keeps manifest + // files sorted without an O(log n) lookup per record. + manifest.files.push(record); task.set_progress(index as u64 + 1, file_count); } Ok(Inventory { manifest, queries })