Skip to content

Commit

Permalink
auto merge of rust-lang#7608 : glinscott/rust/json_perf, r=pcwalton
Browse files Browse the repository at this point in the history
Avoids the overhead of read_char for every character.

Benchmark reading example.json 10 times from
https://code.google.com/p/rapidjson/wiki/Performance

Before: 2.55s
After:  0.16s

Regression testing is already done by isrustfastyet.
  • Loading branch information
bors committed Jul 8, 2013
2 parents f7b293b + 149c976 commit f503e53
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
35 changes: 28 additions & 7 deletions src/libextra/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,22 +481,30 @@ pub fn to_pretty_str(json: &Json) -> ~str {
io::with_str_writer(|wr| to_pretty_writer(wr, json))
}

static BUF_SIZE : uint = 64000;

#[allow(missing_doc)]
pub struct Parser {
priv rdr: @io::Reader,
priv buf: ~[char],
priv buf_idx: uint,
priv ch: char,
priv line: uint,
priv col: uint,
}

/// Decode a json value from an io::reader
pub fn Parser(rdr: @io::Reader) -> Parser {
Parser {
let mut p = Parser {
rdr: rdr,
ch: rdr.read_char(),
buf: rdr.read_chars(BUF_SIZE),
buf_idx: 0,
ch: 0 as char,
line: 1,
col: 1,
}
col: 0,
};
p.bump();
p
}

impl Parser {
Expand All @@ -521,13 +529,26 @@ impl Parser {
fn eof(&self) -> bool { self.ch == -1 as char }

fn bump(&mut self) {
self.ch = self.rdr.read_char();
if self.eof() {
return;
}

self.col += 1u;

if self.buf_idx >= self.buf.len() {
self.buf = self.rdr.read_chars(BUF_SIZE);
if self.buf.len() == 0 {
self.ch = -1 as char;
return;
}
self.buf_idx = 0;
}
self.ch = self.buf[self.buf_idx];
self.buf_idx += 1;

if self.ch == '\n' {
self.line += 1u;
self.col = 1u;
} else {
self.col += 1u;
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/libstd/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ pub fn is_uppercase(c: char) -> bool { general_category::Lu(c) }
///
#[inline]
pub fn is_whitespace(c: char) -> bool {
('\x09' <= c && c <= '\x0d')
c == ' '
|| ('\x09' <= c && c <= '\x0d')
|| general_category::Zs(c)
|| general_category::Zl(c)
|| general_category::Zp(c)
Expand Down

0 comments on commit f503e53

Please sign in to comment.