Skip to content

Commit e99ef2b

Browse files
committed
perf: ReplaceSource
1 parent 42dd94a commit e99ef2b

File tree

4 files changed

+57
-53
lines changed

4 files changed

+57
-53
lines changed

src/helpers.rs

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::{
1313
encoder::create_encoder,
1414
linear_map::LinearMap,
1515
source::{Mapping, OriginalLocation},
16+
source_content_lines::SourceContentLines,
1617
with_indices::WithIndices,
1718
MapOptions, Rope, SourceMap,
1819
};
@@ -704,40 +705,6 @@ struct SourceMapLineData<'a> {
704705
type InnerSourceIndexValueMapping<'a> =
705706
LinearMap<(Cow<'a, str>, Option<&'a Arc<str>>)>;
706707

707-
type Owner = Arc<str>;
708-
709-
type BorrowedValue<'a> = Vec<WithIndices<'a, &'a str>>;
710-
711-
self_cell::self_cell!(
712-
struct SourceContentLines {
713-
owner: Owner,
714-
#[covariant]
715-
dependent: BorrowedValue,
716-
}
717-
);
718-
719-
impl SourceContentLines {
720-
pub fn get(&self, line: usize) -> Option<&WithIndices<'_, &str>> {
721-
self.borrow_dependent().get(line)
722-
}
723-
}
724-
725-
impl From<Arc<str>> for SourceContentLines {
726-
fn from(value: Arc<str>) -> Self {
727-
SourceContentLines::new(value, |owner| {
728-
split_into_lines(&owner.as_ref())
729-
.map(WithIndices::new)
730-
.collect::<Vec<_>>()
731-
})
732-
}
733-
}
734-
735-
impl From<&Arc<str>> for SourceContentLines {
736-
fn from(value: &Arc<str>) -> Self {
737-
Self::from(value.clone())
738-
}
739-
}
740-
741708
#[allow(clippy::too_many_arguments)]
742709
pub fn stream_chunks_of_combined_source_map<'a, S>(
743710
source: S,
@@ -978,7 +945,7 @@ where
978945
let end = start + name.len();
979946
i.substring(start, end)
980947
});
981-
if Rope::from(&name) == original_name {
948+
if name == original_name {
982949
let mut name_index_mapping = name_index_mapping.borrow_mut();
983950
final_name_index =
984951
name_index_mapping.get(&name_index).copied().unwrap_or(-2);

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ mod raw_source;
1212
mod replace_source;
1313
mod rope;
1414
mod source;
15+
mod source_content_lines;
1516
mod source_map_source;
1617
mod with_indices;
1718

src/replace_source.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::{
22
borrow::Cow,
33
cell::RefCell,
44
hash::{Hash, Hasher},
5+
sync::Arc,
56
};
67

78
use rustc_hash::FxHashMap as HashMap;
@@ -12,7 +13,7 @@ use crate::{
1213
},
1314
linear_map::LinearMap,
1415
rope::Rope,
15-
with_indices::WithIndices,
16+
source_content_lines::SourceContentLines,
1617
BoxSource, MapOptions, Mapping, OriginalLocation, Source, SourceExt,
1718
SourceMap, SourceValue,
1819
};
@@ -318,21 +319,22 @@ impl std::fmt::Debug for ReplaceSource {
318319
}
319320
}
320321

321-
enum SourceContent<'a> {
322-
Raw(Rope<'a>),
323-
Lines(Vec<WithIndices<'a, Rope<'a>>>),
322+
enum SourceContent {
323+
Raw(Arc<str>),
324+
Lines(SourceContentLines),
324325
}
325326

326-
fn check_content_at_position<'a>(
327-
lines: &[WithIndices<'a, Rope<'a>>],
327+
fn check_content_at_position(
328+
lines: &SourceContentLines,
328329
line: u32,
329330
column: u32,
330-
expected: Rope, // FIXME: memory
331+
expected: &Rope,
331332
) -> bool {
332333
if let Some(line) = lines.get(line as usize - 1) {
333334
line
334335
.substring(column as usize, usize::MAX)
335-
.starts_with(&expected)
336+
.into_rope()
337+
.starts_with(expected)
336338
} else {
337339
false
338340
}
@@ -389,15 +391,13 @@ impl StreamChunks for ReplaceSource {
389391
// In this case, we can't split this mapping.
390392
// webpack-sources also have this function, refer https://github.com/webpack/webpack-sources/blob/main/lib/ReplaceSource.js#L158
391393
let check_original_content =
392-
|source_index: u32, line: u32, column: u32, expected_chunk: Rope| {
394+
|source_index: u32, line: u32, column: u32, expected_chunk: &Rope| {
393395
if let Some(Some(source_content)) =
394396
source_content_lines.borrow_mut().get_mut(&source_index)
395397
{
396398
match source_content {
397399
SourceContent::Raw(source) => {
398-
let lines = split_into_lines(source)
399-
.map(WithIndices::new)
400-
.collect::<Vec<_>>();
400+
let lines = SourceContentLines::from(&*source);
401401
let matched =
402402
check_content_at_position(&lines, line, column, expected_chunk);
403403
*source_content = SourceContent::Lines(lines);
@@ -451,7 +451,7 @@ impl StreamChunks for ReplaceSource {
451451
original.source_index,
452452
original.original_line,
453453
original.original_column,
454-
chunk.byte_slice(0..chunk_pos as usize),
454+
&chunk.byte_slice(0..chunk_pos as usize),
455455
)
456456
}) {
457457
original.original_column += chunk_pos;
@@ -508,7 +508,7 @@ impl StreamChunks for ReplaceSource {
508508
original.source_index,
509509
original.original_line,
510510
original.original_column,
511-
chunk_slice.clone(),
511+
&chunk_slice,
512512
)
513513
})
514514
{
@@ -628,7 +628,7 @@ impl StreamChunks for ReplaceSource {
628628
original.source_index,
629629
original.original_line,
630630
original.original_column,
631-
chunk.byte_slice(
631+
&chunk.byte_slice(
632632
chunk_pos as usize..(chunk_pos + offset as u32) as usize,
633633
),
634634
)
@@ -683,9 +683,8 @@ impl StreamChunks for ReplaceSource {
683683
},
684684
&mut |source_index, source, source_content| {
685685
let mut source_content_lines = source_content_lines.borrow_mut();
686-
let lines = source_content.map(|source_content| {
687-
SourceContent::Raw(source_content.as_ref().into())
688-
});
686+
let lines = source_content
687+
.map(|source_content| SourceContent::Raw(source_content.clone()));
689688
source_content_lines.insert(source_index, lines);
690689
on_source(source_index, source, source_content);
691690
},

src/source_content_lines.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use std::sync::Arc;
2+
3+
use crate::{helpers::split_into_lines, with_indices::WithIndices};
4+
5+
type Owner = Arc<str>;
6+
7+
type BorrowedValue<'a> = Vec<WithIndices<'a, &'a str>>;
8+
9+
self_cell::self_cell!(
10+
pub struct SourceContentLines {
11+
owner: Owner,
12+
#[covariant]
13+
dependent: BorrowedValue,
14+
}
15+
);
16+
17+
impl SourceContentLines {
18+
pub fn get(&self, line: usize) -> Option<&WithIndices<'_, &str>> {
19+
self.borrow_dependent().get(line)
20+
}
21+
}
22+
23+
impl From<Arc<str>> for SourceContentLines {
24+
fn from(value: Arc<str>) -> Self {
25+
SourceContentLines::new(value, |owner| {
26+
split_into_lines(&owner.as_ref())
27+
.map(WithIndices::new)
28+
.collect::<Vec<_>>()
29+
})
30+
}
31+
}
32+
33+
impl From<&Arc<str>> for SourceContentLines {
34+
fn from(value: &Arc<str>) -> Self {
35+
Self::from(value.clone())
36+
}
37+
}

0 commit comments

Comments
 (0)