Skip to content

Commit 523b701

Browse files
committed
perf: try on_source
1 parent d08e8c4 commit 523b701

File tree

9 files changed

+97
-89
lines changed

9 files changed

+97
-89
lines changed

src/cached_source.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> StreamChunks<'_>
122122
fn stream_chunks<'a>(
123123
&'a self,
124124
options: &MapOptions,
125-
on_chunk: crate::helpers::OnChunk,
125+
on_chunk: crate::helpers::OnChunk<'_, 'a>,
126126
on_source: crate::helpers::OnSource<'_, 'a>,
127127
on_name: crate::helpers::OnName<'_, 'a>,
128128
) -> crate::helpers::GeneratedInfo {

src/concat_source.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl<'a> StreamChunks<'a> for ConcatSource {
149149
fn stream_chunks(
150150
&'a self,
151151
options: &MapOptions,
152-
on_chunk: OnChunk,
152+
on_chunk: OnChunk<'_, 'a>,
153153
on_source: OnSource<'_, 'a>,
154154
on_name: OnName<'_, 'a>,
155155
) -> crate::helpers::GeneratedInfo {

src/helpers.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ pub trait StreamChunks<'a> {
7070
fn stream_chunks(
7171
&'a self,
7272
options: &MapOptions,
73-
on_chunk: OnChunk,
73+
on_chunk: OnChunk<'_, 'a>,
7474
on_source: OnSource<'_, 'a>,
7575
on_name: OnName<'_, 'a>,
7676
) -> GeneratedInfo;
7777
}
7878

7979
/// [OnChunk] abstraction, see [webpack-sources onChunk](https://github.com/webpack/webpack-sources/blob/9f98066311d53a153fdc7c633422a1d086528027/lib/helpers/streamChunks.js#L13).
80-
pub type OnChunk<'a> = &'a mut dyn FnMut(Option<&str>, Mapping);
80+
pub type OnChunk<'a, 'b> = &'a mut dyn FnMut(Option<&'b str>, Mapping);
8181

8282
/// [OnSource] abstraction, see [webpack-sources onSource](https://github.com/webpack/webpack-sources/blob/9f98066311d53a153fdc7c633422a1d086528027/lib/helpers/streamChunks.js#L13).
8383
pub type OnSource<'a, 'b> = &'a mut dyn FnMut(u32, &str, Option<&'b str>);
@@ -90,7 +90,7 @@ pub fn stream_chunks_default<'a>(
9090
source: &'a str,
9191
source_map: Option<&'a SourceMap>,
9292
options: &MapOptions,
93-
on_chunk: OnChunk,
93+
on_chunk: OnChunk<'_, 'a>,
9494
on_source: OnSource<'_, 'a>,
9595
on_name: OnName<'_, 'a>,
9696
) -> GeneratedInfo {
@@ -506,12 +506,12 @@ pub fn get_generated_source_info(source: &str) -> GeneratedInfo {
506506
}
507507
}
508508

