From 74685e4dc19b9f1c0464a69453d0f7f1aa873b29 Mon Sep 17 00:00:00 2001 From: Timon Date: Sun, 22 Nov 2026 16:27:28 +0000 Subject: [PATCH] Add a non-freeing executor update --- node-graph/graph-craft/src/proto.rs | 11 -------- .../src/dynamic_executor.rs | 24 +++++++++++------ .../src/dynamic_executor/test.rs | 27 +++++++++++++++++++ .../libraries/core-types/src/runtime.rs | 7 ++--- 4 files changed, 45 insertions(+), 24 deletions(-) diff --git a/node-graph/graph-craft/src/proto.rs b/node-graph/graph-craft/src/proto.rs index 2e8697eac03..fc5dbd84ff8 100644 --- a/node-graph/graph-craft/src/proto.rs +++ b/node-graph/graph-craft/src/proto.rs @@ -384,16 +384,6 @@ impl ProtoNetwork { nullification_node_id } - pub fn source_ids(&self) -> impl Iterator { - self.nodes.iter().filter_map(|(_, node)| match &node.construction_args { - ConstructionArgs::Value(value) => match &**value { - TaggedValue::SourceId(source) => Some(source), - _ => None, - }, - _ => None, - }) - } - fn find_context_dependencies(&mut self, id: NodeId, analyzed: &mut FxHashMap)>) -> (ContextModification, Option) { if let Some(result) = analyzed.get(&id) { return result.clone(); @@ -1177,7 +1167,6 @@ mod test { network.insert_context_nullification_nodes().unwrap(); assert!(nullification_filters(&network).is_empty()); - assert_eq!(network.source_ids().copied().collect::>(), vec![SourceId(1), SourceId(1)]); } fn find_node<'a>(network: &'a ProtoNetwork, name: &str) -> (NodeId, &'a ProtoNode) { diff --git a/node-graph/interpreted-executor/src/dynamic_executor.rs b/node-graph/interpreted-executor/src/dynamic_executor.rs index 68185553e9f..302b8c29c41 100644 --- a/node-graph/interpreted-executor/src/dynamic_executor.rs +++ b/node-graph/interpreted-executor/src/dynamic_executor.rs @@ -19,9 +19,8 @@ pub struct DynamicExecutor { tree: BorrowTree, /// Stores the types of the proto nodes. typing_context: TypingContext, - // This allows us to keep the nodes around for one more frame which is used for introspection + // Kept for one more update, so a node that comes back is reused orphaned_nodes: HashSet, - sources: Vec, } impl Default for DynamicExecutor { @@ -31,7 +30,6 @@ impl Default for DynamicExecutor { tree: Default::default(), typing_context: TypingContext::new(&node_registry::NODE_REGISTRY), orphaned_nodes: HashSet::new(), - sources: Vec::new(), } } } @@ -55,7 +53,6 @@ impl DynamicExecutor { let mut typing_context = TypingContext::new(&node_registry::NODE_REGISTRY); typing_context.update(&proto_network)?; let output = proto_network.output; - let sources = proto_network.source_ids().copied().collect(); let tree = BorrowTree::new(proto_network, &typing_context)?; Ok(Self { @@ -63,12 +60,11 @@ impl DynamicExecutor { output, typing_context, orphaned_nodes: HashSet::new(), - sources, }) } - pub fn sources(&self) -> &[SourceId] { - &self.sources + pub fn sources(&self) -> impl Iterator { + self.tree.sources.iter().map(|(_, source)| source) } /// Updates the existing [`BorrowTree`] to reflect the new [`ProtoNetwork`], reusing nodes where possible. @@ -96,7 +92,6 @@ impl DynamicExecutor { (ResolvedDocumentNodeTypesDelta { add, remove: Vec::new() }, e) })?; - self.sources = proto_network.source_ids().copied().collect(); let (add, orphaned) = self.tree.update(proto_network, &self.typing_context).map_err(|e| (ResolvedDocumentNodeTypesDelta::default(), e))?; let old_to_remove = core::mem::replace(&mut self.orphaned_nodes, orphaned); let mut remove = Vec::with_capacity(old_to_remove.len() - self.orphaned_nodes.len().min(old_to_remove.len())); @@ -113,6 +108,13 @@ impl DynamicExecutor { Ok(ResolvedDocumentNodeTypesDelta { add, remove }) } + pub fn update_without_freeing(&mut self, proto_network: ProtoNetwork) -> Result { + let orphaned_nodes = std::mem::take(&mut self.orphaned_nodes); + let result = self.update(proto_network); + self.orphaned_nodes = orphaned_nodes; + result + } + pub fn input_type(&self) -> Option { self.typing_context.type_of(self.output).map(|node_io| node_io.call_argument.clone()) } @@ -270,6 +272,8 @@ pub struct BorrowTree { source_map: HashMap, /// The context features each node's output depends on. context_features: HashMap, + /// The async source of each node that holds one. + sources: Vec<(NodeId, SourceId)>, } impl BorrowTree { @@ -436,6 +440,7 @@ impl BorrowTree { /// - Returns `None` if the node is not found in the `nodes` HashMap. pub fn free_node(&mut self, id: NodeId) -> Option { self.context_features.remove(&id); + self.sources.retain(|(node, _)| *node != id); let (_, path, _) = self.nodes.remove(&id)?; if self.source_map.get(&path)?.0 == id { self.source_map.remove(&path); @@ -510,6 +515,9 @@ impl BorrowTree { match &proto_node.construction_args { ConstructionArgs::Value(value) => { + if let TaggedValue::SourceId(source) = &**value { + self.sources.push((id, *source)); + } let upcasted = UpcastNode::new(value.to_owned()); let node = ObservedNode::new(Box::new(upcasted) as TypeErasedBox<'_>); self.store_node(node, id, path.into(), Box::default()); diff --git a/node-graph/interpreted-executor/src/dynamic_executor/test.rs b/node-graph/interpreted-executor/src/dynamic_executor/test.rs index 5191b29863b..054f9c13f12 100644 --- a/node-graph/interpreted-executor/src/dynamic_executor/test.rs +++ b/node-graph/interpreted-executor/src/dynamic_executor/test.rs @@ -1176,3 +1176,30 @@ fn a_network_node_resolves_to_the_proto_node_its_export_is_wired_to() { assert_eq!(tree.output_node(&document_network, &[NodeId(6)]), Some(NodeId(2))); assert_eq!(tree.output_node(&document_network, &[NodeId(8)]), None); } + +fn source_network(node: u64) -> ProtoNetwork { + ProtoNetwork { + inputs: vec![], + output: NodeId(node), + nodes: vec![value_node(node, TaggedValue::SourceId(SourceId(node)))], + } +} + +#[test] +fn update_without_freeing_keeps_the_nodes_the_network_leaves_out() { + let mut executor = DynamicExecutor::new(source_network(0)).unwrap(); + + executor.update_without_freeing(source_network(1)).unwrap(); + executor.update_without_freeing(source_network(1)).unwrap(); + assert!(executor.tree().get(NodeId(0)).is_some()); +} + +#[test] +fn a_source_is_listed_for_as_long_as_its_node_lives() { + let mut executor = DynamicExecutor::new(source_network(0)).unwrap(); + + for _ in 0..3 { + executor.update(source_network(1)).unwrap(); + assert_eq!(executor.sources().any(|source| *source == SourceId(0)), executor.tree().get(NodeId(0)).is_some()); + } +} diff --git a/node-graph/libraries/core-types/src/runtime.rs b/node-graph/libraries/core-types/src/runtime.rs index c0dcd9b624d..9ce59d6372a 100644 --- a/node-graph/libraries/core-types/src/runtime.rs +++ b/node-graph/libraries/core-types/src/runtime.rs @@ -72,12 +72,9 @@ impl AsyncRuntime { })) } - pub fn update_sources(&self, live: &[SourceId]) { + pub fn update_sources<'a>(&self, live: impl IntoIterator) { let mut generations = self.0.generations.lock().unwrap_or_else(PoisonError::into_inner); - generations.retain(|source, _| live.contains(source)); - for source in live { - generations.entry(*source).or_insert(0); - } + *generations = live.into_iter().map(|source| (*source, generations.get(source).copied().unwrap_or(0))).collect(); } pub fn snapshot(&self) -> Generations {