DXF import: the symbol-name and layout tests count the work instead of timing it

many_by_block_looks_import_in_linear_time and
many_paper_layouts_are_read_in_linear_time checked for quadratic work by
timing the import (under 4 and 5 seconds), so a slow machine could fail
them and a regression that stayed under the bound passed. They now count
the work in test builds:

- the names `unique` tries for a new symbol: 4,000 symbols named by one
  block try fewer than 8,000 names (8,002,000 without the per-label
  suffix memo);
- the layout names the paper-layout de-duplication compares: 10,000
  layouts compare at most 10,000 (49,995,000 with a list in place of the
  set).

The counters are thread-local cells under cfg(test); release builds do
not count.
This commit is contained in:
dotCooCoo committed 2026-11-27 19:07:30 -08:00
1 parent 5d70217e0c
commit 61d4c573c1
3 files changed
+50 -9

No files matched your search

+15 -2
View File
@@ -708,16 +708,29 @@ fn rgb_color([r, g, b]: [u8; 3]) -> Color {
Color::rgb8(r, g, b)
}
#[cfg(test)]
thread_local! {
/// How many names [`unique`] tried on this thread (tests).
pub(crate) static SYMBOL_NAMES_TRIED: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}
/// Whether `taken` holds `name`, counted as one name tried (tests).
fn is_taken(taken: &HashSet<String>, name: &str) -> bool {
#[cfg(test)]
SYMBOL_NAMES_TRIED.with(|n| n.set(n.get().saturating_add(1)));
taken.contains(name)
}
/// `base`, or `base 2`, `base 3`… when taken; `next` remembers where each base left off.
fn unique(base: &str, next: &mut HashMap<String, usize>, taken: &HashSet<String>) -> String {
if !taken.contains(base) {
if !is_taken(taken, base) {
return base.to_string();
}
let i = next.entry(base.to_string()).or_insert(2);
loop {
let name = format!("{base} {i}");
*i += 1;
if !taken.contains(&name) {
if !is_taken(taken, &name) {
return name;
}
}
+27 -1
View File
@@ -175,6 +175,32 @@ fn layer(l: model::Layer) -> LayerDef {
/// drawings have a few dozen layouts with short names.
const MAX_LAYOUT_NAME_BYTES: usize = 4 << 20;
#[cfg(test)]
thread_local! {
/// How many times [`layouts`] compared two layout names on this thread (tests).
pub(crate) static LAYOUT_NAME_COMPARISONS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}
/// A paper layout's name as [`layouts`] de-duplicates it (lower case: names differing only in
/// ASCII case are one layout).
struct LayoutKey(String);
impl PartialEq for LayoutKey {
fn eq(&self, other: &Self) -> bool {
#[cfg(test)]
LAYOUT_NAME_COMPARISONS.with(|n| n.set(n.get().saturating_add(1)));
self.0 == other.0
}
}
impl Eq for LayoutKey {}
impl std::hash::Hash for LayoutKey {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
/// Model space, then each paper layout (its LAYOUT object, in tab order); a file without layout
/// objects but with paper-space art has one paper layout.
fn layouts(doc: &model::Document<'_>, block_names: &HashMap<u64, &str>) -> Vec<LayoutDef> {
@@ -187,7 +213,7 @@ fn layouts(doc: &model::Document<'_>, block_names: &HashMap<u64, &str>) -> Vec<L
continue;
}
let Some(block) = block else { continue };
let key = l.name.to_ascii_lowercase();
let key = LayoutKey(l.name.to_ascii_lowercase());
let cost = l.name.len() + block.len();
// A layout past the allowance is skipped alone: a later short one is still offered, and a
// skipped one reserves neither its name nor its bytes.
+8 -6
View File
@@ -743,15 +743,16 @@ fn many_by_block_looks_import_in_linear_time() {
inserts.extend([(0, "INSERT"), (2, "Door"), (420, colour), (10, "0"), (20, "0")]);
}
let text = with_door(by_block, &inserts);
let start = std::time::Instant::now();
build::SYMBOL_NAMES_TRIED.with(|t| t.set(0));
let doc = open(&text, &points()).document;
let took = start.elapsed();
let tried = build::SYMBOL_NAMES_TRIED.with(std::cell::Cell::get);
let mut names: Vec<&str> = doc.symbols.iter().map(|s| s.name.as_str()).collect();
assert_eq!(names.len(), n);
names.sort_unstable();
names.dedup();
assert_eq!(names.len(), n, "names stay unique");
assert!(took < std::time::Duration::from_secs(4), "import took {took:?}");
// Each symbol after the first tries its label and then the next free suffix.
assert!(tried < 2 * n, "{tried} names tried for {n} symbols");
}
#[test]
@@ -1151,12 +1152,13 @@ fn with_layouts(names: &[String]) -> Vec<u8> {
#[test]
fn many_paper_layouts_are_read_in_linear_time() {
let names: Vec<String> = (0..100_000).map(|i| format!("L{i}")).collect();
let names: Vec<String> = (0..10_000).map(|i| format!("L{i}")).collect();
let bytes = with_layouts(&names);
let t = std::time::Instant::now();
reader::LAYOUT_NAME_COMPARISONS.with(|c| c.set(0));
let info = info(&bytes).unwrap();
let compared = reader::LAYOUT_NAME_COMPARISONS.with(std::cell::Cell::get);
assert_eq!(info.layouts.len(), names.len() + 1);
assert!(t.elapsed() < std::time::Duration::from_secs(5), "layout de-duplication is quadratic: {:?}", t.elapsed());
assert!(compared <= names.len(), "layout de-duplication compared {compared} names for {} layouts", names.len());
}
#[test]