Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't allocate a string when calling println #9979

Merged
merged 1 commit into from Oct 21, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/libstd/rt/io/stdio.rs
Expand Up @@ -8,6 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use fmt;
use libc;
use option::{Option, Some, None};
use result::{Ok, Err};
Expand Down Expand Up @@ -56,7 +57,9 @@ pub fn stderr() -> StdWriter {
pub fn print(s: &str) {
// XXX: need to see if not caching stdin() is the cause of performance
// issues, it should be possible to cache a stdout handle in each Task
// and then re-use that across calls to print/println
// and then re-use that across calls to print/println. Note that the
// resolution of this comment will affect all of the prints below as
// well.
stdout().write(s.as_bytes());
}

Expand All @@ -68,6 +71,20 @@ pub fn println(s: &str) {
out.write(['\n' as u8]);
}

/// Similar to `print`, but takes a `fmt::Arguments` structure to be compatible
/// with the `format_args!` macro.
pub fn print_args(fmt: &fmt::Arguments) {
let mut out = stdout();
fmt::write(&mut out as &mut Writer, fmt);
}

/// Similar to `println`, but takes a `fmt::Arguments` structure to be
/// compatible with the `format_args!` macro.
pub fn println_args(fmt: &fmt::Arguments) {
let mut out = stdout();
fmt::writeln(&mut out as &mut Writer, fmt);
}

/// Representation of a reader of a standard input stream
pub struct StdReader {
priv inner: ~RtioFileStream
Expand Down
9 changes: 2 additions & 7 deletions src/libsyntax/ext/expand.rs
Expand Up @@ -999,16 +999,11 @@ pub fn std_macros() -> @str {
macro_rules! writeln(($dst:expr, $($arg:tt)*) => (
format_args!(|args| { ::std::fmt::writeln($dst, args) }, $($arg)*)
))
// FIXME(#6846) once stdio is redesigned, this shouldn't perform an
// allocation but should rather delegate to an invocation of
// write! instead of format!
macro_rules! print (
($($arg:tt)*) => (::std::io::print(format!($($arg)*)))
($($arg:tt)*) => (format_args!(::std::rt::io::stdio::print_args, $($arg)*))
)
// FIXME(#6846) once stdio is redesigned, this shouldn't perform an
// allocation but should rather delegate to an io::Writer
macro_rules! println (
($($arg:tt)*) => (::std::io::println(format!($($arg)*)))
($($arg:tt)*) => (format_args!(::std::rt::io::stdio::println_args, $($arg)*))
)

macro_rules! local_data_key (
Expand Down