Skip to content

Commit a65b388

Browse files
authored
perf: cache decode mappings (#97)
* feat: cache decode_mappings * feat: reduce memory call
1 parent 4740971 commit a65b388

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

src/concat_source.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,16 @@ impl StreamChunks for ConcatSource {
161161
let mut current_column_offset = 0;
162162
let mut source_mapping: HashMap<String, u32> = HashMap::default();
163163
let mut name_mapping: HashMap<String, u32> = HashMap::default();
164-
let mut need_to_cloas_mapping = false;
164+
let mut need_to_close_mapping = false;
165+
166+
let source_index_mapping: RefCell<HashMap<u32, u32>> =
167+
RefCell::new(HashMap::default());
168+
let name_index_mapping: RefCell<HashMap<u32, u32>> =
169+
RefCell::new(HashMap::default());
170+
165171
for item in self.children() {
166-
let source_index_mapping: RefCell<HashMap<u32, u32>> =
167-
RefCell::new(HashMap::default());
168-
let name_index_mapping: RefCell<HashMap<u32, u32>> =
169-
RefCell::new(HashMap::default());
172+
source_index_mapping.borrow_mut().clear();
173+
name_index_mapping.borrow_mut().clear();
170174
let mut last_mapping_line = 0;
171175
let GeneratedInfo {
172176
generated_line,
@@ -180,7 +184,7 @@ impl StreamChunks for ConcatSource {
180184
} else {
181185
mapping.generated_column
182186
};
183-
if need_to_cloas_mapping {
187+
if need_to_close_mapping {
184188
if mapping.generated_line != 1 || mapping.generated_column != 0 {
185189
on_chunk(
186190
None,
@@ -191,7 +195,7 @@ impl StreamChunks for ConcatSource {
191195
},
192196
);
193197
}
194-
need_to_cloas_mapping = false;
198+
need_to_close_mapping = false;
195199
}
196200
let result_source_index =
197201
mapping.original.as_ref().and_then(|original| {
@@ -282,7 +286,7 @@ impl StreamChunks for ConcatSource {
282286
.insert(i, global_index.unwrap());
283287
},
284288
);
285-
if need_to_cloas_mapping && (generated_line != 1 || generated_column != 0)
289+
if need_to_close_mapping && (generated_line != 1 || generated_column != 0)
286290
{
287291
on_chunk(
288292
None,
@@ -292,14 +296,14 @@ impl StreamChunks for ConcatSource {
292296
original: None,
293297
},
294298
);
295-
need_to_cloas_mapping = false;
299+
need_to_close_mapping = false;
296300
}
297301
if generated_line > 1 {
298302
current_column_offset = generated_column;
299303
} else {
300304
current_column_offset += generated_column;
301305
}
302-
need_to_cloas_mapping = need_to_cloas_mapping
306+
need_to_close_mapping = need_to_close_mapping
303307
|| (options.final_source && last_mapping_line == generated_line);
304308
current_line_offset += generated_line - 1;
305309
}

src/source.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
convert::{TryFrom, TryInto},
55
fmt,
66
hash::{Hash, Hasher},
7-
sync::Arc,
7+
sync::{Arc, OnceLock},
88
};
99

1010
use dyn_clone::DynClone;
@@ -179,14 +179,26 @@ impl MapOptions {
179179
}
180180

181181
/// The source map created by [Source::map].
182-
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
182+
#[derive(Debug, Clone, Default, PartialEq, Eq)]
183183
pub struct SourceMap {
184184
file: Option<String>,
185185
mappings: String,
186186
sources: Vec<Cow<'static, str>>,
187187
sources_content: Vec<Cow<'static, str>>,
188188
names: Vec<Cow<'static, str>>,
189189
source_root: Option<String>,
190+
decoded_mappings: OnceLock<Vec<Mapping>>,
191+
}
192+
193+
impl Hash for SourceMap {
194+
fn hash<H: Hasher>(&self, state: &mut H) {
195+
self.file.hash(state);
196+
self.mappings.hash(state);
197+
self.sources.hash(state);
198+
self.sources_content.hash(state);
199+
self.names.hash(state);
200+
self.source_root.hash(state);
201+
}
190202
}
191203

192204
impl SourceMap {
@@ -205,6 +217,7 @@ impl SourceMap {
205217
sources_content,
206218
names,
207219
source_root: None,
220+
decoded_mappings: Default::default(),
208221
}
209222
}
210223

@@ -220,7 +233,11 @@ impl SourceMap {
220233

221234
/// Get the decoded mappings in [SourceMap].
222235
pub fn decoded_mappings(&'_ self) -> impl IntoIterator<Item = Mapping> + '_ {
223-
decode_mappings(self)
236+
self
237+
.decoded_mappings
238+
.get_or_init(|| decode_mappings(self).collect::<Vec<_>>())
239+
.clone()
240+
.into_iter()
224241
}
225242

226243
/// Get the mappings string in [SourceMap].
@@ -412,6 +429,7 @@ impl TryFrom<RawSourceMap> for SourceMap {
412429
sources_content,
413430
names,
414431
source_root: raw.source_root,
432+
decoded_mappings: Default::default(),
415433
})
416434
}
417435
}

0 commit comments

Comments
 (0)