From c1cd6131d13a40f10f0e6cefc2c9c0da9d8c5eb5 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 00:27:36 -0800 Subject: [PATCH] SVG import: complexity tests count the work done instead of timing it Two SVG import tests asserted elapsed time (1 s and 5 s), so a loaded machine could fail them and a slower but still bounded run could pass. - prepass.rs: a test-only counter adds up the numbers Edits::id tries for a made-up id. The test asserts 55,000 tries for 5,000 ids made up past 50,000 taken ones. With each id starting again at the number of ids made so far, as before the fix it covers, the count is 237,507,500 and the assertion fails. - entities.rs: the run of 1 MiB of `&` keeps its check that nothing is expanded and drops the timing. The scan is craft_svg::guard in craft-libs, whose own test checks the linear read. --- crates/svg/src/entities.rs | 5 ++--- crates/svg/src/import/prepass.rs | 21 ++++++++++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/crates/svg/src/entities.rs b/crates/svg/src/entities.rs index e80622ae42..d74f02135d 100644 --- a/crates/svg/src/entities.rs +++ b/crates/svg/src/entities.rs @@ -23,11 +23,10 @@ mod tests { let ns = "]>"; assert!(!expansion_too_large(ns)); assert!(!expansion_too_large("")); - // Many `&`s with no `;` after them are read in linear time. + // Many `&`s with no `;` after them expand to nothing. The guard's own tests in craft-libs + // check that it reads them in linear time. let ampersands = format!("]>", "&".repeat(1 << 20)); - let start = std::time::Instant::now(); assert!(!expansion_too_large(&ersands)); - assert!(start.elapsed() < std::time::Duration::from_secs(5), "{:?}", start.elapsed()); } #[test] diff --git a/crates/svg/src/import/prepass.rs b/crates/svg/src/import/prepass.rs index e541963561..54e830df0f 100644 --- a/crates/svg/src/import/prepass.rs +++ b/crates/svg/src/import/prepass.rs @@ -56,6 +56,19 @@ pub(crate) struct Edits { styles: HashMap, } +#[cfg(test)] +thread_local! { + /// The numbers [`Edits::id`] has tried for a made-up id on this thread, taken or not: the + /// work of making up ids. + static TRIED: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// Count one number tried for a made-up id in [`TRIED`]; nothing outside tests. +fn tried() { + #[cfg(test)] + TRIED.set(TRIED.get() + 1); +} + impl Edits { fn insert(&mut self, at: usize, text: String) { self.list.push((at..at, text)); @@ -71,8 +84,10 @@ impl Edits { return id.clone(); } let taken = self.taken.get_or_insert_with(|| named_made_up_ids(n.document())); + tried(); while taken.contains(&self.next) { self.next += 1; + tried(); } let id = format!("{MADE_UP_ID}{}", self.next); self.next += 1; @@ -491,7 +506,7 @@ mod tests { use super::*; /// A file that already holds the made-up ids does not make each new id try them all again: - /// 5,000 ids made up past 50,000 taken ones take a moment, not 250 million tries. + /// 5,000 ids made up past 50,000 taken ones try 55,000 numbers, not 250 million. #[test] fn made_up_ids_skip_the_taken_ones_once() { let taken: String = (0..50_000).map(|i| format!("")).collect(); @@ -500,9 +515,9 @@ mod tests { let doc = roxmltree::Document::parse(&svg).unwrap(); let rects: Vec = doc.descendants().filter(|n| n.has_tag_name("rect")).collect(); let mut edits = Edits::default(); - let start = std::time::Instant::now(); + TRIED.set(0); let ids: Vec = rects.iter().map(|n| edits.id(&svg, *n)).collect(); - assert!(start.elapsed() < std::time::Duration::from_secs(1), "{:?}", start.elapsed()); + assert_eq!(TRIED.get(), 55_000); assert_eq!(ids.first().map(String::as_str), Some(format!("{MADE_UP_ID}50000").as_str())); assert_eq!(ids.iter().collect::>().len(), ids.len()); }