From a5fce60883a3bb33365817b7f00fc5ba90427741 Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Sat, 2 Mar 2024 16:26:34 +0800 Subject: [PATCH] perf: avoid a few clones in wasm (#288) --- crates/loro-internal/src/value.rs | 10 +++++----- crates/loro-wasm/src/convert.rs | 17 ++++------------- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/crates/loro-internal/src/value.rs b/crates/loro-internal/src/value.rs index 53bf72e0..329524b8 100644 --- a/crates/loro-internal/src/value.rs +++ b/crates/loro-internal/src/value.rs @@ -333,10 +333,10 @@ pub mod wasm { } } - impl From for JsValue { - fn from(value: TreeDiff) -> Self { + impl From<&TreeDiff> for JsValue { + fn from(value: &TreeDiff) -> Self { let array = Array::new(); - for diff in value.diff.into_iter() { + for diff in value.diff.iter() { let obj = Object::new(); js_sys::Reflect::set(&obj, &"target".into(), &diff.target.into()).unwrap(); match diff.action { @@ -358,8 +358,8 @@ pub mod wasm { } } - impl From> for JsValue { - fn from(value: Delta) -> Self { + impl From<&Delta> for JsValue { + fn from(value: &Delta) -> Self { let arr = Array::new_with_length(value.len() as u32); for (i, v) in value.iter().enumerate() { arr.set(i as u32, JsValue::from(v.clone())); diff --git a/crates/loro-wasm/src/convert.rs b/crates/loro-wasm/src/convert.rs index b88ab70a..1d411c37 100644 --- a/crates/loro-wasm/src/convert.rs +++ b/crates/loro-wasm/src/convert.rs @@ -71,9 +71,7 @@ pub(crate) fn resolved_diff_to_js(value: &Diff, doc: &Arc) -> JsValue { Diff::Tree(tree) => { js_sys::Reflect::set(&obj, &JsValue::from_str("type"), &JsValue::from_str("tree")) .unwrap(); - - // TODO: PERF Avoid clone - js_sys::Reflect::set(&obj, &JsValue::from_str("diff"), &tree.clone().into()).unwrap(); + js_sys::Reflect::set(&obj, &JsValue::from_str("diff"), &tree.into()).unwrap(); } Diff::List(list) => { // set type as "list" @@ -96,13 +94,7 @@ pub(crate) fn resolved_diff_to_js(value: &Diff, doc: &Arc) -> JsValue { js_sys::Reflect::set(&obj, &JsValue::from_str("type"), &JsValue::from_str("text")) .unwrap(); // set diff as array - // TODO: PERF Avoid clone - js_sys::Reflect::set( - &obj, - &JsValue::from_str("diff"), - &JsValue::from(text.clone()), - ) - .unwrap(); + js_sys::Reflect::set(&obj, &JsValue::from_str("diff"), &JsValue::from(text)).unwrap(); } Diff::Map(map) => { js_sys::Reflect::set(&obj, &JsValue::from_str("type"), &JsValue::from_str("map")) @@ -111,8 +103,7 @@ pub(crate) fn resolved_diff_to_js(value: &Diff, doc: &Arc) -> JsValue { js_sys::Reflect::set( &obj, &JsValue::from_str("updated"), - // TODO: PERF Avoid clone - &map_delta_to_js(map.clone(), doc), + &map_delta_to_js(map, doc), ) .unwrap(); } @@ -201,7 +192,7 @@ pub fn convert(value: LoroValue) -> JsValue { } } -fn map_delta_to_js(value: ResolvedMapDelta, doc: &Arc) -> JsValue { +fn map_delta_to_js(value: &ResolvedMapDelta, doc: &Arc) -> JsValue { let obj = Object::new(); for (key, value) in value.updated.iter() { let value = if let Some(value) = value.value.clone() {