perf: speed up find path and common ancestors

This commit is contained in:
Zixuan Chen 2022-11-09 14:11:06 +08:00
parent 58fb7de26c
commit 934b0fdda9
2 changed files with 47 additions and 22 deletions

View file

@ -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();

View file

@ -117,6 +117,21 @@ impl<T: Dag + ?Sized> 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];
}