diff --git a/.vscode/settings.json b/.vscode/settings.json index 76d0b720..b164a984 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -53,6 +53,7 @@ "FIXME", "TODO", "FUTURE", + "PERF", "XXX", "[ ]", "[x]" diff --git a/crates/loro-common/src/id.rs b/crates/loro-common/src/id.rs index a0ad689e..ffad33e0 100644 --- a/crates/loro-common/src/id.rs +++ b/crates/loro-common/src/id.rs @@ -108,7 +108,7 @@ impl ID { #[inline] pub fn to_span(&self, len: usize) -> IdSpan { IdSpan { - client_id: self.peer, + peer: self.peer, counter: CounterSpan::new(self.counter, self.counter + len as Counter), } } diff --git a/crates/loro-common/src/span.rs b/crates/loro-common/src/span.rs index dd1e4e2d..d5bdc5bc 100644 --- a/crates/loro-common/src/span.rs +++ b/crates/loro-common/src/span.rs @@ -270,8 +270,7 @@ impl IdLpSpan { /// We need this because it'll make merging deletions easier. #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub struct IdSpan { - // TODO: rename this to peer_id - pub client_id: PeerID, + pub peer: PeerID, pub counter: CounterSpan, } @@ -279,7 +278,7 @@ impl IdSpan { #[inline] pub fn new(peer: PeerID, from: Counter, to: Counter) -> Self { Self { - client_id: peer, + peer, counter: CounterSpan { start: from, end: to, @@ -289,7 +288,7 @@ impl IdSpan { #[inline] pub fn contains(&self, id: ID) -> bool { - self.client_id == id.peer && self.counter.contains(id.counter) + self.peer == id.peer && self.counter.contains(id.counter) } #[inline(always)] @@ -310,29 +309,29 @@ impl IdSpan { /// This is different from id_start. id_start may be greater than id_end, but this is the min of id_start and id_end-1 #[inline] pub fn norm_id_start(&self) -> ID { - ID::new(self.client_id, self.counter.min()) + ID::new(self.peer, self.counter.min()) } /// This is different from id_end. id_start may be greater than id_end. This is the max of id_start+1 and id_end #[inline] pub fn norm_id_end(&self) -> ID { - ID::new(self.client_id, self.counter.norm_end()) + ID::new(self.peer, self.counter.norm_end()) } pub fn to_id_span_vec(self) -> IdSpanVector { let mut out = IdSpanVector::default(); - out.insert(self.client_id, self.counter); + out.insert(self.peer, self.counter); out } pub fn get_intersection(&self, other: &Self) -> Option { - if self.client_id != other.client_id { + if self.peer != other.peer { return None; } let counter = self.counter.get_intersection(&other.counter)?; Some(Self { - client_id: self.client_id, + peer: self.peer, counter, }) } @@ -349,7 +348,7 @@ impl Sliceable for IdSpan { #[inline] fn slice(&self, from: usize, to: usize) -> Self { IdSpan { - client_id: self.client_id, + peer: self.peer, counter: self.counter.slice(from, to), } } @@ -357,7 +356,7 @@ impl Sliceable for IdSpan { impl Mergable for IdSpan { fn is_mergable(&self, other: &Self, _: &()) -> bool { - self.client_id == other.client_id && self.counter.is_mergable(&other.counter, &()) + self.peer == other.peer && self.counter.is_mergable(&other.counter, &()) } fn merge(&mut self, other: &Self, _: &()) { @@ -493,12 +492,12 @@ mod test_id_span { use super::*; macro_rules! id_spans { - ($([$client_id:expr, $from:expr, $to:expr]),*) => { + ($([$peer:expr, $from:expr, $to:expr]),*) => { { let mut id_spans = RleVecWithIndex::new(); $( id_spans.push(IdSpan { - client_id: $client_id, + peer: $peer, counter: CounterSpan::new($from, $to), }); )* @@ -511,19 +510,19 @@ mod test_id_span { fn test_id_span_rle_vec() { let mut id_span_vec = RleVecWithIndex::new(); id_span_vec.push(IdSpan { - client_id: 0, + peer: 0, counter: CounterSpan::new(0, 2), }); assert_eq!(id_span_vec.merged_len(), 1); assert_eq!(id_span_vec.atom_len(), 2); id_span_vec.push(IdSpan { - client_id: 0, + peer: 0, counter: CounterSpan::new(2, 4), }); assert_eq!(id_span_vec.merged_len(), 1); assert_eq!(id_span_vec.atom_len(), 4); id_span_vec.push(IdSpan { - client_id: 2, + peer: 2, counter: CounterSpan::new(2, 4), }); assert_eq!(id_span_vec.merged_len(), 2); diff --git a/crates/loro-internal/src/arena.rs b/crates/loro-internal/src/arena.rs index 0cb81822..6d78762e 100644 --- a/crates/loro-internal/src/arena.rs +++ b/crates/loro-internal/src/arena.rs @@ -52,8 +52,6 @@ pub struct StrAllocResult { pub start: usize, /// unicode end pub end: usize, - // TODO: remove this field? - pub utf16_len: usize, } impl SharedArena { @@ -394,10 +392,8 @@ fn _alloc_value(values_lock: &mut MutexGuard<'_, Vec>, value: LoroVal fn _alloc_str(text_lock: &mut MutexGuard<'_, StrArena>, str: &str) -> StrAllocResult { let start = text_lock.len_unicode(); - let start_wchars = text_lock.len_utf16(); text_lock.alloc(str); StrAllocResult { - utf16_len: text_lock.len_utf16() - start_wchars, start, end: text_lock.len_unicode(), } diff --git a/crates/loro-internal/src/container/richtext/tracker/crdt_rope.rs b/crates/loro-internal/src/container/richtext/tracker/crdt_rope.rs index afab45e4..5f6f81b2 100644 --- a/crates/loro-internal/src/container/richtext/tracker/crdt_rope.rs +++ b/crates/loro-internal/src/container/richtext/tracker/crdt_rope.rs @@ -330,7 +330,7 @@ impl CrdtRope { for (leaf, group) in &updates.into_iter().group_by(|x| x.leaf) { let elem = self.tree.get_elem(leaf).unwrap(); for u in group { - debug_assert_eq!(u.id_span.client_id, elem.id.peer); + debug_assert_eq!(u.id_span.peer, elem.id.peer); let start = (u.id_span.ctr_start() - elem.id.counter).max(0); let end = u.id_span.ctr_end() - elem.id.counter; tree_update_info.push(( diff --git a/crates/loro-internal/src/container/richtext/tracker/id_to_cursor.rs b/crates/loro-internal/src/container/richtext/tracker/id_to_cursor.rs index 574171ae..c73140ae 100644 --- a/crates/loro-internal/src/container/richtext/tracker/id_to_cursor.rs +++ b/crates/loro-internal/src/container/richtext/tracker/id_to_cursor.rs @@ -46,7 +46,7 @@ impl IdToCursor { /// id_span should be within the same `Cursor` and should be a `Insert` pub fn update_insert(&mut self, id_span: IdSpan, new_leaf: LeafIndex) { debug_assert!(!id_span.is_reversed()); - let list = self.map.get_mut(&id_span.client_id).unwrap(); + let list = self.map.get_mut(&id_span.peer).unwrap(); let last = list.last().unwrap(); debug_assert!(last.counter + last.cursor.rle_len() as Counter > id_span.counter.max()); let mut index = match list.binary_search_by_key(&id_span.counter.start, |x| x.counter) { @@ -104,7 +104,7 @@ impl IdToCursor { pub fn iter(&self, mut iter_id_span: IdSpan) -> impl Iterator + '_ { iter_id_span.normalize_(); - let list = self.map.get(&iter_id_span.client_id).unwrap_or(&EMPTY_VEC); + let list = self.map.get(&iter_id_span.peer).unwrap_or(&EMPTY_VEC); let mut index = 0; let mut offset_in_insert_set = 0; let mut counter = 0; @@ -146,7 +146,7 @@ impl IdToCursor { return Some(IterCursor::Insert { leaf: elem.leaf, id_span: IdSpan::new( - iter_id_span.client_id, + iter_id_span.peer, start_counter .max(iter_id_span.counter.start) .min(iter_id_span.counter.end), diff --git a/crates/loro-internal/src/dag/mermaid.rs b/crates/loro-internal/src/dag/mermaid.rs index 8e08a240..b9d17290 100644 --- a/crates/loro-internal/src/dag/mermaid.rs +++ b/crates/loro-internal/src/dag/mermaid.rs @@ -41,9 +41,9 @@ fn to_str(output: Output) -> String { is_first = false; s += format!( "{}-{}(\"c{}: [{}, {})\")", - id_span.client_id, + id_span.peer, id_span.counter.start, - id_span.client_id, + id_span.peer, id_span.counter.start, id_span.counter.end ) diff --git a/crates/loro-internal/src/oplog.rs b/crates/loro-internal/src/oplog.rs index 97b51e11..665800a8 100644 --- a/crates/loro-internal/src/oplog.rs +++ b/crates/loro-internal/src/oplog.rs @@ -465,7 +465,7 @@ impl OpLog { pub(crate) fn iter_ops(&self, id_span: IdSpan) -> impl Iterator + '_ { self.changes - .get(&id_span.client_id) + .get(&id_span.peer) .map(move |changes| { let len = changes.len(); let start = changes @@ -830,7 +830,7 @@ impl OpLog { ) -> impl Iterator + 'a { let spans: Vec<_> = from.diff_iter(to).1.collect(); spans.into_iter().flat_map(move |span| { - let peer = span.client_id; + let peer = span.peer; let cnt = span.counter.start; let end_cnt = span.counter.end; let peer_changes = self.changes.get(&peer).unwrap(); diff --git a/crates/loro-internal/src/version.rs b/crates/loro-internal/src/version.rs index fd3a4b67..e4eff7e9 100644 --- a/crates/loro-internal/src/version.rs +++ b/crates/loro-internal/src/version.rs @@ -262,22 +262,22 @@ impl VersionVectorDiff { } pub fn get_id_spans_left(&self) -> impl Iterator + '_ { - self.left.iter().map(|(client_id, span)| IdSpan { - client_id: *client_id, + self.left.iter().map(|(peer, span)| IdSpan { + peer: *peer, counter: *span, }) } pub fn get_id_spans_right(&self) -> impl Iterator + '_ { - self.right.iter().map(|(client_id, span)| IdSpan { - client_id: *client_id, + self.right.iter().map(|(peer, span)| IdSpan { + peer: *peer, counter: *span, }) } } fn subtract_start(m: &mut FxHashMap, target: IdSpan) { - if let Some(span) = m.get_mut(&target.client_id) { + if let Some(span) = m.get_mut(&target.peer) { if span.start < target.counter.end { span.start = target.counter.end; } @@ -286,11 +286,11 @@ fn subtract_start(m: &mut FxHashMap, target: IdSpan) { fn merge(m: &mut FxHashMap, mut target: IdSpan) { target.normalize_(); - if let Some(span) = m.get_mut(&target.client_id) { + if let Some(span) = m.get_mut(&target.peer) { span.start = span.start.min(target.counter.start); span.end = span.end.max(target.counter.end); } else { - m.insert(target.client_id, target.counter); + m.insert(target.peer, target.counter); } } @@ -451,11 +451,11 @@ impl VersionVector { /// Returns the spans that are in `self` but not in `rhs` pub fn sub_iter<'a>(&'a self, rhs: &'a Self) -> impl Iterator + 'a { - self.iter().filter_map(move |(client_id, &counter)| { - if let Some(&rhs_counter) = rhs.get(client_id) { + self.iter().filter_map(move |(peer, &counter)| { + if let Some(&rhs_counter) = rhs.get(peer) { if counter > rhs_counter { Some(IdSpan { - client_id: *client_id, + peer: *peer, counter: CounterSpan { start: rhs_counter, end: counter, @@ -466,7 +466,7 @@ impl VersionVector { } } else if counter > 0 { Some(IdSpan { - client_id: *client_id, + peer: *peer, counter: CounterSpan { start: 0, end: counter, @@ -479,9 +479,7 @@ impl VersionVector { } pub fn sub_vec(&self, rhs: &Self) -> IdSpanVector { - self.sub_iter(rhs) - .map(|x| (x.client_id, x.counter)) - .collect() + self.sub_iter(rhs).map(|x| (x.peer, x.counter)).collect() } pub fn distance_to(&self, other: &Self) -> usize { @@ -618,7 +616,7 @@ impl VersionVector { } pub fn intersect_span(&self, target: IdSpan) -> Option { - if let Some(&end) = self.get(&target.client_id) { + if let Some(&end) = self.get(&target.peer) { if end > target.ctr_start() { let count_end = target.ctr_end(); return Some(CounterSpan { @@ -667,22 +665,22 @@ impl VersionVector { } pub fn extend_to_include(&mut self, span: IdSpan) { - if let Some(counter) = self.get_mut(&span.client_id) { + if let Some(counter) = self.get_mut(&span.peer) { if *counter < span.counter.norm_end() { *counter = span.counter.norm_end(); } } else { - self.insert(span.client_id, span.counter.norm_end()); + self.insert(span.peer, span.counter.norm_end()); } } pub fn shrink_to_exclude(&mut self, span: IdSpan) { if span.counter.min() == 0 { - self.remove(&span.client_id); + self.remove(&span.peer); return; } - if let Some(counter) = self.get_mut(&span.client_id) { + if let Some(counter) = self.get_mut(&span.peer) { if *counter > span.counter.min() { *counter = span.counter.min(); } @@ -692,7 +690,7 @@ impl VersionVector { pub fn forward(&mut self, spans: &IdSpanVector) { for span in spans.iter() { self.extend_to_include(IdSpan { - client_id: *span.0, + peer: *span.0, counter: *span.1, }); } @@ -701,7 +699,7 @@ impl VersionVector { pub fn retreat(&mut self, spans: &IdSpanVector) { for span in spans.iter() { self.shrink_to_exclude(IdSpan { - client_id: *span.0, + peer: *span.0, counter: *span.1, }); } @@ -981,35 +979,35 @@ impl PatchedVersionVector { #[inline] pub fn extend_to_include(&mut self, span: IdSpan) { self.patch.extend_to_include(span); - self.omit_if_needless(span.client_id); + self.omit_if_needless(span.peer); } #[inline] pub fn shrink_to_exclude(&mut self, span: IdSpan) { self.patch.shrink_to_exclude(span); - self.omit_if_needless(span.client_id); + self.omit_if_needless(span.peer); } #[inline] pub fn forward(&mut self, spans: &IdSpanVector) { for span in spans.iter() { let span = IdSpan { - client_id: *span.0, + peer: *span.0, counter: *span.1, }; - if let Some(counter) = self.patch.get_mut(&span.client_id) { + if let Some(counter) = self.patch.get_mut(&span.peer) { if *counter < span.counter.norm_end() { *counter = span.counter.norm_end(); - self.omit_if_needless(span.client_id); + self.omit_if_needless(span.peer); } } else { let target = span.counter.norm_end(); - if self.base.get(&span.client_id) == Some(&target) { + if self.base.get(&span.peer) == Some(&target) { continue; } - self.patch.insert(span.client_id, target); + self.patch.insert(span.peer, target); } } } @@ -1018,14 +1016,14 @@ impl PatchedVersionVector { pub fn retreat(&mut self, spans: &IdSpanVector) { for span in spans.iter() { let span = IdSpan { - client_id: *span.0, + peer: *span.0, counter: *span.1, }; - if let Some(counter) = self.patch.get_mut(&span.client_id) { + if let Some(counter) = self.patch.get_mut(&span.peer) { if *counter > span.counter.min() { *counter = span.counter.min(); - self.omit_if_needless(span.client_id); + self.omit_if_needless(span.peer); } } }