Skip to content

Commit 7b91c0e

Browse files
authored
Sync to dtolnay/ryu master (#17)
1 parent 85fecf5 commit 7b91c0e

File tree

22 files changed

+266
-299
lines changed

22 files changed

+266
-299
lines changed

.clippy.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
msrv = "1.31.0"
1+
msrv = "1.36.0"

.github/workflows/ci.yml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@ jobs:
1919
with:
2020
toolchain: ${{matrix.rust}}
2121
- run: cargo test
22+
- run: cargo test --features small
2223
- run: cargo build --tests --features no-panic --release
2324
if: matrix.rust == 'nightly'
2425

2526
msrv:
26-
name: Rust 1.31.0
27+
name: Rust 1.36.0
2728
runs-on: ubuntu-latest
2829
steps:
2930
- uses: actions/checkout@v2
30-
- uses: dtolnay/rust-toolchain@1.31.0
31+
- uses: dtolnay/rust-toolchain@1.36.0
3132
- run: cargo build
3233
- run: cargo build --features small
3334

@@ -40,11 +41,23 @@ jobs:
4041
with:
4142
components: miri
4243
- run: cargo miri test
44+
env:
45+
MIRIFLAGS: "-Zmiri-tag-raw-pointers"
4346

4447
clippy:
4548
name: Clippy
4649
runs-on: ubuntu-latest
50+
if: github.event_name != 'pull_request'
4751
steps:
4852
- uses: actions/checkout@v2
4953
- uses: dtolnay/rust-toolchain@clippy
50-
- run: cargo clippy -- -Dclippy::all -Dclippy::pedantic
54+
- run: cargo clippy --tests --benches -- -Dclippy::all -Dclippy::pedantic
55+
56+
outdated:
57+
name: Outdated
58+
runs-on: ubuntu-latest
59+
if: github.event_name != 'pull_request'
60+
steps:
61+
- uses: actions/checkout@v2
62+
- uses: dtolnay/install@cargo-outdated
63+
- run: cargo outdated --exit-code 1

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ license = "Apache-2.0 OR BSL-1.0"
66
description = "Fast floating point to string conversion, ECMAScript compliant."
77
repository = "https://github.com/boa-dev/ryu-js"
88
documentation = "https://docs.rs/ryu-js"
9+
categories = ["value-formatting"]
910
readme = "README.md"
11+
exclude = ["performance.png", "chart/**"]
1012
edition = "2018"
13+
rust-version = "1.36"
1114

1215
[features]
1316
# Use smaller lookup tables. Instead of storing every required power of
@@ -21,8 +24,8 @@ no-panic = { version = "0.1", optional = true }
2124

2225
[dev-dependencies]
2326
num_cpus = "1.8"
24-
rand = "0.7"
25-
rand_xorshift = "0.2"
27+
rand = "0.8"
28+
rand_xorshift = "0.3"
2629

2730
[package.metadata.docs.rs]
2831
targets = ["x86_64-unknown-linux-gnu"]

README.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ under the creative commons CC-BY-SA license.
1717
This Rust implementation is a line-by-line port of Ulf Adams' implementation in
1818
C, [https://github.com/ulfjack/ryu][upstream].
1919

20-
*Requirements: this crate supports any compiler version back to rustc 1.31; it
20+
*Requirements: this crate supports any compiler version back to rustc 1.36; it
2121
uses nothing from the Rust standard library so is usable from no_std crates.*
2222

2323
[paper]: https://dl.acm.org/citation.cfm?id=3192369
@@ -28,6 +28,8 @@ uses nothing from the Rust standard library so is usable from no_std crates.*
2828
ryu-js = "0.2"
2929
```
3030

31+
<br>
32+
3133
## Example
3234

3335
```rust
@@ -38,7 +40,27 @@ fn main() {
3840
}
3941
```
4042

41-
## Performance
43+
<br>
44+
45+
## Performance (lower is better)
46+
47+
![performance](https://raw.githubusercontent.com/boa-dev/ryu-js/master/performance.png)
48+
49+
You can run upstream's benchmarks with:
50+
51+
```console
52+
$ git clone https://github.com/ulfjack/ryu c-ryu
53+
$ cd c-ryu
54+
$ bazel run -c opt //ryu/benchmark:ryu_benchmark --
55+
```
56+
57+
And the same benchmark against our implementation with:
58+
59+
```console
60+
$ git clone https://github.com/boa-dev/ryu-js rust-ryu
61+
$ cd rust-ryu
62+
$ cargo run --example upstream_benchmark --release
63+
```
4264

4365
The benchmarks measure the average time to print a 32-bit float and average
4466
time to print a 64-bit float, where the inputs are distributed as uniform random
@@ -55,20 +77,10 @@ standard library which you can run with:
5577
$ cargo bench
5678
```
5779

58-
The benchmark shows Ryū approximately 4-10x faster than the standard library
80+
The benchmark shows Ryū approximately 2-5x faster than the standard library
5981
across a range of f32 and f64 inputs. Measurements are in nanoseconds per
6082
iteration; smaller is better.
6183

62-
| type=f32 | 0.0 | 0.1234 | 2.718281828459045 | f32::MAX |
63-
|:--------:|:----:|:------:|:-----------------:|:--------:|
64-
| RYU | 3ns | 28ns | 23ns | 22ns |
65-
| STD | 40ns | 106ns | 128ns | 110ns |
66-
67-
| type=f64 | 0.0 | 0.1234 | 2.718281828459045 | f64::MAX |
68-
|:--------:|:----:|:------:|:-----------------:|:--------:|
69-
| RYU | 3ns | 50ns | 35ns | 32ns |
70-
| STD | 39ns | 105ns | 128ns | 202ns |
71-
7284
## Formatting
7385

7486
This library tends to produce more human-readable output than the standard

benches/bench.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
// cargo bench
22

33
#![feature(test)]
4+
#![allow(
5+
clippy::approx_constant,
6+
clippy::excessive_precision,
7+
clippy::unreadable_literal
8+
)]
49

