From 934b0fdda91f55709a41ae5cbf1335059429648e Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Wed, 9 Nov 2022 14:11:06 +0800 Subject: [PATCH] perf: speed up find path and common ancestors --- crates/loro-core/examples/text_sync.rs | 46 ++++++++++++++------------ crates/loro-core/src/dag.rs | 23 +++++++++++++ 2 files changed, 47 insertions(+), 22 deletions(-) diff --git a/crates/loro-core/examples/text_sync.rs b/crates/loro-core/examples/text_sync.rs index afc6c7ba..4bd82a74 100644 --- a/crates/loro-core/examples/text_sync.rs +++ b/crates/loro-core/examples/text_sync.rs @@ -20,29 +20,31 @@ fn main() { let mut loro = LoroCore::default(); let mut loro_b = LoroCore::default(); let start = Instant::now(); - for txn in txns.unwrap().as_array().unwrap() { - let mut text = loro.get_or_create_root_text("text").unwrap(); - let patches = txn - .as_object() - .unwrap() - .get("patches") - .unwrap() - .as_array() - .unwrap(); - for patch in patches { - let pos = patch[0].as_u64().unwrap() as usize; - let del_here = patch[1].as_u64().unwrap() as usize; - let ins_content = patch[2].as_str().unwrap(); - text.delete(pos, del_here); - text.insert(pos, ins_content); - } + for _ in 0..10 { + for txn in txns.unwrap().as_array().unwrap() { + let mut text = loro.get_or_create_root_text("text").unwrap(); + let patches = txn + .as_object() + .unwrap() + .get("patches") + .unwrap() + .as_array() + .unwrap(); + for patch in patches { + let pos = patch[0].as_u64().unwrap() as usize; + let del_here = patch[1].as_u64().unwrap() as usize; + let ins_content = patch[2].as_str().unwrap(); + text.delete(pos, del_here); + text.insert(pos, ins_content); + } - let a = text.text_len(); - drop(text); - loro_b.import(loro.export(loro_b.vv())); - let text = loro_b.get_or_create_root_text("text").unwrap(); - let b = text.text_len(); - assert_eq!(a, b); + let a = text.text_len(); + drop(text); + loro_b.import(loro.export(loro_b.vv())); + let text = loro_b.get_or_create_root_text("text").unwrap(); + let b = text.text_len(); + assert_eq!(a, b); + } } loro_b.debug_inspect(); loro.debug_inspect(); diff --git a/crates/loro-core/src/dag.rs b/crates/loro-core/src/dag.rs index 6645889a..f715ec71 100644 --- a/crates/loro-core/src/dag.rs +++ b/crates/loro-core/src/dag.rs @@ -117,6 +117,21 @@ impl DagUtils for T { if from.client_id == to.client_id { let from_span = self.get(from).unwrap(); let to_span = self.get(to).unwrap(); + if std::ptr::eq(from_span, to_span) { + if from.counter < to.counter { + ans.right.insert( + from.client_id, + CounterSpan::new(from.counter + 1, to.counter + 1), + ); + } else { + ans.left.insert( + from.client_id, + CounterSpan::new(to.counter + 1, from.counter + 1), + ); + } + return ans; + } + if from_span.deps().len() == 1 && to_span.contains_id(from_span.deps()[0]) { ans.left.insert( from.client_id, @@ -533,6 +548,14 @@ where if left.client_id == right.client_id { let left_span = get(left).unwrap(); let right_span = get(right).unwrap(); + if std::ptr::eq(left_span, right_span) { + if left.counter < right.counter { + return smallvec![left]; + } else { + return smallvec![right]; + } + } + if left_span.deps().len() == 1 && right_span.contains_id(left_span.deps()[0]) { return smallvec![right]; }