Skip to content

Commit a6137fb

Browse files
committed
compiletest: Remove unnecessary memory allocation in iter_header
Replace `BufRead::lines` with `BuRead::read_line` to reduce memory allocations.
1 parent 320ada6 commit a6137fb

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/tools/compiletest/src/header.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -628,12 +628,18 @@ fn iter_header(testfile: &Path, cfg: Option<&str>, it: &mut dyn FnMut(&str)) {
628628
// It took me like 2 days to debug why compile-flags weren’t taken into account for my test :)
629629
let comment_with_brace = comment.to_string() + "[";
630630

631-
let rdr = BufReader::new(File::open(testfile).unwrap());
632-
for ln in rdr.lines() {
631+
let mut rdr = BufReader::new(File::open(testfile).unwrap());
632+
let mut ln = String::new();
633+
634+
loop {
635+
ln.clear();
636+
if rdr.read_line(&mut ln).unwrap() == 0 {
637+
break;
638+
}
639+
633640
// Assume that any directives will be found before the first
634641
// module or function. This doesn't seem to be an optimization
635642
// with a warm page cache. Maybe with a cold one.
636-
let ln = ln.unwrap();
637643
let ln = ln.trim();
638644
if ln.starts_with("fn") || ln.starts_with("mod") {
639645
return;

0 commit comments

Comments
 (0)