dtolnay / itoa

Fast function for printing integer primitives to a decimal string
Apache License 2.0
306 stars 37 forks source link

Optimize performance of writing u128/i128 on 64bit system #26

Closed Kogia-sima closed 3 years ago

Kogia-sima commented 4 years ago

itoap crate has very fast implementation for writing u128 in decimal format. It is derived from 6502's method, but is optimized for 64-bit system.

I ported that implementation to itoa repository and now performance has dramatically improved.

Before

test bench_itoa_fmt::bench_u128_0     ... bench:          31 ns/iter (+/- 0)
test bench_itoa_fmt::bench_u128_max   ... bench:         433 ns/iter (+/- 2)
test bench_itoa_write::bench_u128_0   ... bench:          31 ns/iter (+/- 0)
test bench_itoa_write::bench_u128_max ... bench:         434 ns/iter (+/- 2)

After

test bench_itoa_fmt::bench_u128_0     ... bench:          30 ns/iter (+/- 0)
test bench_itoa_fmt::bench_u128_max   ... bench:         120 ns/iter (+/- 0)
test bench_itoa_write::bench_u128_0   ... bench:          30 ns/iter (+/- 0)
test bench_itoa_write::bench_u128_max ... bench:         121 ns/iter (+/- 0)

Note that this algorithm passes random tests, random digits tests, and boundary tests. (https://github.com/Kogia-sima/itoap/blob/ae36b332dac2495b4c2ff32977ea8f5080a083b6/src/lib.rs#L297-L421)

Kogia-sima commented 3 years ago

Closing in favour of #27