@@ -3,6 +3,7 @@ use std::{
33 cell:: { OnceCell , RefCell } ,
44 marker:: PhantomData ,
55 ops:: Range ,
6+ sync:: Arc ,
67} ;
78
89use rustc_hash:: FxHashMap as HashMap ;
@@ -12,21 +13,18 @@ use crate::{
1213 encoder:: create_encoder,
1314 linear_map:: LinearMap ,
1415 source:: { Mapping , OriginalLocation } ,
16+ source_content_lines:: SourceContentLines ,
1517 with_indices:: WithIndices ,
1618 MapOptions , Rope , SourceMap ,
1719} ;
1820
19- // Adding this type because sourceContentLine not happy
20- type InnerSourceContentLine < ' a , ' b > =
21- RefCell < LinearMap < OnceCell < Option < Vec < WithIndices < ' a , Rope < ' b > > > > > > > ;
22-
2321pub fn get_map < ' a , S : StreamChunks > (
2422 stream : & ' a S ,
2523 options : & ' a MapOptions ,
2624) -> Option < SourceMap > {
2725 let mut mappings_encoder = create_encoder ( options. columns ) ;
2826 let mut sources: Vec < String > = Vec :: new ( ) ;
29- let mut sources_content: Vec < String > = Vec :: new ( ) ;
27+ let mut sources_content: Vec < Arc < str > > = Vec :: new ( ) ;
3028 let mut names: Vec < String > = Vec :: new ( ) ;
3129
3230 stream. stream_chunks (
@@ -47,9 +45,9 @@ pub fn get_map<'a, S: StreamChunks>(
4745 sources[ source_index] = source. to_string ( ) ;
4846 if let Some ( source_content) = source_content {
4947 if sources_content. len ( ) <= source_index {
50- sources_content. resize ( source_index + 1 , "" . to_string ( ) ) ;
48+ sources_content. resize ( source_index + 1 , "" . into ( ) ) ;
5149 }
52- sources_content[ source_index] = source_content. to_string ( ) ;
50+ sources_content[ source_index] = source_content. clone ( ) ;
5351 }
5452 } ,
5553 // on_name
@@ -82,8 +80,9 @@ pub trait StreamChunks {
8280pub type OnChunk < ' a , ' b > = & ' a mut dyn FnMut ( Option < Rope < ' b > > , Mapping ) ;
8381
8482/// [OnSource] abstraction, see [webpack-sources onSource](https://github.com/webpack/webpack-sources/blob/9f98066311d53a153fdc7c633422a1d086528027/lib/helpers/streamChunks.js#L13).
83+ ///
8584pub type OnSource < ' a , ' b > =
86- & ' a mut dyn FnMut ( u32 , Cow < ' b , str > , Option < Rope < ' b > > ) ;
85+ & ' a mut dyn FnMut ( u32 , Cow < ' b , str > , Option < & ' b Arc < str > > ) ;
8786
8887/// [OnName] abstraction, see [webpack-sources onName](https://github.com/webpack/webpack-sources/blob/9f98066311d53a153fdc7c633422a1d086528027/lib/helpers/streamChunks.js#L13).
8988pub type OnName < ' a , ' b > = & ' a mut dyn FnMut ( u32 , Cow < ' b , str > ) ;
@@ -195,8 +194,6 @@ where
195194 }
196195}
197196
198- const EMPTY_ROPE : Rope = Rope :: new ( ) ;
199-
200197/// Split the string with a needle, each string will contain the needle.
201198///
202199/// Copied and modified from https://github.com/rust-lang/cargo/blob/30efe860c0e4adc1a6d7057ad223dc6e47d34edf/src/cargo/sources/registry/index.rs#L1048-L1072
@@ -371,7 +368,7 @@ where
371368 on_source (
372369 i as u32 ,
373370 get_source ( source_map, source) ,
374- source_map. get_source_content ( i) . map ( Rope :: from ) ,
371+ source_map. get_source_content ( i) ,
375372 )
376373 }
377374 for ( i, name) in source_map. names ( ) . iter ( ) . enumerate ( ) {
@@ -435,7 +432,7 @@ where
435432 on_source (
436433 i as u32 ,
437434 get_source ( source_map, source) ,
438- source_map. get_source_content ( i) . map ( Rope :: from ) ,
435+ source_map. get_source_content ( i) ,
439436 )
440437 }
441438 for ( i, name) in source_map. names ( ) . iter ( ) . enumerate ( ) {
@@ -585,7 +582,7 @@ where
585582 on_source (
586583 i as u32 ,
587584 get_source ( source_map, source) ,
588- source_map. get_source_content ( i) . map ( Rope :: from ) ,
585+ source_map. get_source_content ( i) ,
589586 )
590587 }
591588 let final_line = if result. generated_column == 0 {
@@ -633,7 +630,7 @@ where
633630 on_source (
634631 i as u32 ,
635632 get_source ( source_map, source) ,
636- source_map. get_source_content ( i) . map ( Rope :: from ) ,
633+ source_map. get_source_content ( i) ,
637634 )
638635 }
639636 let mut current_generated_line = 1 ;
@@ -706,14 +703,14 @@ struct SourceMapLineData<'a> {
706703}
707704
708705type InnerSourceIndexValueMapping < ' a > =
709- LinearMap < ( Cow < ' a , str > , Option < Rope < ' a > > ) > ;
706+ LinearMap < ( Cow < ' a , str > , Option < & ' a Arc < str > > ) > ;
710707
711708#[ allow( clippy:: too_many_arguments) ]
712709pub fn stream_chunks_of_combined_source_map < ' a , S > (
713710 source : S ,
714711 source_map : & ' a SourceMap ,
715712 inner_source_name : & ' a str ,
716- inner_source : Option < Rope < ' a > > ,
713+ inner_source : Option < & ' a Arc < str > > ,
717714 inner_source_map : & ' a SourceMap ,
718715 remove_inner_source : bool ,
719716 on_chunk : OnChunk < ' _ , ' a > ,
@@ -725,7 +722,7 @@ where
725722 S : SourceText < ' a > + ' a ,
726723{
727724 let on_source = RefCell :: new ( on_source) ;
728- let inner_source: RefCell < Option < Rope < ' a > > > = RefCell :: new ( inner_source) ;
725+ let inner_source: RefCell < Option < & Arc < str > > > = RefCell :: new ( inner_source) ;
729726 let source_mapping: RefCell < HashMap < Cow < str > , u32 > > =
730727 RefCell :: new ( HashMap :: default ( ) ) ;
731728 let mut name_mapping: HashMap < Cow < str > , u32 > = HashMap :: default ( ) ;
@@ -740,10 +737,11 @@ where
740737 RefCell :: new ( LinearMap :: default ( ) ) ;
741738 let inner_source_index_value_mapping: RefCell < InnerSourceIndexValueMapping > =
742739 RefCell :: new ( LinearMap :: default ( ) ) ;
743- let inner_source_contents: RefCell < LinearMap < Option < Rope < ' a > > > > =
744- RefCell :: new ( LinearMap :: default ( ) ) ;
745- let inner_source_content_lines: InnerSourceContentLine =
740+ let inner_source_contents: RefCell < LinearMap < Option < Arc < str > > > > =
746741 RefCell :: new ( LinearMap :: default ( ) ) ;
742+ let inner_source_content_lines: RefCell <
743+ LinearMap < OnceCell < Option < SourceContentLines > > > ,
744+ > = RefCell :: new ( LinearMap :: default ( ) ) ;
747745 let inner_name_index_mapping: RefCell < LinearMap < i64 > > =
748746 RefCell :: new ( LinearMap :: default ( ) ) ;
749747 let inner_name_index_value_mapping: RefCell < LinearMap < Cow < str > > > =
@@ -828,11 +826,9 @@ where
828826 Some ( once_cell) => once_cell. get_or_init ( || {
829827 let inner_source_contents = inner_source_contents. borrow ( ) ;
830828 match inner_source_contents. get ( & inner_source_index) {
831- Some ( Some ( source_content) ) => Some (
832- split_into_lines ( source_content)
833- . map ( WithIndices :: new)
834- . collect ( ) ,
835- ) ,
829+ Some ( Some ( source_content) ) => {
830+ Some ( SourceContentLines :: from ( source_content. clone ( ) ) )
831+ }
836832 _ => None ,
837833 }
838834 } ) ,
@@ -848,8 +844,9 @@ where
848844 } ) ;
849845 if let Some ( original_chunk) = original_chunk {
850846 if original_chunk. len ( ) <= inner_chunk. len ( )
851- && inner_chunk. get_byte_slice ( ..original_chunk. len ( ) )
852- == Some ( original_chunk)
847+ && inner_chunk
848+ . get_byte_slice ( ..original_chunk. len ( ) )
849+ . is_some_and ( |slice| slice == original_chunk)
853850 {
854851 inner_original_column += location_in_chunk;
855852 inner_name_index = -1 ;
@@ -928,11 +925,9 @@ where
928925 Some ( once_cell) => once_cell. get_or_init ( || {
929926 let inner_source_contents = inner_source_contents. borrow ( ) ;
930927 match inner_source_contents. get ( & inner_source_index) {
931- Some ( Some ( source_content) ) => Some (
932- split_into_lines ( source_content)
933- . map ( WithIndices :: new)
934- . collect ( ) ,
935- ) ,
928+ Some ( Some ( source_content) ) => {
929+ Some ( SourceContentLines :: from ( source_content. clone ( ) ) )
930+ }
936931 _ => None ,
937932 }
938933 } ) ,
@@ -945,12 +940,12 @@ where
945940 name_index_value_mapping. get ( & name_index) . cloned ( ) . unwrap ( ) ;
946941 let original_name = original_source_lines
947942 . get ( inner_original_line as usize - 1 )
948- . map_or ( EMPTY_ROPE , |i| {
943+ . map_or ( "" , |i| {
949944 let start = inner_original_column as usize ;
950945 let end = start + name. len ( ) ;
951946 i. substring ( start, end)
952947 } ) ;
953- if Rope :: from ( & name) == original_name {
948+ if name == original_name {
954949 let mut name_index_mapping = name_index_mapping. borrow_mut ( ) ;
955950 final_name_index =
956951 name_index_mapping. get ( & name_index) . copied ( ) . unwrap_or ( -2 ) ;
@@ -1015,7 +1010,7 @@ where
10151010 on_source. borrow_mut ( ) (
10161011 len,
10171012 Cow :: Borrowed ( inner_source_name) ,
1018- inner_source. borrow ( ) . clone ( ) ,
1013+ * inner_source. borrow ( ) ,
10191014 ) ;
10201015 global_index = Some ( len) ;
10211016 }
@@ -1089,13 +1084,13 @@ where
10891084 * inner_source_index. borrow_mut ( ) = i as i64 ;
10901085 let mut inner_source = inner_source. borrow_mut ( ) ;
10911086 if let Some ( inner_source) = inner_source. as_ref ( ) {
1092- source_content = Some ( inner_source. clone ( ) ) ;
1087+ source_content = Some ( inner_source) ;
10931088 } else {
1094- * inner_source = source_content. clone ( ) ;
1089+ * inner_source = source_content;
10951090 }
10961091 source_index_mapping. borrow_mut ( ) . insert ( i, -2 ) ;
10971092 stream_chunks_of_source_map (
1098- source_content. unwrap ( ) ,
1093+ source_content. unwrap ( ) . as_ref ( ) ,
10991094 inner_source_map,
11001095 & mut |chunk, mapping| {
11011096 let mut inner_source_map_line_data =
@@ -1151,7 +1146,7 @@ where
11511146 & mut |i, source, source_content| {
11521147 inner_source_contents
11531148 . borrow_mut ( )
1154- . insert ( i, source_content. clone ( ) ) ;
1149+ . insert ( i, source_content. cloned ( ) ) ;
11551150 inner_source_content_lines
11561151 . borrow_mut ( )
11571152 . insert ( i, Default :: default ( ) ) ;
@@ -1200,7 +1195,7 @@ pub fn stream_and_get_source_and_map<'a, S: StreamChunks>(
12001195) -> ( GeneratedInfo , Option < SourceMap > ) {
12011196 let mut mappings_encoder = create_encoder ( options. columns ) ;
12021197 let mut sources: Vec < String > = Vec :: new ( ) ;
1203- let mut sources_content: Vec < String > = Vec :: new ( ) ;
1198+ let mut sources_content: Vec < Arc < str > > = Vec :: new ( ) ;
12041199 let mut names: Vec < String > = Vec :: new ( ) ;
12051200
12061201 let generated_info = input_source. stream_chunks (
@@ -1215,11 +1210,11 @@ pub fn stream_and_get_source_and_map<'a, S: StreamChunks>(
12151210 sources. push ( "" . into ( ) ) ;
12161211 }
12171212 sources[ source_index2] = source. to_string ( ) ;
1218- if let Some ( ref source_content) = source_content {
1213+ if let Some ( source_content) = source_content {
12191214 while sources_content. len ( ) <= source_index2 {
12201215 sources_content. push ( "" . into ( ) ) ;
12211216 }
1222- sources_content[ source_index2] = source_content. to_string ( ) ;
1217+ sources_content[ source_index2] = source_content. clone ( ) ;
12231218 }
12241219 on_source ( source_index, source, source_content) ;
12251220 } ,
0 commit comments