Skip to content

Commit baeed88

Browse files
committed
auto merge of #10060 : alexcrichton/rust/cached-stdout, r=brson
Almost all languages provide some form of buffering of the stdout stream, and this commit adds this feature for rust. A handle to stdout is lazily initialized in the Task structure as a buffered owned Writer trait object. The buffer behavior depends on where stdout is directed to. Like C, this line-buffers the stream when the output goes to a terminal (flushes on newlines), and also like C this uses a fixed-size buffer when output is not directed at a terminal. We may decide the fixed-size buffering is overkill, but it certainly does reduce write syscall counts when piping output elsewhere. This is a *huge* benefit to any code using logging macros or the printing macros. Formatting emits calls to `write` very frequently, and to have each of them backed by a write syscall was very expensive. In a local benchmark of printing 10000 lines of "what" to stdout, I got the following timings: when | terminal | redirected ----------|---------------|-------- before | 0.575s | 0.525s after | 0.197s | 0.013s C | 0.019s | 0.004s I can also confirm that we're buffering the output appropriately in both situtations. We're still far slower than C, but I believe much of that has to do with the "homing" that all tasks due, we're still performing an order of magnitude more write syscalls than C does.
2 parents deeca5d + e8f72c3 commit baeed88

File tree

7 files changed

+139
-31
lines changed

7 files changed

+139
-31
lines changed

src/libstd/rt/io/buffered.rs

+53-7
Original file line numberDiff line numberDiff line change
@@ -221,17 +221,48 @@ impl<W: Writer> Writer for BufferedWriter<W> {
221221
}
222222

223223
impl<W: Writer> Decorator<W> for BufferedWriter<W> {
224-
fn inner(self) -> W {
225-
self.inner
226-
}
224+
fn inner(self) -> W { self.inner }
225+
fn inner_ref<'a>(&'a self) -> &'a W { &self.inner }
226+
fn inner_mut_ref<'a>(&'a mut self) -> &'a mut W { &mut self.inner }
227+
}
227228

228-
fn inner_ref<'a>(&'a self) -> &'a W {
229-
&self.inner
229+
/// Wraps a Writer and buffers output to it, flushing whenever a newline (0xa,
230+
/// '\n') is detected.
231+
///
232+
/// Note that this structure does NOT flush the output when dropped.
233+
pub struct LineBufferedWriter<W> {
234+
priv inner: BufferedWriter<W>,
235+
}
236+
237+
impl<W: Writer> LineBufferedWriter<W> {
238+
/// Creates a new `LineBufferedWriter`
239+
pub fn new(inner: W) -> LineBufferedWriter<W> {
240+
// Lines typically aren't that long, don't use a giant buffer
241+
LineBufferedWriter {
242+
inner: BufferedWriter::with_capacity(1024, inner)
243+
}
230244
}
245+
}
231246

232-
fn inner_mut_ref<'a>(&'a mut self) -> &'a mut W {
233-
&mut self.inner
247+
impl<W: Writer> Writer for LineBufferedWriter<W> {
248+
fn write(&mut self, buf: &[u8]) {
249+
match buf.iter().position(|&b| b == '\n' as u8) {
250+
Some(i) => {
251+
self.inner.write(buf.slice_to(i + 1));
252+
self.inner.flush();
253+
self.inner.write(buf.slice_from(i + 1));
254+
}
255+
None => self.inner.write(buf),
256+
}
234257
}
258+
259+
fn flush(&mut self) { self.inner.flush() }
260+
}
261+
262+
impl<W: Writer> Decorator<W> for LineBufferedWriter<W> {
263+
fn inner(self) -> W { self.inner.inner() }
264+
fn inner_ref<'a>(&'a self) -> &'a W { self.inner.inner_ref() }
265+
fn inner_mut_ref<'a>(&'a mut self) -> &'a mut W { self.inner.inner_mut_ref() }
235266
}
236267

