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).
This commit is contained in:
objz committed 2026-08-24 19:14:49 +02:00
1 parent 7f07c7636c
commit cc3b783ab1
2 files changed
+33 -11

No files matched your search

+26 -10
View File
@@ -1,3 +1,6 @@
// SPDX-FileCopyrightText: 2026 Constantin Bauer
// SPDX-License-Identifier: GPL-3.0-only
use std::collections::{BTreeMap, HashMap}; use std::collections::{BTreeMap, HashMap};
use std::io::{Read, Seek}; use std::io::{Read, Seek};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@@ -121,10 +124,15 @@ impl ContentManifest {
return Ok(Self::default()); return Ok(Self::default());
} }
let bytes = std::fs::read(path)?; 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 { if manifest.version != MANIFEST_VERSION {
return Err(ManifestError::UnsupportedVersion(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) Ok(manifest)
} }
@@ -158,23 +166,31 @@ impl ContentManifest {
Ok(result) 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> { pub fn record(&self, relative_path: &Path) -> Option<&ContentFileRecord> {
let index = self
.files
.partition_point(|record| record.relative_path < relative_path);
self.files self.files
.iter() .get(index)
.find(|record| record.relative_path == relative_path) .filter(|record| record.relative_path == relative_path)
} }
pub fn upsert(&mut self, record: ContentFileRecord) { pub fn upsert(&mut self, record: ContentFileRecord) {
if let Some(existing) = self let index = self
.files .files
.iter_mut() .partition_point(|existing| existing.relative_path < record.relative_path);
.find(|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 { } else {
self.files.push(record); // insert at the sorted position instead of re-sorting the whole
self.files // vec; bulk callers that push pre-sorted records get O(1) each.
.sort_by(|left, right| left.relative_path.cmp(&right.relative_path)); self.files.insert(index, record);
} }
} }
+7 -1
View File
@@ -1,3 +1,6 @@
// SPDX-FileCopyrightText: 2026 Constantin Bauer
// SPDX-License-Identifier: GPL-3.0-only
use std::collections::{HashMap, HashSet, VecDeque}; use std::collections::{HashMap, HashSet, VecDeque};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::{Arc, LazyLock, Mutex}; use std::sync::{Arc, LazyLock, Mutex};
@@ -428,7 +431,10 @@ fn reconcile_inventory(
fingerprint: record.fingerprint.clone(), 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); task.set_progress(index as u64 + 1, file_count);
} }
Ok(Inventory { manifest, queries }) Ok(Inventory { manifest, queries })