@@ -7,7 +7,13 @@ use std::{
77use dashmap:: DashMap ;
88use rustc_hash:: FxHasher ;
99
10- use crate :: { helpers:: StreamChunks , MapOptions , Source , SourceMap } ;
10+ use crate :: {
11+ helpers:: {
12+ stream_and_get_source_and_map, stream_chunks_of_raw_source,
13+ stream_chunks_of_source_map, StreamChunks ,
14+ } ,
15+ MapOptions , Source , SourceMap ,
16+ } ;
1117
1218/// It tries to reused cached results from other methods to avoid calculations,
1319/// usually used after modify is finished.
@@ -44,10 +50,10 @@ use crate::{helpers::StreamChunks, MapOptions, Source, SourceMap};
4450/// ```
4551pub struct CachedSource < T > {
4652 inner : Arc < T > ,
47- cached_buffer : OnceLock < Vec < u8 > > ,
53+ cached_buffer : OnceLock < Arc < Vec < u8 > > > ,
4854 cached_source : OnceLock < Arc < str > > ,
4955 cached_maps :
50- DashMap < MapOptions , Option < SourceMap > , BuildHasherDefault < FxHasher > > ,
56+ Arc < DashMap < MapOptions , Option < SourceMap > , BuildHasherDefault < FxHasher > > > ,
5157}
5258
5359impl < T > CachedSource < T > {
@@ -78,7 +84,7 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> Source for CachedSource<T> {
7884 fn buffer ( & self ) -> Cow < [ u8 ] > {
7985 let cached = self
8086 . cached_buffer
81- . get_or_init ( || self . inner . buffer ( ) . to_vec ( ) ) ;
87+ . get_or_init ( || self . inner . buffer ( ) . to_vec ( ) . into ( ) ) ;
8288 Cow :: Borrowed ( cached)
8389 }
8490
@@ -91,7 +97,7 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> Source for CachedSource<T> {
9197 map. clone ( )
9298 } else {
9399 let map = self . inner . map ( options) ;
94- self . cached_maps . insert ( options. to_owned ( ) , map. clone ( ) ) ;
100+ self . cached_maps . insert ( options. clone ( ) , map. clone ( ) ) ;
95101 map
96102 }
97103 }
@@ -111,24 +117,27 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> StreamChunks
111117 on_source : crate :: helpers:: OnSource ,
112118 on_name : crate :: helpers:: OnName ,
113119 ) -> crate :: helpers:: GeneratedInfo {
114- // if self.cached_maps.contains_key(options)
115- // && (self.cached_buffer.get().is_some()
116- // || self.cached_source.get().is_some())
117- // {
118- // let source = self.source();
119- // if let Some(map) = &self.map(options) {
120- // return stream_chunks_of_source_map(
121- // &source, map, on_chunk, on_source, on_name, options,
122- // );
123- // } else {
124- // return stream_chunks_of_raw_source(
125- // &source, options, on_chunk, on_source, on_name,
126- // );
127- // }
128- // }
129- self
130- . inner
131- . stream_chunks ( options, on_chunk, on_source, on_name)
120+ if self . cached_maps . contains_key ( options) {
121+ let source = self . source ( ) ;
122+ if let Some ( map) = & self . map ( options) {
123+ return stream_chunks_of_source_map (
124+ & source, map, on_chunk, on_source, on_name, options,
125+ ) ;
126+ } else {
127+ return stream_chunks_of_raw_source (
128+ & source, options, on_chunk, on_source, on_name,
129+ ) ;
130+ }
131+ }
132+ let ( generated_info, map) = stream_and_get_source_and_map (
133+ self . original ( ) ,
134+ options,
135+ on_chunk,
136+ on_source,
137+ on_name,
138+ ) ;
139+ self . cached_maps . insert ( options. clone ( ) , map) ;
140+ generated_info
132141 }
133142}
134143
0 commit comments