Skip to content

Commit 60f4eed

Browse files
authored
perf: stream_chunks_of_combined_source_map (#116)
* perf: stream_chunks_of_combined_source_map * perf: rm SourceMapLineChunk * perf: chunks can without WithIndices
1 parent 08fca46 commit 60f4eed

File tree

1 file changed

+47
-58
lines changed

1 file changed

+47
-58
lines changed

src/helpers.rs

Lines changed: 47 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::{
22
borrow::{BorrowMut, Cow},
3-
cell::RefCell,
4-
rc::Rc,
3+
cell::{OnceCell, RefCell},
54
};
65

76
use rustc_hash::FxHashMap as HashMap;
@@ -17,7 +16,7 @@ use crate::{
1716

1817
// Adding this type because sourceContentLine not happy
1918
type InnerSourceContentLine<'a> =
20-
RefCell<HashMap<u32, Option<Rc<Vec<WithIndices<&'a str>>>>>>;
19+
RefCell<LinearMap<OnceCell<Option<Vec<WithIndices<&'a str>>>>>>;
2120

2221
pub fn get_map<'a, S: StreamChunks<'a>>(
2322
stream: &'a S,
@@ -600,7 +599,7 @@ fn stream_chunks_of_source_map_lines_full<'a>(
600599
#[derive(Debug)]
601600
struct SourceMapLineData<'a> {
602601
pub mappings_data: Vec<i64>,
603-
pub chunks: Vec<WithIndices<Cow<'a, str>>>,
602+
pub chunks: Vec<Cow<'a, str>>,
604603
}
605604

606605
type InnerSourceIndexValueMapping<'a> =
@@ -638,7 +637,7 @@ pub fn stream_chunks_of_combined_source_map<'a>(
638637
let inner_source_contents: RefCell<LinearMap<Option<&str>>> =
639638
RefCell::new(LinearMap::default());
640639
let inner_source_content_lines: InnerSourceContentLine =
641-
RefCell::new(HashMap::default());
640+
RefCell::new(LinearMap::default());
642641
let inner_name_index_mapping: RefCell<LinearMap<i64>> =
643642
RefCell::new(LinearMap::default());
644643
let inner_name_index_value_mapping: RefCell<LinearMap<Cow<str>>> =
@@ -668,6 +667,7 @@ pub fn stream_chunks_of_combined_source_map<'a>(
668667
}
669668
Some(l as u32 - 1)
670669
};
670+
671671
stream_chunks_of_source_map(
672672
source,
673673
source_map,
@@ -715,41 +715,38 @@ pub fn stream_chunks_of_combined_source_map<'a>(
715715
let inner_generated_column = mappings_data[mi];
716716
let location_in_chunk = original_column - inner_generated_column;
717717
if location_in_chunk > 0 {
718-
let mut inner_source_content_lines =
718+
let inner_source_content_lines =
719719
inner_source_content_lines.borrow_mut();
720-
let mut original_source_lines = inner_source_content_lines
721-
.get(&inner_source_index)
722-
.cloned()
723-
.and_then(|id| id);
724-
if original_source_lines.is_none() {
725-
let inner_source_contents = inner_source_contents.borrow();
726-
original_source_lines = if let Some(Some(original_source)) =
727-
inner_source_contents.get(&inner_source_index)
728-
{
729-
Some(Rc::new(
730-
split_into_lines(original_source)
731-
.map(WithIndices::new)
732-
.collect(),
733-
))
734-
} else {
735-
None
720+
let original_source_lines =
721+
match inner_source_content_lines.get(&inner_source_index) {
722+
Some(once_cell) => once_cell.get_or_init(|| {
723+
let inner_source_contents = inner_source_contents.borrow();
724+
match inner_source_contents.get(&inner_source_index) {
725+
Some(Some(source_content)) => Some(
726+
split_into_lines(source_content)
727+
.map(WithIndices::new)
728+
.collect(),
729+
),
730+
_ => None,
731+
}
732+
}),
733+
None => &None,
736734
};
737-
inner_source_content_lines
738-
.insert(inner_source_index, original_source_lines.clone());
739-
}
740735
if let Some(original_source_lines) = original_source_lines {
741736
let original_chunk = original_source_lines
742737
.get(inner_original_line as usize - 1)
743-
.map_or("", |lines| {
738+
.map(|lines| {
744739
let start = inner_original_column as usize;
745740
let end = start + location_in_chunk as usize;
746741
lines.substring(start, end)
747742
});
748-
if inner_chunk.substring(0, location_in_chunk as usize)
749-
== original_chunk
750-
{
751-
inner_original_column += location_in_chunk;
752-
inner_name_index = -1;
743+
if let Some(original_chunk) = original_chunk {
744+
if original_chunk.len() <= inner_chunk.len()
745+
&& &inner_chunk[..original_chunk.len()] == original_chunk
746+
{
747+
inner_original_column += location_in_chunk;
748+
inner_name_index = -1;
749+
}
753750
}
754751
}
755752
}
@@ -817,30 +814,23 @@ pub fn stream_chunks_of_combined_source_map<'a>(
817814
// when we don't have an inner name,
818815
// but we have an outer name
819816
// it can be used when inner original code equals to the name
820-
let mut inner_source_content_lines =
817+
let inner_source_content_lines =
821818
inner_source_content_lines.borrow_mut();
822-
let mut original_source_lines = inner_source_content_lines
823-
.get(&inner_source_index)
824-
.cloned()
825-
.and_then(|id| id);
826-
if original_source_lines.is_none() {
827-
let inner_source_contents = inner_source_contents.borrow_mut();
828-
original_source_lines = inner_source_contents
829-
.get(&inner_source_index)
830-
.and_then(|original_source| {
831-
original_source.as_ref().map(|s| {
832-
let lines = split_into_lines(s);
833-
Rc::new(
834-
lines
835-
.into_iter()
819+
let original_source_lines =
820+
match inner_source_content_lines.get(&inner_source_index) {
821+
Some(once_cell) => once_cell.get_or_init(|| {
822+
let inner_source_contents = inner_source_contents.borrow();
823+
match inner_source_contents.get(&inner_source_index) {
824+
Some(Some(source_content)) => Some(
825+
split_into_lines(source_content)
836826
.map(WithIndices::new)
837-
.collect::<Vec<_>>(),
838-
)
839-
})
840-
});
841-
inner_source_content_lines
842-
.insert(inner_source_index, original_source_lines.clone());
843-
}
827+
.collect(),
828+
),
829+
_ => None,
830+
}
831+
}),
832+
None => &None,
833+
};
844834
if let Some(original_source_lines) = original_source_lines {
845835
let name_index_value_mapping =
846836
name_index_value_mapping.borrow();
@@ -1049,14 +1039,13 @@ pub fn stream_chunks_of_combined_source_map<'a>(
10491039
.unwrap_or(-1),
10501040
);
10511041
// SAFETY: final_source is false
1052-
let chunk = WithIndices::new(chunk.unwrap());
1053-
data.chunks.push(chunk);
1042+
data.chunks.push(chunk.unwrap());
10541043
},
10551044
&mut |i, source, source_content| {
1056-
inner_source_contents
1045+
inner_source_contents.borrow_mut().insert(i, source_content);
1046+
inner_source_content_lines
10571047
.borrow_mut()
1058-
.insert(i, source_content.map(Into::into));
1059-
inner_source_content_lines.borrow_mut().insert(i, None);
1048+
.insert(i, Default::default());
10601049
inner_source_index_mapping.borrow_mut().insert(i, -2);
10611050
inner_source_index_value_mapping
10621051
.borrow_mut()

0 commit comments

Comments
 (0)