Skip to content

serialize_u64 is slow #84

@dtolnay

Description

@dtolnay

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions