Skip to content

Commit

Permalink
style: use clippy to perf code (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lampese authored Jul 24, 2024
1 parent e01505e commit 7e45006
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 42 deletions.
38 changes: 16 additions & 22 deletions crates/loro-internal/src/container/richtext/richtext_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,6 @@ mod cursor_cache {
RichtextTreeTrait,
};
use generic_btree::{rle::HasLength, BTree, Cursor, LeafIndex};
use loro_common::LoroError;

#[derive(Debug, Clone)]
struct CursorCacheItem {
Expand Down Expand Up @@ -1404,25 +1403,22 @@ impl RichtextState {
entity_index: usize,
text: BytesSlice,
id: IdFull,
) {
) -> Cursor {
let elem = RichtextStateChunk::try_new(text, id).unwrap();
self.style_ranges
.as_mut()
.map(|x| x.insert(entity_index, elem.rle_len()));
let leaf;
leaf = {
let q = &entity_index;
match self.tree.query::<EntityQuery>(q) {
Some(result) => {
let p = self
.tree
.prefer_left(result.cursor)
.unwrap_or(result.cursor);
self.tree.insert_by_path(p, elem).0
}
None => self.tree.push(elem),
let q = &entity_index;
match self.tree.query::<EntityQuery>(q) {
Some(result) => {
let p = self
.tree
.prefer_left(result.cursor)
.unwrap_or(result.cursor);
self.tree.insert_by_path(p, elem).0
}
};
None => self.tree.push(elem),
}
}

/// This is used to accept changes from DiffCalculator.
Expand All @@ -1441,13 +1437,11 @@ impl RichtextState {
&self
);

let cursor;
let event_index;
let (c, f) = self
.tree
.query_with_finder_return::<EntityIndexQueryWithEventIndex>(&entity_index);
cursor = c.map(|x| x.cursor);
event_index = f.event_index;
let cursor = c.map(|x| x.cursor);
let event_index = f.event_index;

match cursor {
Some(cursor) => {
Expand All @@ -1456,7 +1450,7 @@ impl RichtextState {
.as_mut()
.map(|x| x.insert(entity_index, elem.rle_len()))
.unwrap_or(&EMPTY_STYLES);
self.tree.insert_by_path(cursor, elem).0;
self.tree.insert_by_path(cursor, elem);
(event_index, styles)
}
None => {
Expand Down Expand Up @@ -1887,8 +1881,8 @@ impl RichtextState {
}

if let RichtextStateChunk::Text(s) = span.elem {
match unicode_slice(&s.as_str(), start, end) {
Ok(x) => ans.push_str(&x),
match unicode_slice(s.as_str(), start, end) {
Ok(x) => ans.push_str(x),
Err(()) => return Err(LoroError::UTF16InUnicodeCodePoint { pos: pos + len }),
}
}
Expand Down
18 changes: 7 additions & 11 deletions crates/loro-internal/src/diff/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl<D: DiffHandler> OperateProxy<D> {
}
}

pub fn flush_del_ins(&mut self) -> () {
pub fn flush_del_ins(&mut self) {
if let Some((del_old_index, del_old_len)) = self.del.take() {
if let Some((_, ins_new_index, ins_new_len)) = self.ins.take() {
self.handler.replace(
Expand All @@ -94,27 +94,27 @@ impl<D: DiffHandler> OperateProxy<D> {
} else {
self.handler
.delete((del_old_index as isize + self.offset) as usize, del_old_len);
self.offset = self.offset - del_old_len as isize;
self.offset -= del_old_len as isize
}
} else if let Some((ins_old_index, ins_new_index, ins_new_len)) = self.ins.take() {
self.handler.insert(
(ins_old_index as isize + self.offset) as usize,
ins_new_index,
ins_new_len,
);
self.offset = self.offset + ins_new_len as isize;
self.offset += ins_new_len as isize
}
}

pub fn delete(&mut self, old_index: usize, old_len: usize) -> () {
pub fn delete(&mut self, old_index: usize, old_len: usize) {
if let Some((del_old_index, del_old_len)) = self.del.take() {
self.del = Some((del_old_index, del_old_len + old_len));
} else {
self.del = Some((old_index, old_len));
}
}

pub fn insert(&mut self, old_index: usize, new_index: usize, new_len: usize) -> () {
pub fn insert(&mut self, old_index: usize, new_index: usize, new_len: usize) {
self.ins = if let Some((ins_old_index, ins_new_index, ins_new_len)) = self.ins.take() {
Some((ins_old_index, ins_new_index, new_len + ins_new_len))
} else {
Expand All @@ -123,11 +123,7 @@ impl<D: DiffHandler> OperateProxy<D> {
}
}

pub(crate) fn myers_diff<D: DiffHandler>(
proxy: &mut OperateProxy<D>,
old: &[char],
new: &[char],
) -> () {
pub(crate) fn myers_diff<D: DiffHandler>(proxy: &mut OperateProxy<D>, old: &[char], new: &[char]) {
let max_d = (old.len() + new.len() + 1) / 2 + 1;
let mut vb = OffsetVec::new(max_d);
let mut vf = OffsetVec::new(max_d);
Expand Down Expand Up @@ -243,7 +239,7 @@ fn conquer<D: DiffHandler>(
mut new_end: usize,
vf: &mut OffsetVec,
vb: &mut OffsetVec,
) -> () {
) {
let common_prefix_len = common_prefix(&old[old_start..old_end], &new[new_start..new_end]);
if common_prefix_len > 0 {
proxy.flush_del_ins();
Expand Down
13 changes: 6 additions & 7 deletions crates/loro-internal/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1384,7 +1384,7 @@ impl TextHandler {
}
}

pub fn iter(&self, mut callback: impl FnMut(&str) -> bool) -> () {
pub fn iter(&self, mut callback: impl FnMut(&str) -> bool) {
match &self.inner {
MaybeDetached::Detached(t) => {
let t = t.try_lock().unwrap();
Expand All @@ -1395,7 +1395,7 @@ impl TextHandler {
}
}
MaybeDetached::Attached(a) => {
a.with_state(|state| -> () {
a.with_state(|state| {
state.as_richtext_state_mut().unwrap().iter(callback);
});
}
Expand All @@ -1409,7 +1409,7 @@ impl TextHandler {
pub fn char_at(&self, pos: usize) -> LoroResult<char> {
if pos >= self.len_event() {
return Err(LoroError::OutOfBound {
pos: pos,
pos,
len: self.len_event(),
info: format!("Position: {}:{}", file!(), line!()).into_boxed_str(),
});
Expand All @@ -1429,7 +1429,7 @@ impl TextHandler {
Ok(c)
} else {
Err(LoroError::OutOfBound {
pos: pos,
pos,
len: self.len_event(),
info: format!("Position: {}:{}", file!(), line!()).into_boxed_str(),
})
Expand Down Expand Up @@ -2151,7 +2151,7 @@ impl TextHandler {
Ok(())
}

pub fn update(&self, text: &str) -> () {
pub fn update(&self, text: &str) {
let old_str = self.to_string();
let new = text.chars().collect::<Vec<char>>();
myers_diff(
Expand Down Expand Up @@ -3375,7 +3375,7 @@ impl MovableListHandler {
loro_delta::DeltaItem::Replace {
value,
delete,
attr,
attr: _,
} => {
if *delete > 0 {
// skip the deletion if it is already processed by moving
Expand Down Expand Up @@ -3939,7 +3939,6 @@ pub mod counter {
mod test {

use super::{HandlerTrait, TextDelta};
use crate::container::richtext::richtext_state::PosType;
use crate::loro::LoroDoc;
use crate::version::Frontiers;
use crate::{fx_map, ToJson};
Expand Down
2 changes: 1 addition & 1 deletion crates/loro-internal/src/state/richtext_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl RichtextState {
self.state.get_mut().get_char_by_event_index(pos)
}

pub(crate) fn iter(&mut self, mut callback: impl FnMut(&str) -> bool) -> () {
pub(crate) fn iter(&mut self, mut callback: impl FnMut(&str) -> bool) {
for span in self.state.get_mut().iter() {
if !callback(span.text.as_str()) {
return;
Expand Down
2 changes: 1 addition & 1 deletion crates/loro-internal/src/undo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ impl Default for Stack {

impl UndoManagerInner {
fn new(last_counter: Counter) -> Self {
UndoManagerInner {
Self {
latest_counter: Some(last_counter),
undo_stack: Default::default(),
redo_stack: Default::default(),
Expand Down

0 comments on commit 7e45006

Please sign in to comment.