Eval: key nodes only on the graph inputs they read

An exposed-input change re-cooked 21 of 21 nodes in rusty_metal; now 1 (the reader).
Input-change cook (noisy, concurrent load): 1024 ~320 ms -> ~0.1 ms, 2048 ~1500 ms -> ~0.2 ms.
This commit is contained in:
Simon Hull committed 2026-11-03 14:30:34 -06:00
1 parent 55f1a1c323
commit b991f4fb11
2 files changed
+55 -1

No files matched your search

+16
View File
@@ -138,6 +138,22 @@ fn dynamic_parameters_follow_graph_inputs() {
assert!((mean(&s) - 0.5).abs() < 0.05);
}
#[test]
fn exposed_input_change_recooks_only_readers() {
let mut s = sample_session();
run(&mut s, "file.new", json!({"template": "rusty_metal", "size": 64}));
s.preview_size = None;
let u = run(&mut s, "graph.node.add", json!({"def": "uniform", "params": {"mode": 1}}))["id"].as_u64().unwrap();
run(&mut s, "graph.exposed.add", json!({"id": "level", "default": {"t": "float", "v": 0.25}}));
let f = json!({"nodes": [{"id": 1, "op": "get_var", "value": {"t": "text", "v": "level"}}], "output": 1});
run(&mut s, "graph.node.function", json!({"id": u, "param": "color", "function": f}));
s.cook();
run(&mut s, "graph.exposed.set", json!({"id": "level", "value": 0.75}));
s.cook();
let recooked: Vec<u64> = s.cooked.nodes.iter().filter(|(_, r)| !r.cached).map(|(id, _)| u64::from(id.0)).collect();
assert_eq!(recooked, [u], "only the node reading `level` re-cooks");
}
#[test]
fn presets_store_and_restore_inputs() {
let mut s = sample_session();
+39 -1
View File
@@ -327,6 +327,24 @@ fn graph_tree_hash(pkg: &Package, gi: usize, stack: &mut Vec<usize>) -> u64 {
h.finish()
}
/// The graph inputs a node's cook can read: those named by `get_*` ops of its function-valued
/// parameters. Bindings and node functions are already folded into `params` before the key is
/// computed, so these are the only remaining readers of `vars` (`Ctx::vars` in the function nodes).
/// Matching every `get_*` op, not only `get_var`, over-approximates on purpose.
fn vars_read<'v>(params: &BTreeMap<String, Value>, vars: &'v BTreeMap<String, Value>) -> Vec<(&'v str, &'v Value)> {
let mut read: Vec<(&str, &Value)> = params
.values()
.filter_map(|v| if let Value::Function(g) = v { Some(g) } else { None })
.flat_map(|g| g.nodes.iter())
.filter(|n| n.op.starts_with("get_"))
.filter_map(|n| if let Some(Value::Text(name)) = &n.value { vars.get_key_value(name.as_str()) } else { None })
.map(|(k, v)| (k.as_str(), v))
.collect();
read.sort_by_key(|(k, _)| *k);
read.dedup_by_key(|(k, _)| *k);
read
}
fn cook_node(
pkg: &Package,
gi: usize,
@@ -403,7 +421,7 @@ fn cook_node(
res_hash,
sub_hash,
input_img.map(image_key),
serde_json::to_string(vars).unwrap_or_default(),
serde_json::to_string(&vars_read(&params, vars)).unwrap_or_default(),
));
if let Some(v) = cache.get(key) {
return NodeResult { outputs: v, error: None, ms: 0.0, key, cached: true };
@@ -692,4 +710,24 @@ mod tests {
let c = cook(&p, 0, &opts, &mut cache);
assert!((c.image(o, 0).unwrap().data[0] - 0.1).abs() < 1e-4);
}
#[test]
fn input_change_recooks_only_readers() {
use materialcraft_core::function::{FnGraph, FnNode};
let (mut p, noise, blur, out) = sample();
let reader = p.graphs[0].add_node("pixel_processor", [0.0, 100.0]);
let get_a = FnNode { id: 1, op: "get_var".into(), inputs: vec![], value: Some(Value::Text("a".into())), pos: [0.0; 2] };
p.graphs[0].node_mut(reader).unwrap().params.insert("function".into(), Value::Function(Box::new(FnGraph { nodes: vec![get_a], output: Some(1) })));
let cache = &mut Cache::default();
let mut opts = Options::new(&no_res);
opts.overrides.insert("a".into(), Value::Float(0.25));
let first = cook(&p, 0, &opts, cache);
assert!(first.nodes.values().all(|r| r.error.is_none() && !r.cached));
opts.overrides.insert("a".into(), Value::Float(0.75));
let second = cook(&p, 0, &opts, cache);
for id in [noise, blur, out] {
assert!(second.nodes[&id].cached, "node {} does not read `a`", id.0);
}
assert!(!second.nodes[&reader].cached, "the reader must re-cook");
}
}