Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions node-graph/graph-craft/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,16 +384,6 @@ impl ProtoNetwork {
nullification_node_id
}

pub fn source_ids(&self) -> impl Iterator<Item = &SourceId> {
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<NodeId, (ContextModification, Option<NodeId>)>) -> (ContextModification, Option<NodeId>) {
if let Some(result) = analyzed.get(&id) {
return result.clone();
Expand Down Expand Up @@ -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<_>>(), vec![SourceId(1), SourceId(1)]);
}

fn find_node<'a>(network: &'a ProtoNetwork, name: &str) -> (NodeId, &'a ProtoNode) {
Expand Down
24 changes: 16 additions & 8 deletions node-graph/interpreted-executor/src/dynamic_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NodeId>,
sources: Vec<SourceId>,
}

impl Default for DynamicExecutor {
Expand All @@ -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(),
}
}
}
Expand All @@ -55,20 +53,18 @@ 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 {
tree,
output,
typing_context,
orphaned_nodes: HashSet::new(),
sources,
})
}

pub fn sources(&self) -> &[SourceId] {
&self.sources
pub fn sources(&self) -> impl Iterator<Item = &SourceId> {
self.tree.sources.iter().map(|(_, source)| source)
}

/// Updates the existing [`BorrowTree`] to reflect the new [`ProtoNetwork`], reusing nodes where possible.
Expand Down Expand Up @@ -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()));
Expand All @@ -113,6 +108,13 @@ impl DynamicExecutor {
Ok(ResolvedDocumentNodeTypesDelta { add, remove })
}

pub fn update_without_freeing(&mut self, proto_network: ProtoNetwork) -> Result<ResolvedDocumentNodeTypesDelta, (ResolvedDocumentNodeTypesDelta, GraphErrors)> {
let orphaned_nodes = std::mem::take(&mut self.orphaned_nodes);
let result = self.update(proto_network);
self.orphaned_nodes = orphaned_nodes;
Comment thread
timon-schelling marked this conversation as resolved.
Resolved
result
}

pub fn input_type(&self) -> Option<Type> {
self.typing_context.type_of(self.output).map(|node_io| node_io.call_argument.clone())
}
Expand Down Expand Up @@ -270,6 +272,8 @@ pub struct BorrowTree {
source_map: HashMap<Path, (NodeId, NodeTypes)>,
/// The context features each node's output depends on.
context_features: HashMap<NodeId, core_types::ContextFeatures>,
/// The async source of each node that holds one.
sources: Vec<(NodeId, SourceId)>,
}

impl BorrowTree {
Expand Down Expand Up @@ -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<Path> {
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);
Expand Down Expand Up @@ -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());
Expand Down
27 changes: 27 additions & 0 deletions node-graph/interpreted-executor/src/dynamic_executor/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
7 changes: 2 additions & 5 deletions node-graph/libraries/core-types/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,9 @@ impl AsyncRuntime {
}))
}

pub fn update_sources(&self, live: &[SourceId]) {
pub fn update_sources<'a>(&self, live: impl IntoIterator<Item = &'a SourceId>) {
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 {
Expand Down
Loading