From 99d02d91028d6bf879ee062b42433ffce34d7a33 Mon Sep 17 00:00:00 2001 From: Cong-Cong Date: Fri, 11 Oct 2024 16:09:34 +0800 Subject: [PATCH 1/2] perf: inner use BoxSource is better --- src/cached_source.rs | 36 +++++++++++++++++------------------- src/helpers.rs | 8 ++++---- src/replace_source.rs | 40 +++++++++++++++++++++------------------- 3 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/cached_source.rs b/src/cached_source.rs index d6b7b201..fc446bf1 100644 --- a/src/cached_source.rs +++ b/src/cached_source.rs @@ -12,7 +12,7 @@ use crate::{ stream_and_get_source_and_map, stream_chunks_of_raw_source, stream_chunks_of_source_map, StreamChunks, }, - MapOptions, Source, SourceMap, + BoxSource, MapOptions, Source, SourceExt, SourceMap, }; /// It tries to reused cached results from other methods to avoid calculations, @@ -48,8 +48,8 @@ use crate::{ /// "Hello World\nconsole.log('test');\nconsole.log('test2');\nHello2\n" /// ); /// ``` -pub struct CachedSource { - inner: Arc, +pub struct CachedSource { + inner: BoxSource, cached_buffer: Arc>>, cached_source: Arc>>, cached_size: Arc>, @@ -57,11 +57,11 @@ pub struct CachedSource { Arc, BuildHasherDefault>>, } -impl CachedSource { +impl CachedSource { /// Create a [CachedSource] with the original [Source]. - pub fn new(inner: T) -> Self { + pub fn new(inner: T) -> Self { Self { - inner: Arc::new(inner), + inner: SourceExt::boxed(inner), cached_buffer: Default::default(), cached_source: Default::default(), cached_size: Default::default(), @@ -70,12 +70,12 @@ impl CachedSource { } /// Get the original [Source]. - pub fn original(&self) -> &T { + pub fn original(&self) -> &dyn Source { &self.inner } } -impl Source for CachedSource { +impl Source for CachedSource { fn source(&self) -> Cow { let cached = self .cached_source @@ -116,9 +116,7 @@ impl Source for CachedSource { } } -impl StreamChunks<'_> - for CachedSource -{ +impl StreamChunks<'_> for CachedSource { fn stream_chunks<'a>( &'a self, options: &MapOptions, @@ -152,7 +150,7 @@ impl StreamChunks<'_> } Entry::Vacant(entry) => { let (generated_info, map) = stream_and_get_source_and_map( - &self.inner as &T, + &self.inner, options, on_chunk, on_source, @@ -165,7 +163,7 @@ impl StreamChunks<'_> } } -impl Clone for CachedSource { +impl Clone for CachedSource { fn clone(&self) -> Self { Self { inner: self.inner.clone(), @@ -177,27 +175,27 @@ impl Clone for CachedSource { } } -impl Hash for CachedSource { +impl Hash for CachedSource { fn hash(&self, state: &mut H) { self.inner.hash(state); } } -impl PartialEq for CachedSource { +impl PartialEq for CachedSource { fn eq(&self, other: &Self) -> bool { - self.inner == other.inner + self.inner.as_ref() == other.inner.as_ref() } } -impl Eq for CachedSource {} +impl Eq for CachedSource {} -impl std::fmt::Debug for CachedSource { +impl std::fmt::Debug for CachedSource { fn fmt( &self, f: &mut std::fmt::Formatter<'_>, ) -> Result<(), std::fmt::Error> { f.debug_struct("CachedSource") - .field("inner", self.inner.as_ref()) + .field("inner", &self.inner) .field("cached_buffer", &self.cached_buffer.get().is_some()) .field("cached_source", &self.cached_source.get().is_some()) .field("cached_maps", &(!self.cached_maps.is_empty())) diff --git a/src/helpers.rs b/src/helpers.rs index ce2e3cf3..6ed746ab 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -18,8 +18,8 @@ use crate::{ type InnerSourceContentLine<'a> = RefCell>>>>>; -pub fn get_map<'a, S: StreamChunks<'a>>( - stream: &'a S, +pub fn get_map<'a>( + stream: &'a dyn StreamChunks<'a>, options: &'a MapOptions, ) -> Option { let mut mappings_encoder = create_encoder(options.columns); @@ -1083,8 +1083,8 @@ pub fn stream_chunks_of_combined_source_map<'a>( ) } -pub fn stream_and_get_source_and_map<'a, S: StreamChunks<'a>>( - input_source: &'a S, +pub fn stream_and_get_source_and_map<'a>( + input_source: &'a dyn StreamChunks<'a>, options: &MapOptions, on_chunk: OnChunk<'_, 'a>, on_source: OnSource<'_, 'a>, diff --git a/src/replace_source.rs b/src/replace_source.rs index 3d555fb3..90bad657 100644 --- a/src/replace_source.rs +++ b/src/replace_source.rs @@ -4,7 +4,7 @@ use std::{ hash::{Hash, Hasher}, sync::{ atomic::{AtomicBool, Ordering}, - Arc, Mutex, MutexGuard, OnceLock, + Mutex, MutexGuard, OnceLock, }, }; @@ -13,7 +13,8 @@ use rustc_hash::FxHashMap as HashMap; use crate::{ helpers::{get_map, split_into_lines, GeneratedInfo, StreamChunks}, linear_map::LinearMap, - MapOptions, Mapping, OriginalLocation, Source, SourceMap, + BoxSource, MapOptions, Mapping, OriginalLocation, Source, SourceExt, + SourceMap, }; /// Decorates a Source with replacements and insertions of source code, @@ -35,8 +36,8 @@ use crate::{ /// /// assert_eq!(source.source(), "start1\nstart2\nreplaced!\nend1\nend2"); /// ``` -pub struct ReplaceSource { - inner: Arc, +pub struct ReplaceSource { + inner: BoxSource, inner_source_code: OnceLock>, replacements: Mutex>, /// Whether `replacements` is sorted. @@ -82,11 +83,11 @@ impl Replacement { } } -impl ReplaceSource { +impl ReplaceSource { /// Create a [ReplaceSource]. - pub fn new(source: T) -> Self { + pub fn new(source: T) -> Self { Self { - inner: Arc::new(source), + inner: SourceExt::boxed(source), inner_source_code: OnceLock::new(), replacements: Mutex::new(Vec::new()), is_sorted: AtomicBool::new(true), @@ -94,8 +95,8 @@ impl ReplaceSource { } /// Get the original [Source]. - pub fn original(&self) -> &T { - &self.inner + pub fn original(&self) -> BoxSource { + self.inner.clone() } fn replacements(&self) -> MutexGuard> { @@ -113,7 +114,7 @@ impl ReplaceSource { } } -impl ReplaceSource { +impl ReplaceSource { fn get_inner_source_code(&self) -> &str { self .inner_source_code @@ -174,7 +175,7 @@ impl ReplaceSource { } } -impl Source for ReplaceSource { +impl Source for ReplaceSource { fn source(&self) -> Cow { self.sort_replacement(); @@ -233,13 +234,13 @@ impl Source for ReplaceSource { } } -impl std::fmt::Debug for ReplaceSource { +impl std::fmt::Debug for ReplaceSource { fn fmt( &self, f: &mut std::fmt::Formatter<'_>, ) -> Result<(), std::fmt::Error> { f.debug_struct("ReplaceSource") - .field("inner", self.inner.as_ref()) + .field("inner", &self.inner) .field( "inner_source_code", &self @@ -283,7 +284,7 @@ fn check_content_at_position( } } -impl<'a, T: Source> StreamChunks<'a> for ReplaceSource { +impl<'a> StreamChunks<'a> for ReplaceSource { fn stream_chunks( &'a self, options: &crate::MapOptions, @@ -698,7 +699,7 @@ impl<'a, T: Source> StreamChunks<'a> for ReplaceSource { } } -impl Clone for ReplaceSource { +impl Clone for ReplaceSource { fn clone(&self) -> Self { Self { inner: self.inner.clone(), @@ -709,7 +710,7 @@ impl Clone for ReplaceSource { } } -impl Hash for ReplaceSource { +impl Hash for ReplaceSource { fn hash(&self, state: &mut H) { self.sort_replacement(); "ReplaceSource".hash(state); @@ -720,13 +721,14 @@ impl Hash for ReplaceSource { } } -impl PartialEq for ReplaceSource { +impl PartialEq for ReplaceSource { fn eq(&self, other: &Self) -> bool { - self.inner == other.inner && *self.replacements() == *other.replacements() + self.inner.as_ref() == other.inner.as_ref() + && *self.replacements() == *other.replacements() } } -impl Eq for ReplaceSource {} +impl Eq for ReplaceSource {} #[cfg(test)] mod tests { From 8cff2bef0222e92e1fbaa112af67a1c9c6eed2a6 Mon Sep 17 00:00:00 2001 From: Cong-Cong Date: Fri, 11 Oct 2024 16:15:59 +0800 Subject: [PATCH 2/2] chore: upgrade CodSpeedHQ/action --- .github/workflows/Bench.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/Bench.yaml b/.github/workflows/Bench.yaml index 902a8416..18516e5d 100644 --- a/.github/workflows/Bench.yaml +++ b/.github/workflows/Bench.yaml @@ -19,7 +19,7 @@ concurrency: jobs: benchmark: - runs-on: ubuntu-latest + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v4 @@ -35,7 +35,7 @@ jobs: run: cargo codspeed build --features codspeed - name: Run benchmark - uses: CodSpeedHQ/action@v1 + uses: CodSpeedHQ/action@v3 timeout-minutes: 30 with: run: cargo codspeed run