Skip to content

Commit

Permalink
refactor: removing parking_lock (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen authored Nov 29, 2023
1 parent e457a98 commit 438d444
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 25 deletions.
11 changes: 0 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ serde_json = "1"
dyn-clone = "1"
rustc-hash = "1"
once_cell = "1"
parking_lot = "0.12"
dashmap = "5"
substring = "1"
smallvec = "1.11.2"
Expand Down
27 changes: 14 additions & 13 deletions src/replace_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ use std::{
hash::{Hash, Hasher},
sync::{
atomic::{AtomicBool, Ordering},
Arc,
Arc, Mutex, MutexGuard,
},
};

use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use rustc_hash::FxHashMap as HashMap;

use crate::{
Expand Down Expand Up @@ -103,13 +102,16 @@ impl<T> ReplaceSource<T> {
&self.inner
}

fn replacements(&self) -> MutexGuard<Vec<Replacement>> {
self.replacements.lock().unwrap()
}

fn sort_replacement(&self) {
if self.is_sorted.load(Ordering::SeqCst) {
return;
}
self
.replacements
.lock()
.replacements()
.sort_by(|a, b| (a.start, a.end).cmp(&(b.start, b.end)));
self.is_sorted.store(true, Ordering::SeqCst)
}
Expand All @@ -135,7 +137,7 @@ impl<T: Source> ReplaceSource<T> {
content: &str,
name: Option<&str>,
) {
self.replacements.lock().push(Replacement::new(
self.replacements().push(Replacement::new(
start,
end,
content.into(),
Expand All @@ -155,7 +157,7 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> Source for ReplaceSource<T> {
// concatenate strings benchmark, see https://github.com/hoodie/concatenation_benchmarks-rs
let mut source_code = String::new();
let mut inner_pos = 0;
for replacement in self.replacements.lock().iter() {
for replacement in self.replacements.lock().unwrap().iter() {
if inner_pos < replacement.start {
let end_pos = (replacement.start as usize).min(inner_source_code.len());
source_code.push_str(&inner_source_code[inner_pos as usize..end_pos]);
Expand Down Expand Up @@ -185,7 +187,7 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> Source for ReplaceSource<T> {
}

fn map(&self, options: &crate::MapOptions) -> Option<SourceMap> {
let replacements = self.replacements.lock();
let replacements = self.replacements.lock().unwrap();
if replacements.is_empty() {
return self.inner.map(options);
}
Expand Down Expand Up @@ -223,7 +225,7 @@ impl<T: std::fmt::Debug> std::fmt::Debug for ReplaceSource<T> {

impl<T: Source> StreamChunks for ReplaceSource<T> {
fn mappings_size_hint(&self) -> usize {
self.replacements.lock().len() * 2
self.replacements.lock().unwrap().len() * 2
}

fn stream_chunks(
Expand All @@ -235,7 +237,7 @@ impl<T: Source> StreamChunks for ReplaceSource<T> {
) -> crate::helpers::GeneratedInfo {
self.sort_replacement();
let on_name = RefCell::new(on_name);
let repls = self.replacements.lock();
let repls = self.replacements();
let mut pos: u32 = 0;
let mut i: usize = 0;
let mut replacement_end: Option<u32> = None;
Expand Down Expand Up @@ -646,7 +648,7 @@ impl<T: Source> Clone for ReplaceSource<T> {
Self {
inner: self.inner.clone(),
inner_source_code: self.inner_source_code.clone(),
replacements: Mutex::new(self.replacements.lock().clone()),
replacements: Mutex::new(self.replacements().clone()),
is_sorted: AtomicBool::new(true),
}
}
Expand All @@ -656,7 +658,7 @@ impl<T: Hash> Hash for ReplaceSource<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.sort_replacement();
"ReplaceSource".hash(state);
for repl in self.replacements.lock().iter() {
for repl in self.replacements().iter() {
repl.hash(state);
}
self.inner.hash(state);
Expand All @@ -665,8 +667,7 @@ impl<T: Hash> Hash for ReplaceSource<T> {

impl<T: PartialEq> PartialEq for ReplaceSource<T> {
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner
&& *self.replacements.lock() == *other.replacements.lock()
self.inner == other.inner && *self.replacements() == *other.replacements()
}
}

Expand Down

0 comments on commit 438d444

Please sign in to comment.