From 67c8a8d5dd90e476d0579df3738fe1e744ef68f1 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Fri, 4 Jul 2014 16:56:23 -0700 Subject: [PATCH] serialize: speed up json pretty printing by batch writing spaces --- src/libserialize/json.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/libserialize/json.rs b/src/libserialize/json.rs index bb044f65348..36c8b308f41 100644 --- a/src/libserialize/json.rs +++ b/src/libserialize/json.rs @@ -299,11 +299,20 @@ fn escape_char(writer: &mut io::Writer, v: char) -> Result<(), io::IoError> { escape_bytes(writer, buf) } -fn spaces(writer: &mut io::Writer, n: uint) -> Result<(), io::IoError> { - for _ in range(0, n) { - try!(writer.write_str(" ")); +fn spaces(wr: &mut io::Writer, mut n: uint) -> Result<(), io::IoError> { + static len: uint = 16; + static buf: [u8, ..len] = [b' ', ..len]; + + while n >= len { + try!(wr.write(buf)); + n -= len; + } + + if n > 0 { + wr.write(buf.slice_to(n)) + } else { + Ok(()) } - Ok(()) } fn fmt_number_or_null(v: f64) -> String {