237268
struct InternalBufferedWriter<W>(BufferedWriter<W>);
@@ -413,4 +444,19 @@ mod test {
413444
assert_eq!(reader.read_until(8), Some(~[0]));
414445
assert_eq!(reader.read_until(9), None);
415446
}
447+
448+
#[test]
449+
fn test_line_buffer() {
450+
let mut writer = LineBufferedWriter::new(MemWriter::new());
451+
writer.write([0]);
452+
assert_eq!(*writer.inner_ref().inner_ref(), ~[]);
453+
writer.write([1]);
454+
assert_eq!(*writer.inner_ref().inner_ref(), ~[]);
455+
writer.flush();
456+
assert_eq!(*writer.inner_ref().inner_ref(), ~[0, 1]);
457+
writer.write([0, '\n' as u8, 1]);
458+
assert_eq!(*writer.inner_ref().inner_ref(), ~[0, 1, 0, '\n' as u8]);
459+
writer.flush();
460+
assert_eq!(*writer.inner_ref().inner_ref(), ~[0, 1, 0, '\n' as u8, 1]);
461+
}
416462
}

src/libstd/rt/io/stdio.rs

+55-13
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use fmt;
3030
use libc;
3131
use option::{Option, Some, None};
3232
use result::{Ok, Err};
33+
use rt::io::buffered::{LineBufferedWriter, BufferedWriter};
3334
use rt::rtio::{IoFactory, RtioTTY, RtioFileStream, with_local_io,
3435
CloseAsynchronously};
3536
use super::{Reader, Writer, io_error, IoError, OtherIoError};
@@ -111,37 +112,78 @@ pub fn stderr() -> StdWriter {
111112
do src(libc::STDERR_FILENO, false) |src| { StdWriter { inner: src } }
112113
}
113114

115+
/// Executes a closure with the local task's handle on stdout. By default, this
116+
/// stream is a buffering stream, so the handled yielded to the given closure
117+
/// can be used to flush the stdout stream (if necessary). The buffering used is
118+
/// line-buffering when stdout is attached to a terminal, and a fixed sized
119+
/// buffer if it is not attached to a terminal.
120+
///
121+
/// Note that handles generated via the `stdout()` function all output to the
122+
/// same stream, and output among all task may be interleaved as a result of
123+
/// this. This is provided to have access to the default stream for `print` and
124+
/// `println` (and the related macros) for this task.
125+
///
126+
/// Also note that logging macros do not use this stream. Using the logging
127+
/// macros will emit output to stderr.
128+
pub fn with_task_stdout(f: &fn(&mut Writer)) {
129+
use rt::local::Local;
130+
use rt::task::Task;
131+
132+
unsafe {
133+
// Logging may require scheduling operations, so we can't remove the
134+
// task from TLS right now, hence the unsafe borrow. Sad.
135+
let task: *mut Task = Local::unsafe_borrow();
136+
137+
match (*task).stdout_handle {
138+
Some(ref mut handle) => f(*handle),
139+
None => {
140+
let handle = stdout();
141+
let mut handle = if handle.isatty() {
142+
~LineBufferedWriter::new(handle) as ~Writer
143+
} else {
144+
// The default capacity is very large, 64k, but this is just
145+
// a stdout stream, and possibly per task, so let's not make
146+
// this too expensive.
147+
~BufferedWriter::with_capacity(4096, handle) as ~Writer
148+
};
149+
f(handle);
150+
(*task).stdout_handle = Some(handle);
151+
}
152+
}
153+
}
154+
}
155+
114156
/// Prints a string to the stdout of the current process. No newline is emitted
115157
/// after the string is printed.
116158
pub fn print(s: &str) {
117-
// XXX: need to see if not caching stdin() is the cause of performance
118-
// issues, it should be possible to cache a stdout handle in each Task
119-
// and then re-use that across calls to print/println. Note that the
120-
// resolution of this comment will affect all of the prints below as
121-
// well.
122-
stdout().write(s.as_bytes());
159+
do with_task_stdout |io| {
160+
io.write(s.as_bytes());
161+
}
123162
}
124163

