Skip to content

Commit

Permalink
perf: stream_chunks_of_combined_source_map (#116)
Browse files Browse the repository at this point in the history
* perf: stream_chunks_of_combined_source_map

* perf: rm SourceMapLineChunk

* perf: chunks can without WithIndices
  • Loading branch information
SyMind authored Aug 28, 2024
1 parent 08fca46 commit 60f4eed
Showing 1 changed file with 47 additions and 58 deletions.
105 changes: 47 additions & 58 deletions src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{
borrow::{BorrowMut, Cow},
cell::RefCell,
rc::Rc,
cell::{OnceCell, RefCell},
};

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

// Adding this type because sourceContentLine not happy
type InnerSourceContentLine<'a> =
RefCell<HashMap<u32, Option<Rc<Vec<WithIndices<&'a str>>>>>>;
RefCell<LinearMap<OnceCell<Option<Vec<WithIndices<&'a str>>>>>>;

pub fn get_map<'a, S: StreamChunks<'a>>(
stream: &'a S,
Expand Down Expand Up @@ -600,7 +599,7 @@ fn stream_chunks_of_source_map_lines_full<'a>(
#[derive(Debug)]
struct SourceMapLineData<'a> {
pub mappings_data: Vec<i64>,
pub chunks: Vec<WithIndices<Cow<'a, str>>>,
pub chunks: Vec<Cow<'a, str>>,
}

type InnerSourceIndexValueMapping<'a> =
Expand Down Expand Up @@ -638,7 +637,7 @@ pub fn stream_chunks_of_combined_source_map<'a>(
let inner_source_contents: RefCell<LinearMap<Option<&str>>> =
RefCell::new(LinearMap::default());
let inner_source_content_lines: InnerSourceContentLine =
RefCell::new(HashMap::default());
RefCell::new(LinearMap::default());
let inner_name_index_mapping: RefCell<LinearMap<i64>> =
RefCell::new(LinearMap::default());
let inner_name_index_value_mapping: RefCell<LinearMap<Cow<str>>> =
Expand Down Expand Up @@ -668,6 +667,7 @@ pub fn stream_chunks_of_combined_source_map<'a>(
}
Some(l as u32 - 1)
};

stream_chunks_of_source_map(
source,
source_map,
Expand Down Expand Up @@ -715,41 +715,38 @@ pub fn stream_chunks_of_combined_source_map<'a>(
let inner_generated_column = mappings_data[mi];
let location_in_chunk = original_column - inner_generated_column;
if location_in_chunk > 0 {
let mut inner_source_content_lines =
let inner_source_content_lines =
inner_source_content_lines.borrow_mut();
let mut original_source_lines = inner_source_content_lines
.get(&inner_source_index)
.cloned()
.and_then(|id| id);
if original_source_lines.is_none() {
let inner_source_contents = inner_source_contents.borrow();
original_source_lines = if let Some(Some(original_source)) =
inner_source_contents.get(&inner_source_index)
{
Some(Rc::new(
split_into_lines(original_source)
.map(WithIndices::new)
.collect(),
))
} else {
None
let original_source_lines =
match inner_source_content_lines.get(&inner_source_index) {
Some(once_cell) => once_cell.get_or_init(|| {
let inner_source_contents = inner_source_contents.borrow();
match inner_source_contents.get(&inner_source_index) {
Some(Some(source_content)) => Some(
split_into_lines(source_content)
.map(WithIndices::new)
.collect(),
),
_ => None,
}
}),
None => &None,
};
inner_source_content_lines
.insert(inner_source_index, original_source_lines.clone());
}
if let Some(original_source_lines) = original_source_lines {
let original_chunk = original_source_lines
.get(inner_original_line as usize - 1)
.map_or("", |lines| {
.map(|lines| {
let start = inner_original_column as usize;
let end = start + location_in_chunk as usize;
lines.substring(start, end)
});
if inner_chunk.substring(0, location_in_chunk as usize)
== original_chunk
{
inner_original_column += location_in_chunk;
inner_name_index = -1;
if let Some(original_chunk) = original_chunk {
if original_chunk.len() <= inner_chunk.len()
&& &inner_chunk[..original_chunk.len()] == original_chunk
{
inner_original_column += location_in_chunk;
inner_name_index = -1;
}
}
}
}
Expand Down Expand Up @@ -817,30 +814,23 @@ pub fn stream_chunks_of_combined_source_map<'a>(
// when we don't have an inner name,
// but we have an outer name
// it can be used when inner original code equals to the name
let mut inner_source_content_lines =
let inner_source_content_lines =
inner_source_content_lines.borrow_mut();
let mut original_source_lines = inner_source_content_lines
.get(&inner_source_index)
.cloned()
.and_then(|id| id);
if original_source_lines.is_none() {
let inner_source_contents = inner_source_contents.borrow_mut();
original_source_lines = inner_source_contents
.get(&inner_source_index)
.and_then(|original_source| {
original_source.as_ref().map(|s| {
let lines = split_into_lines(s);
Rc::new(
lines
.into_iter()
let original_source_lines =
match inner_source_content_lines.get(&inner_source_index) {
Some(once_cell) => once_cell.get_or_init(|| {
let inner_source_contents = inner_source_contents.borrow();
match inner_source_contents.get(&inner_source_index) {
Some(Some(source_content)) => Some(
split_into_lines(source_content)
.map(WithIndices::new)
.collect::<Vec<_>>(),
)
})
});
inner_source_content_lines
.insert(inner_source_index, original_source_lines.clone());
}
.collect(),
),
_ => None,
}
}),
None => &None,
};
if let Some(original_source_lines) = original_source_lines {
let name_index_value_mapping =
name_index_value_mapping.borrow();
Expand Down Expand Up @@ -1049,14 +1039,13 @@ pub fn stream_chunks_of_combined_source_map<'a>(
.unwrap_or(-1),
);
// SAFETY: final_source is false
let chunk = WithIndices::new(chunk.unwrap());
data.chunks.push(chunk);
data.chunks.push(chunk.unwrap());
},
&mut |i, source, source_content| {
inner_source_contents
inner_source_contents.borrow_mut().insert(i, source_content);
inner_source_content_lines
.borrow_mut()
.insert(i, source_content.map(Into::into));
inner_source_content_lines.borrow_mut().insert(i, None);
.insert(i, Default::default());
inner_source_index_mapping.borrow_mut().insert(i, -2);
inner_source_index_value_mapping
.borrow_mut()
Expand Down

0 comments on commit 60f4eed

Please sign in to comment.