Skip to content

Commit

Permalink
perf: add is_sorted to ReplaceSource (#77)
Browse files Browse the repository at this point in the history
closes #71
  • Loading branch information
Boshen authored Nov 29, 2023
1 parent 92e3bf0 commit e457a98
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/replace_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use std::{
borrow::Cow,
cell::RefCell,
hash::{Hash, Hasher},
sync::Arc,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
};

use once_cell::sync::OnceCell;
Expand Down Expand Up @@ -38,6 +41,8 @@ pub struct ReplaceSource<T> {
inner: Arc<T>,
inner_source_code: OnceCell<Box<str>>,
replacements: Mutex<Vec<Replacement>>,
/// Whether `replacements` is sorted.
is_sorted: AtomicBool,
}

#[derive(Debug, Clone, Eq)]
Expand Down Expand Up @@ -89,6 +94,7 @@ impl<T> ReplaceSource<T> {
inner: Arc::new(source),
inner_source_code: OnceCell::new(),
replacements: Mutex::new(Vec::new()),
is_sorted: AtomicBool::new(true),
}
}

Expand All @@ -98,10 +104,14 @@ impl<T> ReplaceSource<T> {
}

fn sort_replacement(&self) {
if self.is_sorted.load(Ordering::SeqCst) {
return;
}
self
.replacements
.lock()
.sort_by(|a, b| (a.start, a.end).cmp(&(b.start, b.end)));
self.is_sorted.store(true, Ordering::SeqCst)
}
}

Expand Down Expand Up @@ -131,6 +141,7 @@ impl<T: Source> ReplaceSource<T> {
content.into(),
name.map(|s| s.into()),
));
self.is_sorted.store(false, Ordering::SeqCst);
}
}

Expand Down Expand Up @@ -205,6 +216,7 @@ impl<T: std::fmt::Debug> std::fmt::Debug for ReplaceSource<T> {
"replacements",
&self.replacements.lock().iter().take(3).collect::<Vec<_>>(),
)
.field("is_sorted", &self.is_sorted.load(Ordering::SeqCst))
.finish()
}
}
Expand Down Expand Up @@ -635,6 +647,7 @@ impl<T: Source> Clone for ReplaceSource<T> {
inner: self.inner.clone(),
inner_source_code: self.inner_source_code.clone(),
replacements: Mutex::new(self.replacements.lock().clone()),
is_sorted: AtomicBool::new(true),
}
}
}
Expand Down

0 comments on commit e457a98

Please sign in to comment.