open-standards/wai/wai-vector/tests/peak_alloc.rs
Transaction Science 2f147d2a30
Some checks failed
standards workspaces build + test / jouleclaw-rs (pull_request) Successful in 2m13s
standards workspaces build + test / aac-rs (pull_request) Successful in 1m58s
standards workspaces build + test / joule-code-rs (pull_request) Successful in 50s
standards workspaces build + test / joulecontract-rs (pull_request) Successful in 34s
standards workspaces build + test / map-rs (pull_request) Successful in 31s
standards workspaces build + test / mesh-rs (pull_request) Successful in 31s
no-vanity-metrics / Scan committed Markdown for vanity metrics (pull_request) Has been cancelled
no-vanity-metrics / WAI public text names no comparison (pull_request) Has been cancelled
no-vanity-metrics / WAI public text names no company or product (pull_request) Has been cancelled
standards workspaces build + test / arl-rs (pull_request) Has been cancelled
standards workspaces build + test / eoc-rs (pull_request) Has been cancelled
standards workspaces build + test / jcp-rs (pull_request) Has been cancelled
standards workspaces build + test / joule-ui-rs (pull_request) Has been cancelled
standards workspaces build + test / jouledesk-rs (pull_request) Has been cancelled
standards workspaces build + test / joulehook-rs (pull_request) Has been cancelled
standards workspaces build + test / proof-rs (pull_request) Has been cancelled
standards workspaces build + test / sandbox-rs (pull_request) Has been cancelled
standards workspaces build + test / sense-rs (pull_request) Has been cancelled
standards workspaces build + test / smart-byte-rs (pull_request) Has been cancelled
standards workspaces build + test / macOS energy backends (pull_request) Has been cancelled
WAI conformance / cargo-class corpora · linux (pull_request) Has been cancelled
WAI conformance / cargo-class corpora · linux-arm64 (pull_request) Has been cancelled
WAI conformance / cargo-class corpora · macos (pull_request) Has been cancelled
WAI conformance / quantum core and corpus under wasm32-wasip2 (wasmtime) (pull_request) Has been cancelled
WAI conformance / v2 vocabulary · manifests validate as stated (pull_request) Has been cancelled
WAI conformance / independent verifiers · claims, JWP, key discovery and integer payloads (Python) (pull_request) Has been cancelled
WAI conformance / delivery telemetry · CMCD and CMSD against an independent library (pull_request) Has been cancelled
WAI conformance / cross-arch byte-equality · linux (pull_request) Has been cancelled
WAI conformance / cross-arch byte-equality · linux-arm64 (pull_request) Has been cancelled
WAI conformance / cross-arch byte-equality · macos (pull_request) Has been cancelled
WAI conformance / C2PA manifest emission · signed, read back, validated (pull_request) Has been cancelled
WAI energy receipts / energy receipts · linux (pull_request) Has been cancelled
WAI energy receipts / energy receipts · linux-arm64 (pull_request) Has been cancelled
WAI energy receipts / energy receipts · macos (pull_request) Has been cancelled
WAI section references / cited sections exist (pull_request) Has been cancelled
wai-vector vector graphics / linux-aarch64 (pull_request) Has been cancelled
wai-vector vector graphics / linux-x86_64 (pull_request) Has been cancelled
wai-vector vector graphics / macos-arm64 (pull_request) Has been cancelled
wai-vector vector graphics / wasm32-wasip2 (wasmtime) (pull_request) Has been cancelled
wai-vector vector graphics / wasm32 under Node (pull_request) Has been cancelled
wai-vector vector graphics / fuzz, peak allocation, comparison count (pull_request) Has been cancelled
workflow hygiene / every workflow supersedes its own queue (pull_request) Has been cancelled
standards workspaces build + test / openpay (pull_request) Has been cancelled
wai-vector: WOS1 and WVG1 with the refusal order of §2.7, the clock, tracks, the interval check and the cost (D7-2)
The reference crate of wai.graphics.vector (no_std + alloc, no
dependencies; SHA-256 is wai-ops's, by #[path]):
- WOS1 parsed with §2.1's load checks in order, and written;
- WVG1 split (steps 1-3, nothing allocated before the exact length),
  the record pass (step 4), the cross-record checks (step 5(a)-(h)),
  the sets by digest with at most length + 1 bytes read (step 6), the
  glyphs (7), the interval check (8) and the per-frame cost (9);
- the clock with the frame range by division, track evaluation, and
  placement;
- the JSON authoring form, one member per field, packing back to the
  same bytes;
- the CLI: check, cost, pack, unpack, vectors gen and verify;
- a wasm32 module and verify.mjs for Node.

The corpus wai/vector-conformance/ (arith, canonical, refusals,
geometry, cost) with its README's provenance line and a sha256.json
sidecar naming the div255 table; wai-vector.yml with native, wasip2,
node and fuzz jobs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-11 03:59:47 -04:00

72 lines
2.5 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! Parse memory is O(input) plus fixed: nothing is reserved by a header's counts until
//! the exact length shows the bytes back them (§2.0, §2.7 steps 2–3).
//!
//! One test in its own binary, so the counting allocator sees only it.
use std::alloc::{GlobalAlloc, Layout, System};
use std::sync::atomic::{AtomicUsize, Ordering};
use wai_vector::vectors::{all_vectors, corpus_store};
use wai_vector::wvg1::{self, Header};
struct Counting;
static NOW: AtomicUsize = AtomicUsize::new(0);
static PEAK: AtomicUsize = AtomicUsize::new(0);
unsafe impl GlobalAlloc for Counting {
unsafe fn alloc(&self, l: Layout) -> *mut u8 {
let p = unsafe { System.alloc(l) };
if !p.is_null() {
let n = NOW.fetch_add(l.size(), Ordering::SeqCst) + l.size();
PEAK.fetch_max(n, Ordering::SeqCst);
}
p
}
unsafe fn dealloc(&self, p: *mut u8, l: Layout) {
unsafe { System.dealloc(p, l) };
NOW.fetch_sub(l.size(), Ordering::SeqCst);
}
}
#[global_allocator]
static A: Counting = Counting;
/// The peak bytes allocated while `f` runs, above what was live before it.
fn peak(f: impl FnOnce()) -> usize {
let base = NOW.load(Ordering::SeqCst);
PEAK.store(base, Ordering::SeqCst);
f();
PEAK.load(Ordering::SeqCst) - base
}
const C: usize = 6;
const FIXED: usize = 64 << 10;
#[test]
fn peak_allocation_is_linear_in_the_input() {
let mut store = corpus_store();
let held: usize = wai_vector::vectors::sets::corpus_sets().iter().map(|(_, b)| b.len()).max().unwrap();
let mut worst = 0f64;
for v in all_vectors().into_iter().filter(|v| v.ext == "wvg1") {
let input = v.bytes.len() + held;
let p = peak(|| {
let _ = wvg1::load(&v.bytes, &mut store);
});
assert!(p <= C * input + FIXED, "{}: peak {p} bytes for {input} bytes of input", v.name);
worst = worst.max(p as f64 / input as f64);
}
// Headers that claim the caps over a payload of 80 bytes: refused at the exact
// length, having reserved nothing the counts name.
let mut h = Header::read(&all_vectors().into_iter().find(|v| v.name == "minimal").unwrap().bytes);
h.n_keys = 1 << 22;
h.n_glyphs = 1 << 20;
h.n_items = 65_536;
let mut b = Vec::new();
h.write(&mut b);
let p = peak(|| {
assert!(wvg1::parse(&b).is_err());
});
assert!(p <= FIXED, "a header claiming the caps made the parser reserve {p} bytes");
eprintln!("worst peak / input over the corpus: {worst:.2}");
}