Skip to content

Commit 15f65f4

Browse files
committed
perf: strict lifecycle
1 parent 4259b50 commit 15f65f4

File tree

9 files changed

+204
-198
lines changed

9 files changed

+204
-198
lines changed

src/cached_source.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,24 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> Source for CachedSource<T> {
116116
}
117117
}
118118

119-
impl<T: Source + Hash + PartialEq + Eq + 'static> StreamChunks
119+
impl<T: Source + Hash + PartialEq + Eq + 'static> StreamChunks<'_>
120120
for CachedSource<T>
121121
{
122-
fn stream_chunks(
123-
&self,
122+
fn stream_chunks<'a>(
123+
&'a self,
124124
options: &MapOptions,
125125
on_chunk: crate::helpers::OnChunk,
126-
on_source: crate::helpers::OnSource,
126+
on_source: crate::helpers::OnSource<'_, 'a>,
127127
on_name: crate::helpers::OnName,
128128
) -> crate::helpers::GeneratedInfo {
129129
let cached_map = self.cached_maps.entry(options.clone());
130130
match cached_map {
131131
Entry::Occupied(entry) => {
132-
let source = self.source();
132+
let source = self
133+
.cached_source
134+
.get_or_init(|| self.inner.source().into());
133135
if let Some(map) = entry.get() {
136+
let map = unsafe { std::mem::transmute::<&SourceMap, &'a SourceMap>(map) };
134137
stream_chunks_of_source_map(
135138
&source, map, on_chunk, on_source, on_name, options,
136139
)
@@ -141,15 +144,16 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> StreamChunks
141144
}
142145
}
143146
Entry::Vacant(entry) => {
144-
let (generated_info, map) = stream_and_get_source_and_map(
145-
self.original(),
146-
options,
147-
on_chunk,
148-
on_source,
149-
on_name,
150-
);
151-
entry.insert(map);
152-
generated_info
147+
// let (generated_info, map) = stream_and_get_source_and_map(
148+
// self.inner.as_ref(),
149+
// options,
150+
// on_chunk,
151+
// on_source,
152+
// on_name,
153+
// );
154+
// entry.insert(map);
155+
// generated_info
156+
todo!()
153157
}
154158
}
155159
}

src/concat_source.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,16 @@ impl PartialEq for ConcatSource {
145145
}
146146
impl Eq for ConcatSource {}
147147

148-
impl StreamChunks for ConcatSource {
148+
impl<'a> StreamChunks<'a> for ConcatSource {
149149
fn stream_chunks(
150-
&self,
150+
&'a self,
151151
options: &MapOptions,
152152
on_chunk: OnChunk,
153-
on_source: OnSource,
153+
on_source: OnSource<'_, 'a>,
154154
on_name: OnName,
155155
) -> crate::helpers::GeneratedInfo {
156156
if self.children().len() == 1 {
157-
return self.children()[0]
157+
return self.children[0]
158158
.stream_chunks(options, on_chunk, on_source, on_name);
159159
}
160160
let mut current_line_offset = 0;

src/helpers.rs

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ type ArcStr = Arc<str>;
1919
type InnerSourceContentLine =
2020
RefCell<HashMap<i64, Option<Rc<Vec<WithIndices<ArcStr>>>>>>;
2121

22-
pub fn get_map<S: StreamChunks>(
23-
stream: &S,
24-
options: &MapOptions,
22+
pub fn get_map<'a, S: StreamChunks<'a>>(
23+
stream: &'a S,
24+
options: &'a MapOptions,
2525
) -> Option<SourceMap> {
2626
let mut mappings = Vec::new();
2727
let mut sources: Vec<Cow<'static, str>> = Vec::new();
@@ -65,13 +65,13 @@ pub fn get_map<S: StreamChunks>(
6565
}
6666

6767
/// [StreamChunks] abstraction, see [webpack-sources source.streamChunks](https://github.com/webpack/webpack-sources/blob/9f98066311d53a153fdc7c633422a1d086528027/lib/helpers/streamChunks.js#L13).
68-
pub trait StreamChunks {
68+
pub trait StreamChunks<'a> {
6969
/// [StreamChunks] abstraction
7070
fn stream_chunks(
71-
&self,
71+
&'a self,
7272
options: &MapOptions,
7373
on_chunk: OnChunk,
74-
on_source: OnSource,
74+
on_source: OnSource<'_, 'a>,
7575
on_name: OnName,
7676
) -> GeneratedInfo;
7777
}
@@ -80,7 +80,7 @@ pub trait StreamChunks {
8080
pub type OnChunk<'a> = &'a mut dyn FnMut(Option<&str>, Mapping);
8181

8282
/// [OnSource] abstraction, see [webpack-sources onSource](https://github.com/webpack/webpack-sources/blob/9f98066311d53a153fdc7c633422a1d086528027/lib/helpers/streamChunks.js#L13).
83-
pub type OnSource<'a> = &'a mut dyn FnMut(u32, &str, Option<&str>);
83+
pub type OnSource<'a, 'b> = &'a mut dyn FnMut(u32, &str, Option<&'b str>);
8484

8585
/// [OnName] abstraction, see [webpack-sources onName](https://github.com/webpack/webpack-sources/blob/9f98066311d53a153fdc7c633422a1d086528027/lib/helpers/streamChunks.js#L13).
8686
pub type OnName<'a> = &'a mut dyn FnMut(u32, &str);
@@ -94,11 +94,14 @@ pub fn stream_chunks_default<S: Source>(
9494
on_name: OnName,
9595
) -> GeneratedInfo {
9696
if let Some(map) = source.map(options) {
97+
let s = source.source();
9798
stream_chunks_of_source_map(
98-
&source.source(),
99+
&s,
99100
&map,
100101
on_chunk,
101-
on_source,
102+
&mut |a, b, c| {
103+
104+
},
102105
on_name,
103106
options,
104107
)
@@ -556,11 +559,11 @@ pub fn stream_chunks_of_raw_source(
556559
}
557560
}
558561

559-
pub fn stream_chunks_of_source_map(
560-
source: &str,
561-
source_map: &SourceMap,
562+
pub fn stream_chunks_of_source_map<'a>(
563+
source: &'a str,
564+
source_map: &'a SourceMap,
562565
on_chunk: OnChunk,
563-
on_source: OnSource,
566+
on_source: OnSource<'_, 'a>,
564567
on_name: OnName,
565568
options: &MapOptions,
566569
) -> GeneratedInfo {
@@ -602,11 +605,11 @@ fn get_source(source_map: &SourceMap, source: &str) -> String {
602605
}
603606
}
604607

605-
fn stream_chunks_of_source_map_final(
606-
source: &str,
607-
source_map: &SourceMap,
608+
fn stream_chunks_of_source_map_final<'a>(
609+
source: &'a str,
610+
source_map: &'a SourceMap,
608611
on_chunk: OnChunk,
609-
on_source: OnSource,
612+
on_source: OnSource<'_, 'a>,
610613
on_name: OnName,
611614
) -> GeneratedInfo {
612615
let result = get_generated_source_info(source);
@@ -658,11 +661,11 @@ fn stream_chunks_of_source_map_final(
658661
result
659662
}
660663

661-
fn stream_chunks_of_source_map_full(
662-
source: &str,
663-
source_map: &SourceMap,
664+
fn stream_chunks_of_source_map_full<'a>(
665+
source: &'a str,
666+
source_map: &'a SourceMap,
664667
on_chunk: OnChunk,
665-
on_source: OnSource,
668+
on_source: OnSource<'_, 'a>,
666669
on_name: OnName,
667670
) -> GeneratedInfo {
668671
for (i, source) in source_map.sources().iter().enumerate() {
@@ -759,11 +762,11 @@ fn stream_chunks_of_source_map_full(
759762
}
760763
}
761764

762-
fn stream_chunks_of_source_map_lines_final(
763-
source: &str,
764-
source_map: &SourceMap,
765+
fn stream_chunks_of_source_map_lines_final<'a>(
766+
source: &'a str,
767+
source_map: &'a SourceMap,
765768
on_chunk: OnChunk,
766-
on_source: OnSource,
769+
on_source: OnSource<'_, 'a>,
767770
_on_name: OnName,
768771
) -> GeneratedInfo {
769772
let result = get_generated_source_info(source);
@@ -814,11 +817,11 @@ fn stream_chunks_of_source_map_lines_final(
814817
result
815818
}
816819

817-
fn stream_chunks_of_source_map_lines_full(
818-
source: &str,
819-
source_map: &SourceMap,
820+
fn stream_chunks_of_source_map_lines_full<'a>(
821+
source: &'a str,
822+
source_map: &'a SourceMap,
820823
on_chunk: OnChunk,
821-
on_source: OnSource,
824+
on_source: OnSource<'_, 'a>,
822825
_on_name: OnName,
823826
) -> GeneratedInfo {
824827
let lines: Vec<&str> = split_into_lines(source).collect();
@@ -933,21 +936,21 @@ impl SourceMapLineChunk {
933936
}
934937

935938
#[allow(clippy::too_many_arguments)]
936-
pub fn stream_chunks_of_combined_source_map(
937-
source: &str,
938-
source_map: &SourceMap,
939-
inner_source_name: &str,
940-
inner_source: Option<&str>,
941-
inner_source_map: &SourceMap,
939+
pub fn stream_chunks_of_combined_source_map<'a>(
940+
source: &'a str,
941+
source_map: &'a SourceMap,
942+
inner_source_name: &'a str,
943+
inner_source: Option<&'a str>,
944+
inner_source_map: &'a SourceMap,
942945
remove_inner_source: bool,
943946
on_chunk: OnChunk,
944-
on_source: OnSource,
947+
on_source: OnSource<'_, 'a>,
945948
on_name: OnName,
946949
options: &MapOptions,
947950
) -> GeneratedInfo {
948951
let on_source = RefCell::new(on_source);
949-
let inner_source: RefCell<Option<ArcStr>> =
950-
RefCell::new(inner_source.map(Into::into));
952+
let inner_source: RefCell<Option<&'a str>> =
953+
RefCell::new(inner_source);
951954
let source_mapping: RefCell<HashMap<ArcStr, u32>> =
952955
RefCell::new(HashMap::default());
953956
let mut name_mapping: HashMap<ArcStr, u32> = HashMap::default();
@@ -961,7 +964,7 @@ pub fn stream_chunks_of_combined_source_map(
961964
let inner_source_index_mapping: RefCell<HashMap<i64, i64>> =
962965
RefCell::new(HashMap::default());
963966
let inner_source_index_value_mapping: RefCell<
964-
HashMap<i64, (ArcStr, Option<ArcStr>)>,
967+
HashMap<i64, (ArcStr, Option<&'a str>)>,
965968
> = RefCell::new(HashMap::default());
966969
let inner_source_contents: RefCell<HashMap<i64, Option<ArcStr>>> =
967970
RefCell::new(HashMap::default());
@@ -1098,7 +1101,7 @@ pub fn stream_chunks_of_combined_source_map(
10981101
if global_index.is_none() {
10991102
let len = source_mapping.len() as u32;
11001103
source_mapping.insert(source.clone(), len);
1101-
on_source.borrow_mut()(len, &source, source_content.as_deref());
1104+
on_source.borrow_mut()(len, &source, source_content);
11021105
global_index = Some(len);
11031106
}
11041107
source_index = global_index.unwrap() as i64;
@@ -1240,7 +1243,7 @@ pub fn stream_chunks_of_combined_source_map(
12401243
on_source.borrow_mut()(
12411244
len,
12421245
inner_source_name,
1243-
inner_source.borrow().as_deref(),
1246+
*inner_source.borrow(),
12441247
);
12451248
global_index = Some(len);
12461249
}
@@ -1299,14 +1302,13 @@ pub fn stream_chunks_of_combined_source_map(
12991302
);
13001303
}
13011304
},
1302-
&mut |i, source, source_content| {
1305+
&mut |i, source, mut source_content| {
13031306
let i = i as i64;
1304-
let mut source_content: Option<ArcStr> = source_content.map(Into::into);
13051307
if source == inner_source_name {
13061308
*inner_source_index.borrow_mut() = i;
13071309
let mut inner_source = inner_source.borrow_mut();
13081310
if let Some(inner_source) = inner_source.as_ref() {
1309-
source_content = Some(inner_source.clone());
1311+
source_content = Some(inner_source);
13101312
} else {
13111313
*inner_source = source_content.clone();
13121314
}
@@ -1395,7 +1397,7 @@ pub fn stream_chunks_of_combined_source_map(
13951397
if global_index.is_none() {
13961398
let len = source_mapping.len() as u32;
13971399
source_mapping.insert(source.into(), len);
1398-
on_source.borrow_mut()(len, source, source_content.as_deref());
1400+
on_source.borrow_mut()(len, source, source_content);
13991401
global_index = Some(len);
14001402
}
14011403
source_index_mapping
@@ -1412,11 +1414,11 @@ pub fn stream_chunks_of_combined_source_map(
14121414
)
14131415
}
14141416

1415-
pub fn stream_and_get_source_and_map<S: StreamChunks>(
1416-
input_source: &S,
1417-
options: &MapOptions,
1417+
pub fn stream_and_get_source_and_map<'a, S: StreamChunks<'a>>(
1418+
input_source: &'a S,
1419+
options: &'a MapOptions,
14181420
on_chunk: OnChunk,
1419-
on_source: OnSource,
1421+
on_source: OnSource<'_, 'a>,
14201422
on_name: OnName,
14211423
) -> (GeneratedInfo, Option<SourceMap>) {
14221424
let mut mappings = Vec::new();

src/original_source.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ impl std::fmt::Debug for OriginalSource {
9898
}
9999
}
100100

101-
impl StreamChunks for OriginalSource {
101+
impl<'a> StreamChunks<'a> for OriginalSource {
102102
fn stream_chunks(
103-
&self,
103+
&'a self,
104104
options: &MapOptions,
105105
on_chunk: OnChunk,
106-
on_source: OnSource,
106+
on_source: OnSource<'_, 'a>,
107107
_on_name: OnName,
108108
) -> crate::helpers::GeneratedInfo {
109109
on_source(0, &self.name, Some(&self.value));

src/raw_source.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl std::fmt::Debug for RawSource {
135135
}
136136
}
137137

138-
impl StreamChunks for RawSource {
138+
impl StreamChunks<'_> for RawSource {
139139
fn stream_chunks(
140140
&self,
141141
options: &MapOptions,

src/replace_source.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,12 @@ impl<T: std::fmt::Debug> std::fmt::Debug for ReplaceSource<T> {
250250
}
251251
}
252252

253-
impl<T: Source> StreamChunks for ReplaceSource<T> {
253+
impl<'a, T: Source> StreamChunks<'a> for ReplaceSource<T> {
254254
fn stream_chunks(
255-
&self,
255+
&'a self,
256256
options: &crate::MapOptions,
257257
on_chunk: crate::helpers::OnChunk,
258-
on_source: crate::helpers::OnSource,
258+
on_source: crate::helpers::OnSource<'_, 'a>,
259259
on_name: crate::helpers::OnName,
260260
) -> crate::helpers::GeneratedInfo {
261261
self.sort_replacement();
@@ -268,7 +268,7 @@ impl<T: Source> StreamChunks for ReplaceSource<T> {
268268
let mut generated_line_offset: i64 = 0;
269269
let mut generated_column_offset: i64 = 0;
270270
let mut generated_column_offset_line = 0;
271-
let source_content_lines: RefCell<Vec<Option<Vec<String>>>> =
271+
let source_content_lines: RefCell<Vec<Option<Vec<&str>>>> =
272272
RefCell::new(Vec::new());
273273
let name_mapping: RefCell<HashMap<String, u32>> =
274274
RefCell::new(HashMap::default());
@@ -593,7 +593,7 @@ impl<T: Source> StreamChunks for ReplaceSource<T> {
593593
source_content_lines[source_index as usize] =
594594
source_content.map(|source_content| {
595595
split_into_lines(source_content)
596-
.map(|line| line.to_string())
596+
.map(|line| line)
597597
.collect()
598598
});
599599
on_source(source_index, source, source_content);

0 commit comments

Comments
 (0)