Skip to content

Commit 7ee25dd

Browse files
authored
perf: reduce allocations for split_into_lines (#103)
* perf: reduce allocations for `split_into_lines`
1 parent 6f2089f commit 7ee25dd

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

src/helpers.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,8 @@ fn split(haystack: &str, needle: u8) -> impl Iterator<Item = &str> {
494494
}
495495

496496
// /[^\n]+\n?|\n/g
497-
pub fn split_into_lines(source: &str) -> Vec<&str> {
498-
split(source, b'\n').collect()
497+
pub fn split_into_lines(source: &str) -> impl Iterator<Item = &str> {
498+
split(source, b'\n')
499499
}
500500

501501
pub fn get_generated_source_info(source: &str) -> GeneratedInfo {
@@ -869,7 +869,7 @@ fn stream_chunks_of_source_map_lines_full(
869869
on_source: OnSource,
870870
_on_name: OnName,
871871
) -> GeneratedInfo {
872-
let lines = split_into_lines(source);
872+
let lines: Vec<&str> = split_into_lines(source).collect();
873873
if lines.is_empty() {
874874
return GeneratedInfo {
875875
generated_line: 1,
@@ -1100,7 +1100,6 @@ pub fn stream_chunks_of_combined_source_map(
11001100
{
11011101
Some(Rc::new(
11021102
split_into_lines(original_source)
1103-
.into_iter()
11041103
.map(|s| WithIndices::new(s.into()))
11051104
.collect(),
11061105
))

src/replace_source.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ impl<T: Source> StreamChunks for ReplaceSource<T> {
398398
}
399399
// Insert replacement content split into chunks by lines
400400
let repl = &repls[i];
401-
let lines: Vec<&str> = split_into_lines(&repl.content);
401+
let lines: Vec<&str> = split_into_lines(&repl.content).collect();
402402
let mut replacement_name_index = mapping
403403
.original
404404
.as_ref()
@@ -570,7 +570,6 @@ impl<T: Source> StreamChunks for ReplaceSource<T> {
570570
source_content_lines[source_index as usize] =
571571
source_content.map(|source_content| {
572572
split_into_lines(source_content)
573-
.into_iter()
574573
.map(|line| line.to_string())
575574
.collect()
576575
});
@@ -600,7 +599,7 @@ impl<T: Source> StreamChunks for ReplaceSource<T> {
600599

601600
// Insert remaining replacements content split into chunks by lines
602601
let mut line = result.generated_line as i64 + generated_line_offset;
603-
let matches = split_into_lines(&remainder);
602+
let matches: Vec<&str> = split_into_lines(&remainder).collect();
604603
for (m, content_line) in matches.iter().enumerate() {
605604
on_chunk(
606605
Some(content_line),

0 commit comments

Comments
 (0)