Skip to content

Commit 17b6df9

Browse files
committed
Avoid retaining all rustc output in memory.
1 parent 1f730be commit 17b6df9

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

src/cargo/core/compiler/job_queue.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'a> JobState<'a> {
113113
&self,
114114
cmd: &ProcessBuilder,
115115
prefix: Option<String>,
116-
print_output: bool,
116+
capture_output: bool,
117117
) -> CargoResult<Output> {
118118
let prefix = prefix.unwrap_or_else(|| String::new());
119119
cmd.exec_with_streaming(
@@ -125,7 +125,7 @@ impl<'a> JobState<'a> {
125125
let _ = self.tx.send(Message::Stderr(format!("{}{}", prefix, err)));
126126
Ok(())
127127
},
128-
print_output,
128+
capture_output,
129129
)
130130
}
131131
}

src/cargo/util/process_builder.rs

+25-15
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ impl ProcessBuilder {
202202
&self,
203203
on_stdout_line: &mut FnMut(&str) -> CargoResult<()>,
204204
on_stderr_line: &mut FnMut(&str) -> CargoResult<()>,
205-
print_output: bool,
205+
capture_output: bool,
206206
) -> CargoResult<Output> {
207207
let mut stdout = Vec::new();
208208
let mut stderr = Vec::new();
@@ -226,23 +226,33 @@ impl ProcessBuilder {
226226
None => return,
227227
}
228228
};
229-
let data = data.drain(..idx);
230-
let dst = if is_out { &mut stdout } else { &mut stderr };
231-
let start = dst.len();
232-
dst.extend(data);
233-
for line in String::from_utf8_lossy(&dst[start..]).lines() {
234-
if callback_error.is_some() {
235-
break;
236-
}
237-
let callback_result = if is_out {
238-
on_stdout_line(line)
229+
{ // scope for new_lines
230+
let new_lines = if capture_output {
231+
let dst = if is_out { &mut stdout } else { &mut stderr };
232+
let start = dst.len();
233+
let data = data.drain(..idx);
234+
dst.extend(data);
235+
&dst[start..]
239236
} else {
240-
on_stderr_line(line)
237+
&data[..idx]
241238
};
242-
if let Err(e) = callback_result {
243-
callback_error = Some(e);
239+
for line in String::from_utf8_lossy(new_lines).lines() {
240+
if callback_error.is_some() {
241+
break;
242+
}
243+
let callback_result = if is_out {
244+
on_stdout_line(line)
245+
} else {
246+
on_stderr_line(line)
247+
};
248+
if let Err(e) = callback_result {
249+
callback_error = Some(e);
250+
}
244251
}
245252
}
253+
if !capture_output {
254+
data.drain(..idx);
255+
}
246256
})?;
247257
child.wait()
248258
})()
@@ -260,7 +270,7 @@ impl ProcessBuilder {
260270
};
261271

262272
{
263-
let to_print = if print_output { Some(&output) } else { None };
273+
let to_print = if capture_output { Some(&output) } else { None };
264274
if !output.status.success() {
265275
return Err(process_error(
266276
&format!("process didn't exit successfully: {}", self),

tests/testsuite/support/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ impl Execs {
770770
process.exec_with_streaming(
771771
&mut |out| Ok(println!("{}", out)),
772772
&mut |err| Ok(eprintln!("{}", err)),
773-
false,
773+
true,
774774
)
775775
} else {
776776
process.exec_with_output()

0 commit comments

Comments
 (0)