Skip to content

Commit 99d02d9

Browse files
committed
perf: inner use BoxSource is better
1 parent 4859f87 commit 99d02d9

File tree

3 files changed

+42
-42
lines changed

3 files changed

+42
-42
lines changed

src/cached_source.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
stream_and_get_source_and_map, stream_chunks_of_raw_source,
1313
stream_chunks_of_source_map, StreamChunks,
1414
},
15-
MapOptions, Source, SourceMap,
15+
BoxSource, MapOptions, Source, SourceExt, SourceMap,
1616
};
1717

1818
/// It tries to reused cached results from other methods to avoid calculations,
@@ -48,20 +48,20 @@ use crate::{
4848
/// "Hello World\nconsole.log('test');\nconsole.log('test2');\nHello2\n"
4949
/// );
5050
/// ```
51-
pub struct CachedSource<T> {
52-
inner: Arc<T>,
51+
pub struct CachedSource {
52+
inner: BoxSource,
5353
cached_buffer: Arc<OnceLock<Vec<u8>>>,
5454
cached_source: Arc<OnceLock<Arc<str>>>,
5555
cached_size: Arc<OnceLock<usize>>,
5656
cached_maps:
5757
Arc<DashMap<MapOptions, Option<SourceMap>, BuildHasherDefault<FxHasher>>>,
5858
}
5959

60-
impl<T> CachedSource<T> {
60+
impl CachedSource {
6161
/// Create a [CachedSource] with the original [Source].
62-
pub fn new(inner: T) -> Self {
62+
pub fn new<T: Source + 'static>(inner: T) -> Self {
6363
Self {
64-
inner: Arc::new(inner),
64+
inner: SourceExt::boxed(inner),
6565
cached_buffer: Default::default(),
6666
cached_source: Default::default(),
6767
cached_size: Default::default(),
@@ -70,12 +70,12 @@ impl<T> CachedSource<T> {
7070
}
7171

7272
/// Get the original [Source].
73-
pub fn original(&self) -> &T {
73+
pub fn original(&self) -> &dyn Source {
7474
&self.inner
7575
}
7676
}
7777

78-
impl<T: Source + Hash + PartialEq + Eq + 'static> Source for CachedSource<T> {
78+
impl Source for CachedSource {
7979
fn source(&self) -> Cow<str> {
8080
let cached = self
8181
.cached_source
@@ -116,9 +116,7 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> Source for CachedSource<T> {
116116
}
117117
}
118118

119-
impl<T: Source + Hash + PartialEq + Eq + 'static> StreamChunks<'_>
120-
for CachedSource<T>
121-
{
119+
impl StreamChunks<'_> for CachedSource {
122120
fn stream_chunks<'a>(
123121
&'a self,
124122
options: &MapOptions,
@@ -152,7 +150,7 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> StreamChunks<'_>
152150
}
153151
Entry::Vacant(entry) => {
154152
let (generated_info, map) = stream_and_get_source_and_map(
155-
&self.inner as &T,
153+
&self.inner,
156154
options,
157155
on_chunk,
158156
on_source,
@@ -165,7 +163,7 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> StreamChunks<'_>
165163
}
166164
}
167165

168-
impl<T: Source> Clone for CachedSource<T> {
166+
impl Clone for CachedSource {
169167
fn clone(&self) -> Self {
170168
Self {
171169
inner: self.inner.clone(),
@@ -177,27 +175,27 @@ impl<T: Source> Clone for CachedSource<T> {
177175
}
178176
}
179177

180-
impl<T: Hash> Hash for CachedSource<T> {
178+
impl Hash for CachedSource {
181179
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
182180
self.inner.hash(state);
183181
}
184182
}
185183

186-
impl<T: PartialEq> PartialEq for CachedSource<T> {
184+
impl PartialEq for CachedSource {
187185
fn eq(&self, other: &Self) -> bool {
188-
self.inner == other.inner
186+
self.inner.as_ref() == other.inner.as_ref()
189187
}
190188
}
191189

192-
impl<T: Eq> Eq for CachedSource<T> {}
190+
impl Eq for CachedSource {}
193191

194-
impl<T: std::fmt::Debug> std::fmt::Debug for CachedSource<T> {
192+
impl std::fmt::Debug for CachedSource {
195193
fn fmt(
196194
&self,
197195
f: &mut std::fmt::Formatter<'_>,
198196
) -> Result<(), std::fmt::Error> {
199197
f.debug_struct("CachedSource")
200-
.field("inner", self.inner.as_ref())
198+
.field("inner", &self.inner)
201199
.field("cached_buffer", &self.cached_buffer.get().is_some())
202200
.field("cached_source", &self.cached_source.get().is_some())
203201
.field("cached_maps", &(!self.cached_maps.is_empty()))

src/helpers.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use crate::{
1818
type InnerSourceContentLine<'a> =
1919
RefCell<LinearMap<OnceCell<Option<Vec<WithIndices<&'a str>>>>>>;
2020

21-
pub fn get_map<'a, S: StreamChunks<'a>>(
22-
stream: &'a S,
21+
pub fn get_map<'a>(
22+
stream: &'a dyn StreamChunks<'a>,
2323
options: &'a MapOptions,
2424
) -> Option<SourceMap> {
2525
let mut mappings_encoder = create_encoder(options.columns);
@@ -1083,8 +1083,8 @@ pub fn stream_chunks_of_combined_source_map<'a>(
10831083
)
10841084
}
10851085

1086-
pub fn stream_and_get_source_and_map<'a, S: StreamChunks<'a>>(
1087-
input_source: &'a S,
1086+
pub fn stream_and_get_source_and_map<'a>(
1087+
input_source: &'a dyn StreamChunks<'a>,
10881088
options: &MapOptions,
10891089
on_chunk: OnChunk<'_, 'a>,
10901090
on_source: OnSource<'_, 'a>,

src/replace_source.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
hash::{Hash, Hasher},
55
sync::{
66
atomic::{AtomicBool, Ordering},
7-
Arc, Mutex, MutexGuard, OnceLock,
7+
Mutex, MutexGuard, OnceLock,
88
},
99
};
1010

@@ -13,7 +13,8 @@ use rustc_hash::FxHashMap as HashMap;
1313
use crate::{
1414
helpers::{get_map, split_into_lines, GeneratedInfo, StreamChunks},
1515
linear_map::LinearMap,
16-
MapOptions, Mapping, OriginalLocation, Source, SourceMap,
16+
BoxSource, MapOptions, Mapping, OriginalLocation, Source, SourceExt,
17+
SourceMap,
1718
};
1819

1920
/// Decorates a Source with replacements and insertions of source code,
@@ -35,8 +36,8 @@ use crate::{
3536
///
3637
/// assert_eq!(source.source(), "start1\nstart2\nreplaced!\nend1\nend2");
3738
/// ```
38-
pub struct ReplaceSource<T> {
39-
inner: Arc<T>,
39+
pub struct ReplaceSource {
40+
inner: BoxSource,
4041
inner_source_code: OnceLock<Box<str>>,
4142
replacements: Mutex<Vec<Replacement>>,
4243
/// Whether `replacements` is sorted.
@@ -82,20 +83,20 @@ impl Replacement {
8283
}
8384
}
8485

85-
impl<T> ReplaceSource<T> {
86+
impl ReplaceSource {
8687
/// Create a [ReplaceSource].
87-
pub fn new(source: T) -> Self {
88+
pub fn new<T: Source + 'static>(source: T) -> Self {
8889
Self {
89-
inner: Arc::new(source),
90+
inner: SourceExt::boxed(source),
9091
inner_source_code: OnceLock::new(),
9192
replacements: Mutex::new(Vec::new()),
9293
is_sorted: AtomicBool::new(true),
9394
}
9495
}
9596

9697
/// Get the original [Source].
97-
pub fn original(&self) -> &T {
98-
&self.inner
98+
pub fn original(&self) -> BoxSource {
99+
self.inner.clone()
99100
}
100101

101102
fn replacements(&self) -> MutexGuard<Vec<Replacement>> {
@@ -113,7 +114,7 @@ impl<T> ReplaceSource<T> {
113114
}
114115
}
115116

116-
impl<T: Source> ReplaceSource<T> {
117+
impl ReplaceSource {
117118
fn get_inner_source_code(&self) -> &str {
118119
self
119120
.inner_source_code
@@ -174,7 +175,7 @@ impl<T: Source> ReplaceSource<T> {
174175
}
175176
}
176177

177-
impl<T: Source + Hash + PartialEq + Eq + 'static> Source for ReplaceSource<T> {
178+
impl Source for ReplaceSource {
178179
fn source(&self) -> Cow<str> {
179180
self.sort_replacement();
180181

@@ -233,13 +234,13 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> Source for ReplaceSource<T> {
233234
}
234235
}
235236

236-
impl<T: std::fmt::Debug> std::fmt::Debug for ReplaceSource<T> {
237+
impl std::fmt::Debug for ReplaceSource {
237238
fn fmt(
238239
&self,
239240
f: &mut std::fmt::Formatter<'_>,
240241
) -> Result<(), std::fmt::Error> {
241242
f.debug_struct("ReplaceSource")
242-
.field("inner", self.inner.as_ref())
243+
.field("inner", &self.inner)
243244
.field(
244245
"inner_source_code",
245246
&self
@@ -283,7 +284,7 @@ fn check_content_at_position(
283284
}
284285
}
285286

286-
impl<'a, T: Source> StreamChunks<'a> for ReplaceSource<T> {
287+
impl<'a> StreamChunks<'a> for ReplaceSource {
287288
fn stream_chunks(
288289
&'a self,
289290
options: &crate::MapOptions,
@@ -698,7 +699,7 @@ impl<'a, T: Source> StreamChunks<'a> for ReplaceSource<T> {
698699
}
699700
}
700701

701-
impl<T: Source> Clone for ReplaceSource<T> {
702+
impl Clone for ReplaceSource {
702703
fn clone(&self) -> Self {
703704
Self {
704705
inner: self.inner.clone(),
@@ -709,7 +710,7 @@ impl<T: Source> Clone for ReplaceSource<T> {
709710
}
710711
}
711712

712-
impl<T: Hash> Hash for ReplaceSource<T> {
713+
impl Hash for ReplaceSource {
713714
fn hash<H: Hasher>(&self, state: &mut H) {
714715
self.sort_replacement();
715716
"ReplaceSource".hash(state);
@@ -720,13 +721,14 @@ impl<T: Hash> Hash for ReplaceSource<T> {
720721
}
721722
}
722723

723-
impl<T: PartialEq> PartialEq for ReplaceSource<T> {
724+
impl PartialEq for ReplaceSource {
724725
fn eq(&self, other: &Self) -> bool {
725-
self.inner == other.inner && *self.replacements() == *other.replacements()
726+
self.inner.as_ref() == other.inner.as_ref()
727+
&& *self.replacements() == *other.replacements()
726728
}
727729
}
728730

729-
impl<T: Eq> Eq for ReplaceSource<T> {}
731+
impl Eq for ReplaceSource {}
730732

731733
#[cfg(test)]
732734
mod tests {

0 commit comments

Comments
 (0)