From 438d444ddfa38a99b3386a4622e4dad2d7cb9f33 Mon Sep 17 00:00:00 2001 From: Boshen Date: Wed, 29 Nov 2023 17:47:20 +0800 Subject: [PATCH] refactor: removing `parking_lock` (#79) --- Cargo.lock | 11 ----------- Cargo.toml | 1 - src/replace_source.rs | 27 ++++++++++++++------------- 3 files changed, 14 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7acfedbc..82aa52ec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -332,16 +332,6 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f222829ae9293e33a9f5e9f440c6760a3d450a64affe1846486b140db81c1f4" -[[package]] -name = "parking_lot" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" -dependencies = [ - "lock_api", - "parking_lot_core", -] - [[package]] name = "parking_lot_core" version = "0.9.9" @@ -458,7 +448,6 @@ dependencies = [ "dyn-clone", "memchr", "once_cell", - "parking_lot", "regex", "rustc-hash", "serde", diff --git a/Cargo.toml b/Cargo.toml index 26ad74b8..17d9845c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/replace_source.rs b/src/replace_source.rs index 6cbbf0ed..48839e77 100644 --- a/src/replace_source.rs +++ b/src/replace_source.rs @@ -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::{ @@ -103,13 +102,16 @@ impl ReplaceSource { &self.inner } + fn replacements(&self) -> MutexGuard> { + 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) } @@ -135,7 +137,7 @@ impl ReplaceSource { content: &str, name: Option<&str>, ) { - self.replacements.lock().push(Replacement::new( + self.replacements().push(Replacement::new( start, end, content.into(), @@ -155,7 +157,7 @@ impl Source for ReplaceSource { // 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]); @@ -185,7 +187,7 @@ impl Source for ReplaceSource { } fn map(&self, options: &crate::MapOptions) -> Option { - let replacements = self.replacements.lock(); + let replacements = self.replacements.lock().unwrap(); if replacements.is_empty() { return self.inner.map(options); } @@ -223,7 +225,7 @@ impl std::fmt::Debug for ReplaceSource { impl StreamChunks for ReplaceSource { fn mappings_size_hint(&self) -> usize { - self.replacements.lock().len() * 2 + self.replacements.lock().unwrap().len() * 2 } fn stream_chunks( @@ -235,7 +237,7 @@ impl StreamChunks for ReplaceSource { ) -> 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 = None; @@ -646,7 +648,7 @@ impl Clone for ReplaceSource { 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), } } @@ -656,7 +658,7 @@ impl Hash for ReplaceSource { fn hash(&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); @@ -665,8 +667,7 @@ impl Hash for ReplaceSource { impl PartialEq for ReplaceSource { fn eq(&self, other: &Self) -> bool { - self.inner == other.inner - && *self.replacements.lock() == *other.replacements.lock() + self.inner == other.inner && *self.replacements() == *other.replacements() } }