dtolnay / itoa

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

Use different buffer size for every integer type #16

Closed Object905 closed 6 years ago

Object905 commented 6 years ago

Allows to save some stack space. Useful with no_std. No public API changes.

Maximum lengths was generated with this.

Benchmarks are:

Before:

test bench_itoa_fmt::bench_i16_0      ... bench:           9 ns/iter (+/- 0)
test bench_itoa_fmt::bench_i16_min    ... bench:          12 ns/iter (+/- 0)
test bench_itoa_fmt::bench_u128_0     ... bench:          12 ns/iter (+/- 0)
test bench_itoa_fmt::bench_u128_max   ... bench:         254 ns/iter (+/- 2)
test bench_itoa_fmt::bench_u64_0      ... bench:           8 ns/iter (+/- 0)
test bench_itoa_fmt::bench_u64_half   ... bench:          24 ns/iter (+/- 1)
test bench_itoa_fmt::bench_u64_max    ... bench:          20 ns/iter (+/- 0)
test bench_itoa_write::bench_i16_0    ... bench:           9 ns/iter (+/- 0)
test bench_itoa_write::bench_i16_min  ... bench:          12 ns/iter (+/- 0)
test bench_itoa_write::bench_u128_0   ... bench:          12 ns/iter (+/- 0)
test bench_itoa_write::bench_u128_max ... bench:         253 ns/iter (+/- 5)
test bench_itoa_write::bench_u64_0    ... bench:           8 ns/iter (+/- 0)
test bench_itoa_write::bench_u64_half ... bench:          24 ns/iter (+/- 0)
test bench_itoa_write::bench_u64_max  ... bench:          21 ns/iter (+/- 0)

After:

test bench_itoa_fmt::bench_i16_0      ... bench:           9 ns/iter (+/- 0)
test bench_itoa_fmt::bench_i16_min    ... bench:          12 ns/iter (+/- 0)
test bench_itoa_fmt::bench_u128_0     ... bench:          12 ns/iter (+/- 0)
test bench_itoa_fmt::bench_u128_max   ... bench:         254 ns/iter (+/- 2)
test bench_itoa_fmt::bench_u64_0      ... bench:           8 ns/iter (+/- 0)
test bench_itoa_fmt::bench_u64_half   ... bench:          14 ns/iter (+/- 0)
test bench_itoa_fmt::bench_u64_max    ... bench:          20 ns/iter (+/- 0)
test bench_itoa_write::bench_i16_0    ... bench:           9 ns/iter (+/- 0)
test bench_itoa_write::bench_i16_min  ... bench:          12 ns/iter (+/- 1)
test bench_itoa_write::bench_u128_0   ... bench:          11 ns/iter (+/- 0)
test bench_itoa_write::bench_u128_max ... bench:         254 ns/iter (+/- 2)
test bench_itoa_write::bench_u64_0    ... bench:           8 ns/iter (+/- 0)
test bench_itoa_write::bench_u64_half ... bench:          13 ns/iter (+/- 0)
test bench_itoa_write::bench_u64_max  ... bench:          21 ns/iter (+/- 0)

After #[inline]:

test bench_itoa_fmt::bench_i16_0      ... bench:           8 ns/iter (+/- 0)
test bench_itoa_fmt::bench_i16_min    ... bench:          11 ns/iter (+/- 0)
test bench_itoa_fmt::bench_u128_0     ... bench:          11 ns/iter (+/- 0)
test bench_itoa_fmt::bench_u128_max   ... bench:         249 ns/iter (+/- 3)
test bench_itoa_fmt::bench_u64_0      ... bench:           8 ns/iter (+/- 0)
test bench_itoa_fmt::bench_u64_half   ... bench:          13 ns/iter (+/- 0)
test bench_itoa_fmt::bench_u64_max    ... bench:          19 ns/iter (+/- 1)
test bench_itoa_write::bench_i16_0    ... bench:           9 ns/iter (+/- 0)
test bench_itoa_write::bench_i16_min  ... bench:          11 ns/iter (+/- 0)
test bench_itoa_write::bench_u128_0   ... bench:          11 ns/iter (+/- 0)
test bench_itoa_write::bench_u128_max ... bench:         250 ns/iter (+/- 9)
test bench_itoa_write::bench_u64_0    ... bench:           8 ns/iter (+/- 0)
test bench_itoa_write::bench_u64_half ... bench:          13 ns/iter (+/- 0)
test bench_itoa_write::bench_u64_max  ... bench:          19 ns/iter (+/- 0)
dtolnay commented 6 years ago

I released this improvement in 0.4.1.