mirror of
https://github.com/storytold/wordcraft.git
synced 2026-10-29 21:07:24 -06:00
The layout's linear-time tests (#3866) asserted CPU-time ratios, which a busy CI machine can skew. Each walk that once ran again for every item it served (a line's clusters, a page's items, the revision authors, the sections, a table's rows, a run's glyphs, the clipped cells) now counts the elements it looks at in a test-only counter (crate::work, nothing outside tests), and the tests assert 8x the input takes at most 12x the steps (quadratic: ~64x). proof_text, whose walk is the document model's, stays timed, and a ratio over the limit is measured twice more before it fails. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CX1hMKX28764Lrx5moxs1o
30 lines
1.1 KiB
Rust
30 lines
1.1 KiB
Rust
//! A count of the steps the layout's lookups take, for the linear-time tests (#3692): they check
|
|
//! that an input 8x the size costs about 8x the steps, which a busy machine can't skew the way it
|
|
//! skews a timing. Each walk that once ran again for every item it served (a line's clusters, a
|
|
//! page's items, the revision authors, the sections, a table's rows) counts the elements it
|
|
//! looks at, so a caller that walks it once per item again shows up as a quadratic count. Outside
|
|
//! tests the count is nothing.
|
|
|
|
#[cfg(test)]
|
|
thread_local! {
|
|
/// The steps counted on this thread (layout runs on the thread that calls it).
|
|
static STEPS: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
|
|
}
|
|
|
|
/// Count `n` steps of a walk (nothing outside tests).
|
|
#[inline(always)]
|
|
pub(crate) fn steps(n: usize) {
|
|
#[cfg(test)]
|
|
STEPS.with(|s| s.set(s.get().saturating_add(n as u64)));
|
|
#[cfg(not(test))]
|
|
let _ = n;
|
|
}
|
|
|
|
/// The steps `f` takes.
|
|
#[cfg(test)]
|
|
pub(crate) fn count(f: impl FnOnce()) -> u64 {
|
|
let before = STEPS.with(std::cell::Cell::get);
|
|
f();
|
|
STEPS.with(std::cell::Cell::get).saturating_sub(before)
|
|
}
|