Skip to content

Commit 354bf93

Browse files
authored
Store document node inputs in Arc to reduce memory consumption (#3086)
Store values in memo hash contanier in arc
1 parent 469f0a6 commit 354bf93

3 files changed

Lines changed: 11 additions & 9 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use kurbo::BezPath;
2525
use serde_json::{Value, json};
2626
use std::collections::{HashMap, HashSet, VecDeque};
2727
use std::hash::{DefaultHasher, Hash, Hasher};
28+
use std::ops::Deref;
2829

2930
/// All network modifications should be done through this API, so the fields cannot be public. However, all fields within this struct can be public since it it not possible to have a public mutable reference.
3031
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
@@ -1281,7 +1282,7 @@ impl NodeNetworkInterface {
12811282
let artboard = self.document_node(&artboard_node_identifier.to_node(), &[]);
12821283
let clip_input = artboard.unwrap().inputs.get(5).unwrap();
12831284
if let NodeInput::Value { tagged_value, .. } = clip_input {
1284-
if tagged_value.clone().into_inner() == TaggedValue::Bool(true) {
1285+
if tagged_value.clone().deref() == &TaggedValue::Bool(true) {
12851286
return Some(Quad::clip(
12861287
self.document_metadata.bounding_box_document(layer).unwrap_or_default(),
12871288
self.document_metadata.bounding_box_document(artboard_node_identifier).unwrap_or_default(),

node-graph/gcore/src/memo.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ pub mod monitor {
156156
pub const IDENTIFIER: crate::ProtoNodeIdentifier = crate::ProtoNodeIdentifier::new("graphene_core::memo::MonitorNode");
157157
}
158158

159-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
159+
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
160160
pub struct MemoHash<T: Hash> {
161161
hash: u64,
162-
value: T,
162+
value: Arc<T>,
163163
}
164164

165165
impl<'de, T: serde::Deserialize<'de> + Hash> serde::Deserialize<'de> for MemoHash<T> {
@@ -183,10 +183,10 @@ impl<T: Hash + serde::Serialize> serde::Serialize for MemoHash<T> {
183183
impl<T: Hash> MemoHash<T> {
184184
pub fn new(value: T) -> Self {
185185
let hash = Self::calc_hash(&value);
186-
Self { hash, value }
186+
Self { hash, value: value.into() }
187187
}
188188
pub fn new_with_hash(value: T, hash: u64) -> Self {
189-
Self { hash, value }
189+
Self { hash, value: value.into() }
190190
}
191191

192192
fn calc_hash(data: &T) -> u64 {
@@ -198,7 +198,7 @@ impl<T: Hash> MemoHash<T> {
198198
pub fn inner_mut(&mut self) -> MemoHashGuard<'_, T> {
199199
MemoHashGuard { inner: self }
200200
}
201-
pub fn into_inner(self) -> T {
201+
pub fn into_inner(self) -> Arc<T> {
202202
self.value
203203
}
204204
pub fn hash_code(&self) -> u64 {
@@ -244,8 +244,8 @@ impl<T: Hash> Deref for MemoHashGuard<'_, T> {
244244
}
245245
}
246246

247-
impl<T: Hash> std::ops::DerefMut for MemoHashGuard<'_, T> {
247+
impl<T: Hash + Clone> std::ops::DerefMut for MemoHashGuard<'_, T> {
248248
fn deref_mut(&mut self) -> &mut Self::Target {
249-
&mut self.inner.value
249+
Arc::make_mut(&mut self.inner.value)
250250
}
251251
}

node-graph/graph-craft/src/document/value.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,8 @@ impl<'input> Node<'input, DAny<'input>> for UpcastNode {
392392
type Output = FutureAny<'input>;
393393

394394
fn eval(&'input self, _: DAny<'input>) -> Self::Output {
395-
Box::pin(async move { self.value.clone().into_inner().to_dynany() })
395+
let memo_clone = MemoHash::clone(&self.value);
396+
Box::pin(async move { memo_clone.into_inner().as_ref().clone().to_dynany() })
396397
}
397398
}
398399
impl UpcastNode {

0 commit comments

Comments
 (0)