Skip to content

Commit 816b8ea

Browse files
committed
Incrementally update outward wires instead of repeatedly rebuilding them
1 parent 373cca1 commit 816b8ea

1 file changed

Lines changed: 33 additions & 9 deletions

File tree

editor/src/messages/portfolio/document/utility_types/network_interface.rs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,6 +2065,30 @@ impl NodeNetworkInterface {
20652065
network_metadata.transient_metadata.outward_wires.unload();
20662066
}
20672067

2068+
/// Incrementally updates the outward_wires cache when a single input connector changes,
2069+
/// avoiding a full rebuild. If the cache is not loaded, this is a no-op (it will be fully
2070+
/// rebuilt on the next read via `outward_wires()`).
2071+
fn update_outward_wires(&mut self, network_path: &[NodeId], input_connector: &InputConnector, old_input: &NodeInput, new_input: &NodeInput) {
2072+
let Some(network_metadata) = self.network_metadata_mut(network_path) else {
2073+
return;
2074+
};
2075+
let TransientMetadata::Loaded(outward_wires) = &mut network_metadata.transient_metadata.outward_wires else {
2076+
return;
2077+
};
2078+
2079+
// Remove the input_connector from the old output's downstream list
2080+
if let Some(old_output) = OutputConnector::from_input(old_input)
2081+
&& let Some(connections) = outward_wires.get_mut(&old_output)
2082+
{
2083+
connections.retain(|c| c != input_connector);
2084+
}
2085+
2086+
// Add the input_connector to the new output's downstream list
2087+
if let Some(new_output) = OutputConnector::from_input(new_input) {
2088+
outward_wires.entry(new_output).or_default().push(*input_connector);
2089+
}
2090+
}
2091+
20682092
pub fn layer_width(&mut self, node_id: &NodeId, network_path: &[NodeId]) -> Option<u32> {
20692093
let Some(node_metadata) = self.node_metadata(node_id, network_path) else {
20702094
log::error!("Could not get nested node_metadata in layer_width");
@@ -3841,7 +3865,7 @@ impl NodeNetworkInterface {
38413865
return;
38423866
};
38433867

3844-
match input_connector {
3868+
let old_input = match input_connector {
38453869
InputConnector::Node { node_id, input_index } => {
38463870
let Some(node) = network.nodes.get_mut(node_id) else {
38473871
log::error!("Could not get node in set_input_for_import");
@@ -3851,19 +3875,19 @@ impl NodeNetworkInterface {
38513875
log::error!("Could not get input in set_input_for_import");
38523876
return;
38533877
};
3854-
*input = new_input;
3878+
std::mem::replace(input, new_input.clone())
38553879
}
38563880
InputConnector::Export(export_index) => {
38573881
let Some(export) = network.exports.get_mut(*export_index) else {
38583882
log::error!("Could not get export in set_input_for_import");
38593883
return;
38603884
};
3861-
*export = new_input;
3885+
std::mem::replace(export, new_input.clone())
38623886
}
3863-
}
3887+
};
38643888

38653889
self.transaction_modified();
3866-
self.unload_outward_wires(network_path);
3890+
self.update_outward_wires(network_path, input_connector, &old_input, &new_input);
38673891
}
38683892

38693893
pub fn set_input(&mut self, input_connector: &InputConnector, new_input: NodeInput, network_path: &[NodeId]) {
@@ -3985,7 +4009,7 @@ impl NodeNetworkInterface {
39854009
}
39864010
}
39874011
}
3988-
self.unload_outward_wires(network_path);
4012+
self.update_outward_wires(network_path, input_connector, &old_input, &new_input);
39894013
// Layout system
39904014
let Some(current_node_position) = self.position(upstream_node_id, network_path) else {
39914015
log::error!("Could not get current node position in set_input for node {upstream_node_id}");
@@ -4040,17 +4064,17 @@ impl NodeNetworkInterface {
40404064
}
40414065
// If a connection is made to the imports
40424066
(NodeInput::Value { .. } | NodeInput::Scope { .. } | NodeInput::Inline { .. }, NodeInput::Import { .. }) => {
4043-
self.unload_outward_wires(network_path);
4067+
self.update_outward_wires(network_path, input_connector, &old_input, &new_input);
40444068
self.unload_wire(input_connector, network_path);
40454069
}
40464070
// If a connection to the imports is disconnected
40474071
(NodeInput::Import { .. }, NodeInput::Value { .. } | NodeInput::Scope { .. } | NodeInput::Inline { .. }) => {
4048-
self.unload_outward_wires(network_path);
4072+
self.update_outward_wires(network_path, input_connector, &old_input, &new_input);
40494073
self.unload_wire(input_connector, network_path);
40504074
}
40514075
// If a node is disconnected.
40524076
(NodeInput::Node { .. }, NodeInput::Value { .. } | NodeInput::Scope { .. } | NodeInput::Inline { .. }) => {
4053-
self.unload_outward_wires(network_path);
4077+
self.update_outward_wires(network_path, input_connector, &old_input, &new_input);
40544078
self.unload_wire(input_connector, network_path);
40554079

40564080
if let Some((old_upstream_node_id, previous_position)) = previous_metadata {

0 commit comments

Comments
 (0)