Lines
100 %
Functions
Branches
//! A nested IF compiles without re-walking its test per level: the work is
//! bounded quadratically in the depth (ISS-091 owns making it linear).
use crate::compiler::CompileMode;
use crate::compiler::behaviour_oracle::{Outcome, observe};
use crate::reader::MAX_READ_DEPTH;
const SHAPES: [(&str, &str); 19] = [
("second operand", "(if (< a {X}) 1 2)"),
("equality", "(if (= a {X}) 1 2)"),
("literal first", "(if (< 1 {X}) 1 2)"),
("progn", "(if (< a (progn {X})) 1 2)"),
("sum", "(if (< a (+ {X} 1)) 1 2)"),
("let", "(if (< a (let ((y {X})) y)) 1 2)"),
(
"let progn init",
"(if (< a (let ((y (progn {X} 1))) y)) 1 2)",
),
("then arm", "(if (< a 1) {X} 2)"),
("plain test", "(if (< a 1) 1 {X})"),
("cond", "(cond ((< a {X}) 1) (t 2))"),
("cond equality", "(cond ((= a {X}) 1) (t 2))"),
("cond multi-arm", "(cond ((< a {X}) 1) ((< a 5) 3) (t 2))"),
"cond in effect position",
"(progn (cond ((< a {X}) 1) (t 2)) 1)",
("when", "(when (< a (progn {X} 1)) 1)"),
("unless", "(unless (< a (progn {X} 1)) 1)"),
("and", "(and (< a (progn {X} 1)) 1)"),
("or", "(or (< a (progn {X} 1)) 1)"),
"dead operand beside a raising call",
"(handler-case (+ (g) {X}) (t (e) 1))",
"dead LET init beside a raising call",
"(let ((v (handler-case (progn (g) {X}) (t (e) 1)))) v)",
];
fn nested(shape: &str, depth: usize) -> String {
let mut form = shape.replace("{X}", "1");
for _ in 1..depth {
form = shape.replace("{X}", &form);
}
format!("(defun g () (error 'x \"m\")) (let ((a (progn (print \"\") 1))) {form})")
fn work(shape: &str, depth: usize) -> (u64, Vec<Outcome>) {
let (behaviours, stats) = observe(&nested(shape, depth), CompileMode::Eval, &[]);
let outcomes = behaviours.into_iter().map(|b| b.outcome).collect();
(stats.stack_compiles + stats.probe_walks, outcomes)
/// The depths a shape nests to within the reader's nesting limit.
fn depths(shape: &str) -> Vec<usize> {
let at = shape.find("{X}").unwrap_or_default();
let per_level = shape[..at].matches('(').count() - shape[..at].matches(')').count();
[2, 3, 6, 12, 24]
.into_iter()
.filter(|depth| depth * per_level + 4 <= MAX_READ_DEPTH)
.collect()
/// Enforces a QUADRATIC bound, not a linear one: work(2d) <= 4 * work(d) along the
/// depths 3 -> 6 -> 12 -> 24. Each level still walks the operand nest below it
/// once (linear work is ISS-091); walking a test twice per level multiplies the
/// work ~2.5x per level and fails here.
#[test]
fn nesting_an_if_in_a_runtime_operand_does_not_multiply_the_work() {
for (name, shape) in SHAPES {
let points: Vec<(usize, u64)> = depths(shape)
.map(|depth| {
let (units, outcomes) = work(shape, depth);
assert!(
outcomes
.iter()
.all(|outcome| matches!(outcome, Outcome::Value(_))),
"{name} at depth {depth}: {outcomes:?}"
);
(depth, units)
})
.collect();
assert!(points.len() >= 3, "{name}: too few depths: {points:?}");
for pair in points.windows(2) {
pair[1].1 <= 4 * pair[0].1,
"{name}: quadratic bound work(2d) <= 4 * work(d) broken: {points:?}"
/// The audit's ISS-091 nest at depths 8 and 16: the same counter and the same
/// quadratic bound per doubling.
fn the_runtime_operand_nest_at_depths_8_and_16_stays_within_the_quadratic_bound() {
let (_, shape) = SHAPES[0];
let points: Vec<(usize, u64)> = [8, 16]
"depth {depth}: {outcomes:?}"
points[1].1 <= 4 * points[0].1,
"work(16) <= 4 * work(8) broken: {points:?}"