Skip to content

Commit 8b4734a

Browse files
committed
refactor: Cow is better
1 parent c999a75 commit 8b4734a

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

src/concat_source.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl<'a> StreamChunks<'a> for ConcatSource {
159159
}
160160
let mut current_line_offset = 0;
161161
let mut current_column_offset = 0;
162-
let mut source_mapping: HashMap<String, u32> = HashMap::default();
162+
let mut source_mapping: HashMap<Cow<str>, u32> = HashMap::default();
163163
let mut name_mapping: HashMap<&str, u32> = HashMap::default();
164164
let mut need_to_close_mapping = false;
165165

@@ -262,10 +262,10 @@ impl<'a> StreamChunks<'a> for ConcatSource {
262262
}
263263
},
264264
&mut |i, source, source_content| {
265-
let mut global_index = source_mapping.get(source).copied();
265+
let mut global_index = source_mapping.get(&source).copied();
266266
if global_index.is_none() {
267267
let len = source_mapping.len() as u32;
268-
source_mapping.insert(source.to_owned(), len);
268+
source_mapping.insert(source.clone(), len);
269269
on_source(len, source, source_content);
270270
global_index = Some(len);
271271
}

src/helpers.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub fn get_map<'a, S: StreamChunks<'a>>(
3535
mappings.push(mapping);
3636
},
3737
// on_source
38-
&mut |source_index, source: &str, source_content: Option<&str>| {
38+
&mut |source_index, source, source_content| {
3939
let source_index = source_index as usize;
4040
if sources.len() <= source_index {
4141
sources.resize(source_index + 1, Cow::Borrowed(""));
@@ -49,7 +49,7 @@ pub fn get_map<'a, S: StreamChunks<'a>>(
4949
}
5050
},
5151
// on_name
52-
&mut |name_index, name: &str| {
52+
&mut |name_index, name| {
5353
let name_index = name_index as usize;
5454
if names.len() <= name_index {
5555
names.resize(name_index + 1, Cow::Borrowed(""));
@@ -78,7 +78,7 @@ pub trait StreamChunks<'a> {
7878
pub type OnChunk<'a, 'b> = &'a mut dyn FnMut(Option<&'b str>, Mapping);
7979

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

8383
/// [OnName] abstraction, see [webpack-sources onName](https://github.com/webpack/webpack-sources/blob/9f98066311d53a153fdc7c633422a1d086528027/lib/helpers/streamChunks.js#L13).
8484
pub type OnName<'a, 'b> = &'a mut dyn FnMut(u32, &'b str);
@@ -580,13 +580,13 @@ pub fn stream_chunks_of_source_map<'a>(
580580
}
581581
}
582582

583-
fn get_source(source_map: &SourceMap, source: &str) -> String {
583+
fn get_source<'a>(source_map: &SourceMap, source: &'a str) -> Cow<'a, str> {
584584
let source_root = source_map.source_root();
585585
match source_root {
586-
Some(root) if root.is_empty() => source.to_string(),
587-
Some(root) if root.ends_with('/') => format!("{}{}", root, source),
588-
Some(root) => format!("{}/{}", root, source),
589-
None => source.to_string(),
586+
Some(root) if root.is_empty() => Cow::Borrowed(source),
587+
Some(root) if root.ends_with('/') => Cow::Owned(format!("{}{}", root, source)),
588+
Some(root) => Cow::Owned(format!("{}/{}", root, source)),
589+
None => Cow::Borrowed(source),
590590
}
591591
}
592592

@@ -604,7 +604,7 @@ fn stream_chunks_of_source_map_final<'a>(
604604
for (i, source) in source_map.sources().iter().enumerate() {
605605
on_source(
606606
i as u32,
607-
&get_source(source_map, source),
607+
get_source(source_map, source),
608608
source_map.get_source_content(i),
609609
)
610610
}
@@ -656,7 +656,7 @@ fn stream_chunks_of_source_map_full<'a>(
656656
for (i, source) in source_map.sources().iter().enumerate() {
657657
on_source(
658658
i as u32,
659-
&get_source(source_map, source),
659+
get_source(source_map, source),
660660
source_map.get_source_content(i),
661661
)
662662
}
@@ -764,7 +764,7 @@ fn stream_chunks_of_source_map_lines_final<'a>(
764764
for (i, source) in source_map.sources().iter().enumerate() {
765765
on_source(
766766
i as u32,
767-
&get_source(source_map, source),
767+
get_source(source_map, source),
768768
source_map.get_source_content(i),
769769
)
770770
}
@@ -819,7 +819,7 @@ fn stream_chunks_of_source_map_lines_full<'a>(
819819
for (i, source) in source_map.sources().iter().enumerate() {
820820
on_source(
821821
i as u32,
822-
&get_source(source_map, source),
822+
get_source(source_map, source),
823823
source_map.get_source_content(i),
824824
)
825825
}
@@ -918,6 +918,8 @@ impl<'a> SourceMapLineChunk<'a> {
918918
}
919919
}
920920

