Skip to content

Commit 67c8a8d

Browse files
committed
serialize: speed up json pretty printing by batch writing spaces
1 parent 717de50 commit 67c8a8d

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/libserialize/json.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,20 @@ fn escape_char(writer: &mut io::Writer, v: char) -> Result<(), io::IoError> {
299299
escape_bytes(writer, buf)
300300
}
301301

302-
fn spaces(writer: &mut io::Writer, n: uint) -> Result<(), io::IoError> {
303-
for _ in range(0, n) {
304-
try!(writer.write_str(" "));
302+
fn spaces(wr: &mut io::Writer, mut n: uint) -> Result<(), io::IoError> {
303+
static len: uint = 16;
304+
static buf: [u8, ..len] = [b' ', ..len];
305+
306+
while n >= len {
307+
try!(wr.write(buf));
308+
n -= len;
309+
}
310+
311+
if n > 0 {
312+
wr.write(buf.slice_to(n))
313+
} else {
314+
Ok(())
305315
}
306-
Ok(())
307316
}
308317

309318
fn fmt_number_or_null(v: f64) -> String {

0 commit comments

Comments
 (0)