Skip to content

Commit 59bac80

Browse files
committed
perf: build rewritten content into single buffer
1 parent 3e06919 commit 59bac80

1 file changed

Lines changed: 17 additions & 18 deletions

File tree

src/reaper.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,30 @@ pub fn rewrite_file(
1616
) -> std::io::Result<ReapedFile> {
1717
let source = fs::read_to_string(&info.file_path)?;
1818

19-
let new_imports: Vec<String> = info
20-
.statements
21-
.iter()
22-
.flat_map(|stmt| {
23-
stmt.imports.iter().filter_map(|imp| {
24-
Some(format_import(
25-
imp,
26-
exports.get(&imp.import_name)?,
27-
&info.file_path,
28-
ctx,
29-
))
30-
})
31-
})
32-
.collect();
33-
3419
let mut spans: Vec<Span> = info
3520
.statements
3621
.iter()
3722
.map(|s| expand_to_line_end(&source, s.span))
3823
.collect();
3924
spans.sort_by_key(|s| s.start);
40-
4125
let body = remove_spans(&source, &spans);
42-
let imports_rewritten = new_imports.len();
43-
let content = format!("{}\n{body}", new_imports.join("\n"));
26+
27+
let mut content = String::with_capacity(source.len());
28+
let mut imports_rewritten = 0;
29+
for stmt in &info.statements {
30+
for imp in &stmt.imports {
31+
let Some(export) = exports.get(&imp.import_name) else {
32+
continue;
33+
};
34+
if imports_rewritten > 0 {
35+
content.push('\n');
36+
}
37+
content.push_str(&format_import(imp, export, &info.file_path, ctx));
38+
imports_rewritten += 1;
39+
}
40+
}
41+
content.push('\n');
42+
content.push_str(&body);
4443

4544
Ok(ReapedFile {
4645
file_path: info.file_path.clone(),

0 commit comments

Comments
 (0)