921+
type InnerSourceIndexValueMapping<'a> = HashMap<i64, (Cow<'a, str>, Option<&'a str>)>;
922+
921923
#[allow(clippy::too_many_arguments)]
922924
pub fn stream_chunks_of_combined_source_map<'a>(
923925
source: &'a str,
@@ -933,7 +935,7 @@ pub fn stream_chunks_of_combined_source_map<'a>(
933935
) -> GeneratedInfo {
934936
let on_source = RefCell::new(on_source);
935937
let inner_source: RefCell<Option<&str>> = RefCell::new(inner_source);
936-
let source_mapping: RefCell<HashMap<String, u32>> =
938+
let source_mapping: RefCell<HashMap<Cow<str>, u32>> =
937939
RefCell::new(HashMap::default());
938940
let mut name_mapping: HashMap<&str, u32> = HashMap::default();
939941
let source_index_mapping: RefCell<HashMap<i64, i64>> =
@@ -945,9 +947,7 @@ pub fn stream_chunks_of_combined_source_map<'a>(
945947
let inner_source_index: RefCell<i64> = RefCell::new(-2);
946948
let inner_source_index_mapping: RefCell<HashMap<i64, i64>> =
947949
RefCell::new(HashMap::default());
948-
let inner_source_index_value_mapping: RefCell<
949-
HashMap<i64, (String, Option<&str>)>,
950-
> = RefCell::new(HashMap::default());
950+
let inner_source_index_value_mapping: RefCell<InnerSourceIndexValueMapping> = RefCell::new(HashMap::default());
951951
let inner_source_contents: RefCell<HashMap<i64, Option<&str>>> =
952952
RefCell::new(HashMap::default());
953953
let inner_source_content_lines: InnerSourceContentLine =
@@ -1080,11 +1080,11 @@ pub fn stream_chunks_of_combined_source_map<'a>(
10801080
.unwrap_or(("".into(), None));
10811081
let mut source_mapping = source_mapping.borrow_mut();
10821082
let mut global_index =
1083-
source_mapping.get(&source.to_string()).copied();
1083+
source_mapping.get(&source).copied();
10841084
if global_index.is_none() {
10851085
let len = source_mapping.len() as u32;
1086-
source_mapping.insert(source.to_string(), len);
1087-
on_source.borrow_mut()(len, &source, source_content);
1086+
source_mapping.insert(source.clone(), len);
1087+
on_source.borrow_mut()(len, source, source_content);
10881088
global_index = Some(len);
10891089
}
10901090
source_index = global_index.unwrap() as i64;
@@ -1225,7 +1225,7 @@ pub fn stream_chunks_of_combined_source_map<'a>(
12251225
source_mapping.insert(source.into(), len);
12261226
on_source.borrow_mut()(
12271227
len,
1228-
inner_source_name,
1228+
Cow::Borrowed(inner_source_name),
12291229
*inner_source.borrow(),
12301230
);
12311231
global_index = Some(len);
@@ -1360,7 +1360,7 @@ pub fn stream_chunks_of_combined_source_map<'a>(
13601360
inner_source_index_mapping.borrow_mut().insert(i, -2);
13611361
inner_source_index_value_mapping
13621362
.borrow_mut()
1363-
.insert(i, (source.to_string(), source_content));
1363+
.insert(i, (source, source_content));
13641364
},
13651365
&mut |i, name| {
13661366
let i = i as i64;
@@ -1374,10 +1374,10 @@ pub fn stream_chunks_of_combined_source_map<'a>(
13741374
);
13751375
} else {
13761376
let mut source_mapping = source_mapping.borrow_mut();
1377-
let mut global_index = source_mapping.get(source).copied();
1377+
let mut global_index = source_mapping.get(&source).copied();
13781378
if global_index.is_none() {
13791379
let len = source_mapping.len() as u32;
1380-
source_mapping.insert(source.to_string(), len);
1380+
source_mapping.insert(source.clone(), len);
13811381
on_source.borrow_mut()(len, source, source_content);
13821382
global_index = Some(len);
13831383
}

src/original_source.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl<'a> StreamChunks<'a> for OriginalSource {
106106
on_source: OnSource<'_, 'a>,
107107
_on_name: OnName,
108108
) -> crate::helpers::GeneratedInfo {
109-
on_source(0, &self.name, Some(&self.value));
109+
on_source(0, Cow::Borrowed(&self.name), Some(&self.value));
110110
if options.columns {
111111
// With column info we need to read all lines and split them
112112
let mut line = 1;

0 commit comments

Comments
 (0)