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

serialize_u64 is slow #84

Closed
dtolnay opened this issue Jun 25, 2016 · 3 comments
Closed

serialize_u64 is slow #84

dtolnay opened this issue Jun 25, 2016 · 3 comments
Assignees

Comments

@dtolnay
Copy link
Member

dtolnay commented Jun 25, 2016

It currently looks like this:

fn serialize_u64(&mut self, value: u64) -> Result<()> {
    write!(&mut self.writer, "{}", value).map_err(From::from)
}

I tested the following variation and it made an astonishing 20% difference in the bench_log benchmarks (294 MB/s vs 244 MB/s).

if value == 0 {
    self.writer.write_all(b"0").map_err(From::from)
} else {
    let mut scratch = [0u8; 20];
    let mut len = 0;
    while value > 0 {
        scratch[19 - len] = (value % 10) as u8 + b'0';
        value /= 10;
        len += 1;
    }
    self.writer.write_all(&scratch[20-len..20]).map_err(From::from)
}

That is enough to motivate looking for a better solution, or contributing a fix to Rust for whatever is making write! slow.

@dtolnay
Copy link
Member Author

dtolnay commented Jun 25, 2016

Here is the code that write! is using: num.rs#L201-L254.

@dtolnay
Copy link
Member Author

dtolnay commented Jun 25, 2016

The fmt method I linked to is way faster than mine. All the slowness comes from pad_integral which we shouldn't even need because we want the default format. I will look for a way to avoid going through pad_integral.

@dtolnay
Copy link
Member Author

dtolnay commented Jun 27, 2016

Fixed in #87.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

1 participant