@@ -2,7 +2,8 @@ use arrayvec::ArrayVec;
22use rustc_hash:: FxHashMap as HashMap ;
33use std:: {
44 borrow:: { BorrowMut , Cow } ,
5- cell:: RefCell ,
5+ cell:: { OnceCell , RefCell } ,
6+ rc:: Rc ,
67} ;
78
89use crate :: {
@@ -15,7 +16,7 @@ use crate::{
1516
1617// Adding this type because sourceContentLine not happy
1718type InnerSourceContentLine < ' a > =
18- RefCell < HashMap < i64 , Option < Vec < WithIndices < & ' a str > > > > > ;
19+ RefCell < HashMap < i64 , Option < Rc < Vec < WithIndices < & ' a str > > > > > > ;
1920
2021pub fn get_map < ' a , S : StreamChunks < ' a > > (
2122 stream : & ' a S ,
@@ -749,7 +750,29 @@ fn stream_chunks_of_source_map_lines_full<'a>(
749750#[ derive( Debug ) ]
750751struct SourceMapLineData < ' a > {
751752 pub mappings_data : Vec < i64 > ,
752- pub chunks : Vec < WithIndices < Cow < ' a , str > > > ,
753+ pub chunks : Vec < SourceMapLineChunk < ' a > > ,
754+ }
755+
756+ #[ derive( Debug ) ]
757+ struct SourceMapLineChunk < ' a > {
758+ content : Cow < ' a , str > ,
759+ cached : OnceCell < WithIndices < Cow < ' a , str > > > ,
760+ }
761+
762+ impl < ' a > SourceMapLineChunk < ' a > {
763+ pub fn new ( content : Cow < ' a , str > ) -> Self {
764+ Self {
765+ content,
766+ cached : OnceCell :: new ( ) ,
767+ }
768+ }
769+
770+ pub fn substring ( & self , start_index : usize , end_index : usize ) -> & str {
771+ let cached = self
772+ . cached
773+ . get_or_init ( || WithIndices :: new ( self . content . clone ( ) ) ) ;
774+ cached. substring ( start_index, end_index)
775+ }
753776}
754777
755778type InnerSourceIndexValueMapping < ' a > =
@@ -871,11 +894,11 @@ pub fn stream_chunks_of_combined_source_map<'a>(
871894 original_source_lines = if let Some ( Some ( original_source) ) =
872895 inner_source_contents. get ( & inner_source_index)
873896 {
874- Some (
897+ Some ( Rc :: new (
875898 split_into_lines ( original_source)
876899 . map ( WithIndices :: new)
877900 . collect ( ) ,
878- )
901+ ) )
879902 } else {
880903 None
881904 } ;
@@ -973,10 +996,12 @@ pub fn stream_chunks_of_combined_source_map<'a>(
973996 . and_then ( |original_source| {
974997 original_source. as_ref ( ) . map ( |s| {
975998 let lines = split_into_lines ( s) ;
976- lines
977- . into_iter ( )
978- . map ( WithIndices :: new)
979- . collect :: < Vec < _ > > ( )
999+ Rc :: new (
1000+ lines
1001+ . into_iter ( )
1002+ . map ( WithIndices :: new)
1003+ . collect :: < Vec < _ > > ( ) ,
1004+ )
9801005 } )
9811006 } ) ;
9821007 inner_source_content_lines
@@ -1181,7 +1206,7 @@ pub fn stream_chunks_of_combined_source_map<'a>(
11811206 . unwrap_or ( -1 ) ,
11821207 ) ;
11831208 // SAFETY: final_source is false
1184- let chunk = WithIndices :: new ( chunk. unwrap ( ) ) ;
1209+ let chunk = SourceMapLineChunk :: new ( chunk. unwrap ( ) ) ;
11851210 data. chunks . push ( chunk) ;
11861211 } ,
11871212 & mut |i, source, source_content| {
0 commit comments