perf: avoid a few clones in wasm (#288)

This commit is contained in:
Zixuan Chen 2024-03-02 16:26:34 +08:00 committed by GitHub
parent 1db510104e
commit a5fce60883
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 18 deletions

View file

@ -333,10 +333,10 @@ pub mod wasm {
}
}
impl From<TreeDiff> 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<Delta<StringSlice, StyleMeta>> for JsValue {
fn from(value: Delta<StringSlice, StyleMeta>) -> Self {
impl From<&Delta<StringSlice, StyleMeta>> for JsValue {
fn from(value: &Delta<StringSlice, StyleMeta>) -> 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()));

View file

@ -71,9 +71,7 @@ pub(crate) fn resolved_diff_to_js(value: &Diff, doc: &Arc<LoroDoc>) -> 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<LoroDoc>) -> 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<LoroDoc>) -> 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<LoroDoc>) -> JsValue {
fn map_delta_to_js(value: &ResolvedMapDelta, doc: &Arc<LoroDoc>) -> JsValue {
let obj = Object::new();
for (key, value) in value.updated.iter() {
let value = if let Some(value) = value.value.clone() {