125164
/// Prints a string as a line. to the stdout of the current process. A literal
126165
/// `\n` character is printed to the console after the string.
127166
pub fn println(s: &str) {
128-
let mut out = stdout();
129-
out.write(s.as_bytes());
130-
out.write(['\n' as u8]);
167+
do with_task_stdout |io| {
168+
io.write(s.as_bytes());
169+
io.write(['\n' as u8]);
170+
}
131171
}
132172

133173
/// Similar to `print`, but takes a `fmt::Arguments` structure to be compatible
134174
/// with the `format_args!` macro.
135175
pub fn print_args(fmt: &fmt::Arguments) {
136-
let mut out = stdout();
137-
fmt::write(&mut out as &mut Writer, fmt);
176+
do with_task_stdout |io| {
177+
fmt::write(io, fmt);
178+
}
138179
}
139180

140181
/// Similar to `println`, but takes a `fmt::Arguments` structure to be
141182
/// compatible with the `format_args!` macro.
142183
pub fn println_args(fmt: &fmt::Arguments) {
143-
let mut out = stdout();
144-
fmt::writeln(&mut out as &mut Writer, fmt);
184+
do with_task_stdout |io| {
185+
fmt::writeln(io, fmt);
186+
}
145187
}
146188

147189
/// Representation of a reader of a standard input stream

