Skip to content

Commit

Permalink
Sync to dtolnay/ryu master (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Dec 16, 2021
1 parent 85fecf5 commit 7b91c0e
Show file tree
Hide file tree
Showing 22 changed files with 266 additions and 299 deletions.
2 changes: 1 addition & 1 deletion .clippy.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
msrv = "1.31.0"
msrv = "1.36.0"
19 changes: 16 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ jobs:
with:
toolchain: ${{matrix.rust}}
- run: cargo test
- run: cargo test --features small
- run: cargo build --tests --features no-panic --release
if: matrix.rust == 'nightly'

msrv:
name: Rust 1.31.0
name: Rust 1.36.0
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: dtolnay/rust-toolchain@1.31.0
- uses: dtolnay/rust-toolchain@1.36.0
- run: cargo build
- run: cargo build --features small

Expand All @@ -40,11 +41,23 @@ jobs:
with:
components: miri
- run: cargo miri test
env:
MIRIFLAGS: "-Zmiri-tag-raw-pointers"

clippy:
name: Clippy
runs-on: ubuntu-latest
if: github.event_name != 'pull_request'
steps:
- uses: actions/checkout@v2
- uses: dtolnay/rust-toolchain@clippy
- run: cargo clippy -- -Dclippy::all -Dclippy::pedantic
- run: cargo clippy --tests --benches -- -Dclippy::all -Dclippy::pedantic

outdated:
name: Outdated
runs-on: ubuntu-latest
if: github.event_name != 'pull_request'
steps:
- uses: actions/checkout@v2
- uses: dtolnay/install@cargo-outdated
- run: cargo outdated --exit-code 1
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ license = "Apache-2.0 OR BSL-1.0"
description = "Fast floating point to string conversion, ECMAScript compliant."
repository = "https://github.com/boa-dev/ryu-js"
documentation = "https://docs.rs/ryu-js"
categories = ["value-formatting"]
readme = "README.md"
exclude = ["performance.png", "chart/**"]
edition = "2018"
rust-version = "1.36"

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

[dev-dependencies]
num_cpus = "1.8"
rand = "0.7"
rand_xorshift = "0.2"
rand = "0.8"
rand_xorshift = "0.3"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
38 changes: 25 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ under the creative commons CC-BY-SA license.
This Rust implementation is a line-by-line port of Ulf Adams' implementation in
C, [https://github.com/ulfjack/ryu][upstream].

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

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

<br>

## Example

```rust
Expand All @@ -38,7 +40,27 @@ fn main() {
}
```

## Performance
<br>

## Performance (lower is better)

![performance](https://raw.githubusercontent.com/boa-dev/ryu-js/master/performance.png)

You can run upstream's benchmarks with:

```console
$ git clone https://github.com/ulfjack/ryu c-ryu
$ cd c-ryu
$ bazel run -c opt //ryu/benchmark:ryu_benchmark --
```

And the same benchmark against our implementation with:

```console
$ git clone https://github.com/boa-dev/ryu-js rust-ryu
$ cd rust-ryu
$ cargo run --example upstream_benchmark --release
```

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

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

| type=f32 | 0.0 | 0.1234 | 2.718281828459045 | f32::MAX |
|:--------:|:----:|:------:|:-----------------:|:--------:|
| RYU | 3ns | 28ns | 23ns | 22ns |
| STD | 40ns | 106ns | 128ns | 110ns |

| type=f64 | 0.0 | 0.1234 | 2.718281828459045 | f64::MAX |
|:--------:|:----:|:------:|:-----------------:|:--------:|
| RYU | 3ns | 50ns | 35ns | 32ns |
| STD | 39ns | 105ns | 128ns | 202ns |

## Formatting

This library tends to produce more human-readable output than the standard
Expand Down
5 changes: 5 additions & 0 deletions benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// cargo bench

#![feature(test)]
#![allow(
clippy::approx_constant,
clippy::excessive_precision,
clippy::unreadable_literal
)]

extern crate test;

Expand Down
40 changes: 0 additions & 40 deletions build.rs

This file was deleted.

7 changes: 7 additions & 0 deletions chart/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*.aux
*.fdb_latexmk
*.fls
*.log
*.pdf
*.png
*.svg
80 changes: 80 additions & 0 deletions chart/performance.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{sansmath}
\pgfplotsset{compat=1.16}
\definecolor{ryu}{HTML}{3366FF}
\definecolor{std}{HTML}{949494}
\definecolor{bg}{HTML}{CFCFCF}
\begin{document}
\pagecolor{white}
\begin{tikzpicture}
\edef\entries{
"$0.0$",
"$0.1234$",
"$2.718281828459045$",
"$1.7976931348623157e308$",
}
\begin{axis}[
width=5in,
height=3.5in,
ybar,
ymin=0,
bar width=24pt,
enlarge x limits={abs=39pt},
ylabel={nanos for one call to write},
legend style={
anchor=north west,
at={(0.025,0.975)},
legend columns=1,
draw=none,
fill=none,
},
legend entries={
ryu::Buffer::new().format\_finite(value)\\
std::write!(\&mut buf, ``\{\}'', value)\\
},
legend cell align=left,
xtick={-0.5,0.5,1.5,2.5,3.5,4.5},
xticklabels={},
xtick pos=left,
visualization depends on={y \as \rawy},
every node near coord/.append style={
shift={(axis direction cs:0,-\rawy/2)},
rotate=90,
anchor=center,
font=\sansmath\sffamily,
},
axis background/.style={fill=bg},
tick label style={font=\sansmath\sffamily},
every axis label={font=\sansmath\sffamily},
legend style={font=\sansmath\sffamily},
label style={font=\sansmath\sffamily},
]
\addplot[
black,
fill=ryu,
area legend,
nodes near coords={},
] coordinates {
(0, 3)
(1, 40)
(2, 27)
(3, 28)
};
\addplot[
black,
fill=std,
area legend,
nodes near coords=\pgfmathsetmacro{\input}{{\entries}[\coordindex]}\input,
] coordinates {
(0, 20)
(1, 66)
(2, 88)
(3, 114)
};
\end{axis}
\pgfresetboundingbox\path
(current axis.south west) -- ++(-0.44in,-0.09in)
rectangle (current axis.north east) -- ++(0.05in,0.05in);
\end{tikzpicture}
\end{document}
Binary file added performance.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 0 additions & 11 deletions src/buffer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::raw;
#[cfg(not(maybe_uninit))]
use core::mem;
#[cfg(maybe_uninit)]
use core::mem::MaybeUninit;
use core::{slice, str};
#[cfg(feature = "no-panic")]
Expand All @@ -21,10 +18,7 @@ const NEG_INFINITY: &str = "-Infinity";
/// assert_eq!(printed, "1.234");
/// ```
pub struct Buffer {
#[cfg(maybe_uninit)]
bytes: [MaybeUninit<u8>; 25],
#[cfg(not(maybe_uninit))]
bytes: [u8; 25],
}

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

Buffer { bytes }
}
Expand Down
Loading

0 comments on commit 7b91c0e

Please sign in to comment.