509-
pub fn stream_chunks_of_raw_source(
510-
source: &str,
509+
pub fn stream_chunks_of_raw_source<'a>(
510+
source: &'a str,
511511
options: &MapOptions,
512-
on_chunk: OnChunk,
513-
_on_source: OnSource,
514-
_on_name: OnName,
512+
on_chunk: OnChunk<'_, 'a>,
513+
_on_source: OnSource<'_, 'a>,
514+
_on_name: OnName<'_, 'a>,
515515
) -> GeneratedInfo {
516516
if options.final_source {
517517
return get_generated_source_info(source);
@@ -549,7 +549,7 @@ pub fn stream_chunks_of_raw_source(
549549
pub fn stream_chunks_of_source_map<'a>(
550550
source: &'a str,
551551
source_map: &'a SourceMap,
552-
on_chunk: OnChunk,
552+
on_chunk: OnChunk<'_, 'a>,
553553
on_source: OnSource<'_, 'a>,
554554
on_name: OnName<'_, 'a>,
555555
options: &MapOptions,
@@ -651,7 +651,7 @@ fn stream_chunks_of_source_map_final<'a>(
651651
fn stream_chunks_of_source_map_full<'a>(
652652
source: &'a str,
653653
source_map: &'a SourceMap,
654-
on_chunk: OnChunk,
654+
on_chunk: OnChunk<'_, 'a>,
655655
on_source: OnSource<'_, 'a>,
656656
on_name: OnName<'_, 'a>,
657657
) -> GeneratedInfo {
@@ -807,7 +807,7 @@ fn stream_chunks_of_source_map_lines_final<'a>(
807807
fn stream_chunks_of_source_map_lines_full<'a>(
808808
source: &'a str,
809809
source_map: &'a SourceMap,
810-
on_chunk: OnChunk,
810+
on_chunk: OnChunk<'_, 'a>,
811811
on_source: OnSource<'_, 'a>,
812812
_on_name: OnName,
813813
) -> GeneratedInfo {
@@ -895,19 +895,19 @@ fn stream_chunks_of_source_map_lines_full<'a>(
895895
}
896896

897897
#[derive(Debug)]
898-
struct SourceMapLineData {
898+
struct SourceMapLineData<'a> {
899899
pub mappings_data: Vec<i64>,
900-
pub chunks: Vec<SourceMapLineChunk>,
900+
pub chunks: Vec<SourceMapLineChunk<'a>>,
901901
}
902902

903903
#[derive(Debug)]
904-
struct SourceMapLineChunk {
905-
content: ArcStr,
906-
cached: OnceCell<WithIndices<ArcStr>>,
904+
struct SourceMapLineChunk<'a> {
905+
content: &'a str,
906+
cached: OnceCell<WithIndices<&'a str>>,
907907
}
908908

909-
impl SourceMapLineChunk {
910-
pub fn new(content: ArcStr) -> Self {
909+
impl<'a> SourceMapLineChunk<'a> {
910+
pub fn new(content: &'a str) -> Self {
911911
Self {
912912
content,
913913
cached: OnceCell::new(),
@@ -917,7 +917,7 @@ impl SourceMapLineChunk {
917917
pub fn substring(&self, start_index: usize, end_index: usize) -> &str {
918918
let cached = self
919919
.cached
920-
.get_or_init(|| WithIndices::new(self.content.clone()));
920+
.get_or_init(|| WithIndices::new(self.content));
921921
cached.substring(start_index, end_index)
922922
}
923923
}
@@ -930,14 +930,14 @@ pub fn stream_chunks_of_combined_source_map<'a>(
930930
inner_source: Option<&'a str>,
931931
inner_source_map: &'a SourceMap,
932932
remove_inner_source: bool,
933-
on_chunk: OnChunk,
933+
on_chunk: OnChunk<'_, 'a>,
934934
on_source: OnSource<'_, 'a>,
935935
on_name: OnName<'_, 'a>,
936936
options: &MapOptions,
937937
) -> GeneratedInfo {
938938
let on_source = RefCell::new(on_source);
939-
let inner_source: RefCell<Option<&'a str>> = RefCell::new(inner_source);
940-
let source_mapping: RefCell<HashMap<ArcStr, u32>> =
939+
let inner_source: RefCell<Option<&str>> = RefCell::new(inner_source);
940+
let source_mapping: RefCell<HashMap<String, u32>> =
941941
RefCell::new(HashMap::default());
942942
let mut name_mapping: HashMap<&str, u32> = HashMap::default();
943943
let source_index_mapping: RefCell<HashMap<i64, i64>> =
@@ -950,9 +950,9 @@ pub fn stream_chunks_of_combined_source_map<'a>(
950950
let inner_source_index_mapping: RefCell<HashMap<i64, i64>> =
951951
RefCell::new(HashMap::default());
952952
let inner_source_index_value_mapping: RefCell<
953-
HashMap<i64, (ArcStr, Option<&'a str>)>,
953+
HashMap<i64, (String, Option<&str>)>,
954954
> = RefCell::new(HashMap::default());
955-
let inner_source_contents: RefCell<HashMap<i64, Option<ArcStr>>> =
955+
let inner_source_contents: RefCell<HashMap<i64, Option<&str>>> =
956956
RefCell::new(HashMap::default());
957957
let inner_source_content_lines: InnerSourceContentLine =
958958
RefCell::new(HashMap::default());
@@ -1083,10 +1083,10 @@ pub fn stream_chunks_of_combined_source_map<'a>(
10831083
.cloned()
10841084
.unwrap_or(("".into(), None));
10851085
let mut source_mapping = source_mapping.borrow_mut();
1086-
let mut global_index = source_mapping.get(&source).copied();
1086+
let mut global_index = source_mapping.get(&source.to_string()).copied();
10871087
if global_index.is_none() {
10881088
let len = source_mapping.len() as u32;
1089-
source_mapping.insert(source.clone(), len);
1089+
source_mapping.insert(source.to_string(), len);
10901090
on_source.borrow_mut()(len, &source, source_content);
10911091
global_index = Some(len);
10921092
}
@@ -1363,7 +1363,7 @@ pub fn stream_chunks_of_combined_source_map<'a>(
13631363
inner_source_index_mapping.borrow_mut().insert(i, -2);
13641364
inner_source_index_value_mapping
13651365
.borrow_mut()
1366-
.insert(i, (source.into(), source_content.map(Into::into)));
1366+
.insert(i, (source.to_string(), source_content));
13671367
},
13681368
&mut |i, name| {
13691369
let i = i as i64;
@@ -1380,7 +1380,7 @@ pub fn stream_chunks_of_combined_source_map<'a>(
13801380
let mut global_index = source_mapping.get(source).copied();
13811381
if global_index.is_none() {
13821382
let len = source_mapping.len() as u32;
1383-
source_mapping.insert(source.into(), len);
1383+
source_mapping.insert(source.to_string(), len);
13841384
on_source.borrow_mut()(len, source, source_content);
13851385
global_index = Some(len);
13861386
}
@@ -1401,7 +1401,7 @@ pub fn stream_chunks_of_combined_source_map<'a>(
14011401
pub fn stream_and_get_source_and_map<'a, S: StreamChunks<'a>>(
14021402
input_source: &'a S,
14031403
options: &MapOptions,
1404-
on_chunk: OnChunk,
1404+
on_chunk: OnChunk<'_, 'a>,
14051405
on_source: OnSource<'_, 'a>,
14061406
on_name: OnName<'_, 'a>,
14071407
) -> (GeneratedInfo, Option<SourceMap>) {

src/original_source.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl<'a> StreamChunks<'a> for OriginalSource {
102102
fn stream_chunks(
103103
&'a self,
104104
options: &MapOptions,
105-
on_chunk: OnChunk,
105+
on_chunk: OnChunk<'_, 'a>,
106106
on_source: OnSource<'_, 'a>,
107107
_on_name: OnName,
108108
) -> crate::helpers::GeneratedInfo {

src/raw_source.rs

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{
22
borrow::Cow,
3-
hash::{Hash, Hasher},
3+
hash::{Hash, Hasher}, sync::OnceLock,
44
};
55

66
use crate::{
@@ -27,15 +27,15 @@ use crate::{
2727
#[derive(Clone, Eq)]
2828
pub enum RawSource {
2929
/// Represent buffer.
30-
Buffer(Vec<u8>),
30+
Buffer(Vec<u8>, OnceLock<String>),
3131
/// Represent string.
3232
Source(String),
3333
}
3434

3535
impl RawSource {
3636
/// Whether the [RawSource] represent a buffer.
3737
pub fn is_buffer(&self) -> bool {
38-
matches!(self, Self::Buffer(_))
38+
matches!(self, Self::Buffer(_, _))
3939
}
4040
}
4141

@@ -47,7 +47,7 @@ impl From<String> for RawSource {
4747

4848
impl From<Vec<u8>> for RawSource {
4949
fn from(s: Vec<u8>) -> Self {
50-
Self::Buffer(s)
50+
Self::Buffer(s, OnceLock::new())
5151
}
5252
}
5353

@@ -59,28 +59,28 @@ impl From<&str> for RawSource {
5959

6060
impl From<&[u8]> for RawSource {
6161
fn from(s: &[u8]) -> Self {
62-
Self::Buffer(s.to_owned())
62+
Self::Buffer(s.to_owned(), OnceLock::new())
6363
}
6464
}
6565

6666
impl Source for RawSource {
6767
fn source(&self) -> Cow<str> {
6868
match self {
69-
RawSource::Buffer(i) => String::from_utf8_lossy(i),
69+
RawSource::Buffer(i, _) => String::from_utf8_lossy(i),
7070
RawSource::Source(i) => Cow::Borrowed(i),
7171
}
7272
}
7373

7474
fn buffer(&self) -> Cow<[u8]> {
7575
match self {
76-
RawSource::Buffer(i) => Cow::Borrowed(i),
76+
RawSource::Buffer(i, _) => Cow::Borrowed(i),
7777
RawSource::Source(i) => Cow::Borrowed(i.as_bytes()),
7878
}
7979
}
8080

8181
fn size(&self) -> usize {
8282
match self {
83-
RawSource::Buffer(i) => i.len(),
83+
RawSource::Buffer(i, _) => i.len(),
8484
RawSource::Source(i) => i.len(),
8585
}
8686
}
@@ -91,7 +91,7 @@ impl Source for RawSource {
9191

9292
fn to_writer(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()> {
9393
writer.write_all(match self {
94-
RawSource::Buffer(i) => i,
94+
RawSource::Buffer(i, _) => i,
9595
RawSource::Source(i) => i.as_bytes(),
9696
})
9797
}
@@ -107,7 +107,7 @@ impl Hash for RawSource {
107107
impl PartialEq for RawSource {
108108
fn eq(&self, other: &Self) -> bool {
109109
match (self, other) {
110-
(Self::Buffer(l0), Self::Buffer(r0)) => l0 == r0,
110+
(Self::Buffer(l0, _), Self::Buffer(r0, _)) => l0 == r0,
111111
(Self::Source(l0), Self::Source(r0)) => l0 == r0,
112112
_ => false,
113113
}
@@ -121,7 +121,7 @@ impl std::fmt::Debug for RawSource {
121121
) -> Result<(), std::fmt::Error> {
122122
let mut d = f.debug_struct("RawSource");
123123
match self {
124-
Self::Buffer(buffer) => {
124+
Self::Buffer(buffer, _) => {
125125
d.field(
126126
"buffer",
127127
&buffer.iter().take(50).copied().collect::<Vec<u8>>(),
@@ -135,24 +135,32 @@ impl std::fmt::Debug for RawSource {
135135
}
136136
}
137137

138-
impl StreamChunks<'_> for RawSource {
138+
impl<'a> StreamChunks<'a> for RawSource {
139139
fn stream_chunks(
140-
&self,
140+
&'a self,
141141
options: &MapOptions,
142-
on_chunk: OnChunk,
143-
on_source: OnSource,
144-
on_name: OnName,
142+
on_chunk: OnChunk<'_, 'a>,
143+
on_source: OnSource<'_, 'a>,
144+
on_name: OnName<'_, 'a>,
145145
) -> crate::helpers::GeneratedInfo {
146146
if options.final_source {
147147
get_generated_source_info(&self.source())
148148
} else {
149-
stream_chunks_of_raw_source(
150-
&self.source(),
151-
options,
152-
on_chunk,
153-
on_source,
154-
on_name,
155-
)
149+
match &self {
150+
RawSource::Buffer(buffer, value_as_string) => {
151+
let source = value_as_string.get_or_init(|| {
152+
String::from_utf8_lossy(&buffer).to_string()
153+
});
154+
stream_chunks_of_raw_source(
155+
source,
156+
options,
157+
on_chunk,
158+
on_source,
159+
on_name,
160+
)
161+
},
162+
RawSource::Source(_) => todo!(),
163+
}
156164
}
157165
}
158166
}

0 commit comments

Comments
 (0)