Fix compilation for compile benchmarks

This commit is contained in:
Dennis Kobert
2025-09-18 12:08:31 +02:00
parent 7d3efffdac
commit e176a0b812
8 changed files with 17 additions and 15 deletions

View File

@@ -377,7 +377,7 @@ impl NodeRuntime {
assert_eq!(scoped_network.exports.len(), 1, "Graph with multiple outputs not yet handled");
let c = Compiler {};
let proto_network = match c.compile_single(scoped_network, self.executor.typing_context_mut()) {
let proto_network = match c.compile_single(scoped_network, Some(self.executor.typing_context_mut())) {
Ok(network) => network,
Err(e) => return Err((ResolvedDocumentNodeTypesDelta::default(), e)),
};

View File

@@ -1,12 +1,15 @@
use criterion::{Criterion, black_box, criterion_group, criterion_main};
use criterion::{Criterion, criterion_group, criterion_main};
use graph_craft::util::DEMO_ART;
use std::hint::black_box;
fn compile_to_proto(c: &mut Criterion) {
use graph_craft::util::{compile, load_from_name};
let mut c = c.benchmark_group("Compile Network cold");
for name in DEMO_ART {
let network = load_from_name(name);
c.bench_function(name, |b| b.iter_batched(|| network.clone(), |network| compile(black_box(network)), criterion::BatchSize::SmallInput));
c.bench_function(name, |b| {
b.iter_batched(|| network.clone(), |network| compile(black_box(network), None), criterion::BatchSize::SmallInput)
});
}
}

View File

@@ -5,7 +5,7 @@ use iai_callgrind::{library_benchmark, library_benchmark_group, main};
#[library_benchmark]
#[benches::with_setup(args = ["isometric-fountain", "painted-dreams", "procedural-string-lights", "parametric-dunescape", "red-dress", "valley-of-spires"], setup = load_from_name)]
pub fn compile_to_proto(_input: NodeNetwork) {
std::hint::black_box(compile(_input));
std::hint::black_box(compile(_input, None));
}
library_benchmark_group!(name = compile_group; benchmarks = compile_to_proto);

View File

@@ -5,7 +5,7 @@ use std::error::Error;
pub struct Compiler {}
impl Compiler {
pub fn compile(&self, mut network: NodeNetwork, ty: &mut TypingContext) -> impl Iterator<Item = Result<ProtoNetwork, String>> {
pub fn compile<'a>(&'a self, mut network: NodeNetwork, mut ty: Option<&mut TypingContext>) -> impl Iterator<Item = Result<ProtoNetwork, String>> {
let node_ids = network.nodes.keys().copied().collect::<Vec<_>>();
network.populate_dependants();
for id in node_ids {
@@ -17,12 +17,12 @@ impl Compiler {
let proto_networks = network.into_proto_networks();
proto_networks.map(move |mut proto_network| {
proto_network.insert_context_nullification_nodes(ty)?;
proto_network.insert_context_nullification_nodes(ty.as_deref_mut())?;
proto_network.generate_stable_node_ids();
Ok(proto_network)
})
}
pub fn compile_single(&self, network: NodeNetwork, ty: &mut TypingContext) -> Result<ProtoNetwork, String> {
pub fn compile_single(&self, network: NodeNetwork, ty: Option<&mut TypingContext>) -> Result<ProtoNetwork, String> {
assert_eq!(network.exports.len(), 1, "Graph with multiple outputs not yet handled");
let Some(proto_network) = self.compile(network, ty).next() else {
return Err("Failed to convert graph into proto graph".to_string());

View File

@@ -213,14 +213,14 @@ enum NodeState {
struct NodeList<'a> {
vec: Vec<(NodeId, ProtoNode)>,
ty: &'a mut TypingContext,
ty: Option<&'a mut TypingContext>,
id_mapping: Vec<usize>,
}
impl<'a> NodeList<'a> {
fn push_node(&mut self, node: ProtoNode, old_node_idx: Option<usize>) -> Result<(NodeId, Type), GraphErrors> {
fn push_node(&mut self, node: ProtoNode, old_node_idx: Option<usize>) -> Result<(NodeId, Option<Type>), GraphErrors> {
let node_id = node.stable_node_id().unwrap();
let out_ty = self.ty.infer(node_id, &node)?.return_value;
let out_ty = if let Some(ty) = &mut self.ty { Some(ty.infer(node_id, &node)?.return_value) } else { None };
// log::debug!("{old_node_idx:?}, {node_id:?}, {node:?}, {:?}", self.id_mapping);
if let Some(old_node_idx) = old_node_idx {
assert_eq!(old_node_idx, self.id_mapping.len());
@@ -316,7 +316,7 @@ impl ProtoNetwork {
/// Inserts context nullification nodes to optimize caching.
/// This analysis is performed after topological sorting to ensure proper dependency tracking.
pub fn insert_context_nullification_nodes(&mut self, ty: &mut TypingContext) -> Result<(), String> {
pub fn insert_context_nullification_nodes(&mut self, ty: Option<&mut TypingContext>) -> Result<(), String> {
// Perform topological sort once
self.reorder_ids()?;
@@ -394,7 +394,7 @@ impl ProtoNetwork {
Ok(nullification_node_id)
}
fn find_context_dependencies(&mut self, id: NodeId, new_order: &mut NodeList, results: &mut Vec<(ContextFeatures, NodeId, Type, bool)>) -> Result<(), GraphErrors> {
fn find_context_dependencies(&mut self, id: NodeId, new_order: &mut NodeList, results: &mut Vec<(ContextFeatures, NodeId, Option<Type>, bool)>) -> Result<(), GraphErrors> {
let mut branch_dependencies = Vec::new();
let mut combined_deps = ContextFeatures::default();
let node_index = id.0 as usize;

View File

@@ -8,7 +8,7 @@ pub fn load_network(document_string: &str) -> NodeNetwork {
serde_json::from_str::<NodeNetwork>(&document).expect("Failed to parse document")
}
pub fn compile(network: NodeNetwork, ty: &mut TypingContext) -> ProtoNetwork {
pub fn compile(network: NodeNetwork, ty: Option<&mut TypingContext>) -> ProtoNetwork {
let compiler = Compiler {};
compiler.compile_single(network, ty).unwrap()
}

View File

@@ -232,7 +232,7 @@ fn compile_graph(document_string: String, editor_api: Arc<WasmEditorApi>) -> Res
let compiler = Compiler {};
let mut ty = TypingContext::new(&interpreted_executor::node_registry::NODE_REGISTRY);
compiler.compile_single(wrapped_network, &mut ty).map_err(|x| x.into())
compiler.compile_single(wrapped_network, Some(&mut ty)).map_err(|x| x.into())
}
fn create_executor(proto_network: ProtoNetwork) -> Result<DynamicExecutor, Box<dyn Error>> {

View File

@@ -22,6 +22,5 @@ kurbo = { workspace = true }
vector-types = { workspace = true }
graphic-types = { workspace = true }
# Workspace dependencies
vello = { workspace = true }