src/libstd/rt/logging.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use from_str::from_str;
1313
use libc::exit;
1414
use option::{Some, None, Option};
1515
use rt::io;
16+
use rt::io::stdio::StdWriter;
17+
use rt::io::buffered::LineBufferedWriter;
1618
use rt::crate_map::{ModEntry, CrateMap, iter_crate_map, get_crate_map};
1719
use str::StrSlice;
1820
use u32;
@@ -170,7 +172,7 @@ pub trait Logger {
170172
/// This logger emits output to the stderr of the process, and contains a lazily
171173
/// initialized event-loop driven handle to the stream.
172174
pub struct StdErrLogger {
173-
priv handle: Option<io::stdio::StdWriter>,
175+
priv handle: Option<LineBufferedWriter<StdWriter>>,
174176
}
175177

176178
impl StdErrLogger {
@@ -181,7 +183,7 @@ impl Logger for StdErrLogger {
181183
fn log(&mut self, args: &fmt::Arguments) {
182184
// First time logging? Get a handle to the stderr of this process.
183185
if self.handle.is_none() {
184-
self.handle = Some(io::stderr());
186+
self.handle = Some(LineBufferedWriter::new(io::stderr()));
185187
}
186188
fmt::writeln(self.handle.get_mut_ref() as &mut io::Writer, args);
187189
}

src/libstd/rt/task.rs

+22-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use option::{Option, Some, None};
2323
use rt::borrowck;
2424
use rt::borrowck::BorrowRecord;
2525
use rt::env;
26+
use rt::io::Writer;
2627
use rt::kill::Death;
2728
use rt::local::Local;
2829
use rt::logging::StdErrLogger;
@@ -56,7 +57,8 @@ pub struct Task {
5657
sched: Option<~Scheduler>,
5758
task_type: TaskType,
5859
// Dynamic borrowck debugging info
59-
borrow_list: Option<~[BorrowRecord]>
60+
borrow_list: Option<~[BorrowRecord]>,
61+
stdout_handle: Option<~Writer>,
6062
}
6163

6264
pub enum TaskType {
@@ -141,7 +143,8 @@ impl Task {
141143
name: None,
142144
sched: None,
143145
task_type: SchedTask,
144-
borrow_list: None
146+
borrow_list: None,
147+
stdout_handle: None,
145148
}
146149
}
147150

@@ -175,7 +178,8 @@ impl Task {
175178
coroutine: Some(Coroutine::new(stack_pool, stack_size, start)),
176179
sched: None,
177180
task_type: GreenTask(Some(home)),
178-
borrow_list: None
181+
borrow_list: None,
182+
stdout_handle: None,
179183
}
180184
}
181185

@@ -198,7 +202,8 @@ impl Task {
198202
coroutine: Some(Coroutine::new(stack_pool, stack_size, start)),
199203
sched: None,
200204
task_type: GreenTask(Some(home)),
201-
borrow_list: None
205+
borrow_list: None,
206+
stdout_handle: None,
202207
}
203208
}
204209

@@ -234,6 +239,7 @@ impl Task {
234239

235240
// Run the task main function, then do some cleanup.
236241
do f.finally {
242+
237243
// First, destroy task-local storage. This may run user dtors.
238244
//
239245
// FIXME #8302: Dear diary. I'm so tired and confused.
@@ -257,6 +263,17 @@ impl Task {
257263

258264
// Destroy remaining boxes. Also may run user dtors.
259265
unsafe { cleanup::annihilate(); }
266+
267+
// Finally flush and destroy any output handles which the task
268+
// owns. There are no boxes here, and no user destructors should
269+
// run after this any more.
270+
match self.stdout_handle.take() {
271+
Some(handle) => {
272+
let mut handle = handle;
273+
handle.flush();
274+
}
275+
None => {}
276+
}
260277
}
261278
}
262279

@@ -331,7 +348,7 @@ impl Task {
331348
impl Drop for Task {
332349
fn drop(&mut self) {
333350
rtdebug!("called drop for a task: {}", borrow::to_uint(self));
334-
rtassert!(self.destroyed)
351+
rtassert!(self.destroyed);
335352
}
336353
}
337354

src/libstd/rt/uv/uvio.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1782,7 +1782,7 @@ impl RtioTTY for UvTTY {
17821782
}
17831783

17841784
fn isatty(&self) -> bool {
1785-
unsafe { uvll::guess_handle(self.fd) == uvll::UV_TTY }
1785+
unsafe { uvll::guess_handle(self.fd) == uvll::UV_TTY as c_int }
17861786
}
17871787
}
17881788

src/libstd/rt/uv/uvll.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,8 @@ pub unsafe fn tty_get_winsize(tty: *uv_tty_t, width: *c_int,
977977
#[fixed_stack_segment]; #[inline(never)];
978978
rust_uv_tty_get_winsize(tty, width, height)
979979
}
980-
pub unsafe fn guess_handle(fd: c_int) -> uv_handle_type {
980+
// FIXME(#9613) this should return uv_handle_type, not a c_int
981+
pub unsafe fn guess_handle(fd: c_int) -> c_int {
981982
#[fixed_stack_segment]; #[inline(never)];
982983
rust_uv_guess_handle(fd)
983984
}
@@ -1148,7 +1149,7 @@ extern {
11481149
fn rust_uv_tty_set_mode(tty: *uv_tty_t, mode: c_int) -> c_int;
11491150
fn rust_uv_tty_get_winsize(tty: *uv_tty_t, width: *c_int,
11501151
height: *c_int) -> c_int;
1151-
fn rust_uv_guess_handle(fd: c_int) -> uv_handle_type;
1152+
fn rust_uv_guess_handle(fd: c_int) -> c_int;
11521153

11531154
// XXX: see comments in addrinfo.rs
11541155
// These should all really be constants...

src/rt/rust_uv.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ rust_uv_tty_get_winsize(uv_tty_t *tty, int *width, int *height) {
659659
return uv_tty_get_winsize(tty, width, height);
660660
}
661661

662-
extern "C" uv_handle_type
662+
extern "C" int
663663
rust_uv_guess_handle(int fd) {
664664
return uv_guess_handle(fd);
665665
}

0 commit comments

Comments
 (0)