510
extern crate test;
611

build.rs

Lines changed: 0 additions & 40 deletions
This file was deleted.

chart/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*.aux
2+
*.fdb_latexmk
3+
*.fls
4+
*.log
5+
*.pdf
6+
*.png
7+
*.svg

chart/performance.tex

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
\documentclass{standalone}
2+
\usepackage{pgfplots}
3+
\usepackage{sansmath}
4+
\pgfplotsset{compat=1.16}
5+
\definecolor{ryu}{HTML}{3366FF}
6+
\definecolor{std}{HTML}{949494}
7+
\definecolor{bg}{HTML}{CFCFCF}
8+
\begin{document}
9+
\pagecolor{white}
10+
\begin{tikzpicture}
11+
\edef\entries{
12+
"$0.0$",
13+
"$0.1234$",
14+
"$2.718281828459045$",
15+
"$1.7976931348623157e308$",
16+
}
17+
\begin{axis}[
18+
width=5in,
19+
height=3.5in,
20+
ybar,
21+
ymin=0,
22+
bar width=24pt,
23+
enlarge x limits={abs=39pt},
24+
ylabel={nanos for one call to write},
25+
legend style={
26+
anchor=north west,
27+
at={(0.025,0.975)},
28+
legend columns=1,
29+
draw=none,
30+
fill=none,
31+
},
32+
legend entries={
33+
ryu::Buffer::new().format\_finite(value)\\
34+
std::write!(\&mut buf, ``\{\}'', value)\\
35+
},
36+
legend cell align=left,
37+
xtick={-0.5,0.5,1.5,2.5,3.5,4.5},
38+
xticklabels={},
39+
xtick pos=left,
40+
visualization depends on={y \as \rawy},
41+
every node near coord/.append style={
42+
shift={(axis direction cs:0,-\rawy/2)},
43+
rotate=90,
44+
anchor=center,
45+
font=\sansmath\sffamily,
46+
},
47+
axis background/.style={fill=bg},
48+
tick label style={font=\sansmath\sffamily},
49+
every axis label={font=\sansmath\sffamily},
50+
legend style={font=\sansmath\sffamily},
51+
label style={font=\sansmath\sffamily},
52+
]
53+
\addplot[
54+
black,
55+
fill=ryu,
56+
area legend,
57+
nodes near coords={},
58+
] coordinates {
59+
(0, 3)
60+
(1, 40)
61+
(2, 27)
62+
(3, 28)
63+
};
64+
\addplot[
65+
black,
66+
fill=std,
67+
area legend,
68+
nodes near coords=\pgfmathsetmacro{\input}{{\entries}[\coordindex]}\input,
69+
] coordinates {
70+
(0, 20)
71+
(1, 66)
72+
(2, 88)
73+
(3, 114)
74+
};
75+
\end{axis}
76+
\pgfresetboundingbox\path
77+
(current axis.south west) -- ++(-0.44in,-0.09in)
78+
rectangle (current axis.north east) -- ++(0.05in,0.05in);
79+
\end{tikzpicture}
80+
\end{document}

performance.png

80.4 KB
Loading

src/buffer/mod.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
use crate::raw;
2-
#[cfg(not(maybe_uninit))]
3-
use core::mem;
4-
#[cfg(maybe_uninit)]
52
use core::mem::MaybeUninit;
63
use core::{slice, str};
74
#[cfg(feature = "no-panic")]
@@ -21,10 +18,7 @@ const NEG_INFINITY: &str = "-Infinity";
2118
/// assert_eq!(printed, "1.234");
2219
/// ```
2320
pub struct Buffer {
24-
#[cfg(maybe_uninit)]
2521
bytes: [MaybeUninit<u8>; 25],
26-
#[cfg(not(maybe_uninit))]
27-
bytes: [u8; 25],
2822
}
2923

3024
impl Buffer {
@@ -33,12 +27,7 @@ impl Buffer {
3327
#[inline]
3428
#[cfg_attr(feature = "no-panic", no_panic)]
3529
pub fn new() -> Self {
36-
// assume_init is safe here, since this is an array of MaybeUninit, which does not need
37-
// to be initialized.
38-
#[cfg(maybe_uninit)]
3930
let bytes = [MaybeUninit::<u8>::uninit(); 25];
40-
#[cfg(not(maybe_uninit))]
41-
let bytes = unsafe { core::mem::uninitialized() };
4231

4332
Buffer { bytes }
4433
}

0 commit comments

Comments
 (0)