Skip to content

Commit

Permalink
Introduce rustversion and use round_ties_even()
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Dec 9, 2024
1 parent 24f445d commit 5c0ba9a
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 64 deletions.
3 changes: 2 additions & 1 deletion .config/topic.dic
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
25
26
1G
1M
1ns
Expand All @@ -13,6 +13,7 @@ io
JS
MDN
MSRV
MSRVs
representable
Serde
Serde's
Expand Down
3 changes: 1 addition & 2 deletions .github/upstream-sources.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[
{ "owner": "rust-lang", "repo": "libm", "tag": "libm-v0.2.11", "path": "src/math/trunc.rs" },
{ "owner": "rust-lang", "repo": "libm", "tag": "libm-v0.2.11", "path": "src/math/copysign.rs" },
{ "owner": "rust-lang", "repo": "libm", "tag": "libm-v0.2.11", "path": "src/math/round.rs" },
{ "owner": "rust-lang", "repo": "libm", "tag": "libm-v0.2.11", "path": "src/math/rint.rs" },
{ "owner": "rust-lang", "repo": "rust", "tag": "1.83.0", "path": "library/core/src/time.rs" },
{ "owner": "rust-lang", "repo": "rust", "tag": "1.83.0", "path": "library/std/src/time.rs" },
{
Expand Down
45 changes: 39 additions & 6 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,25 @@ jobs:
build-std: true,
}
features:
- { features: "", no_std: false }
- { features: --features serde, no_std: false, description: (`serde`) }
- { features: "", description: (`default`) }
- { features: --features serde, description: "(`default`, `serde`)" }
- { features: --no-default-features --features std, description: (`std`) }
- { features: --no-default-features, no_std: true, description: (`no_std`) }
- {
features: --no-default-features --features msrv,
no_std: true,
description: "(`no_std`, `msrv`)",
}
- {
features: --no-default-features --features serde,
no_std: true,
description: "(`no_std`, `serde`)",
}
- {
features: "--no-default-features --features msrv,serde",
no_std: true,
description: "(`no_std`, `msrv`, `serde`)",
}
exclude:
- target: { target: x86_64-unknown-linux-gnu, description: Native }
rust: { version: nightly }
Expand Down Expand Up @@ -109,14 +120,25 @@ jobs:
- { version: stable }
- { version: nightly }
features:
- { features: "", no_std: false }
- { features: --features serde, no_std: false, description: (`serde`) }
- { features: "", description: (`default`) }
- { features: --features serde, description: "(`default`, `serde`)" }
- { features: --no-default-features --features std, description: (`std`) }
- { features: --no-default-features, no_std: true, description: (`no_std`) }
- {
features: --no-default-features --features msrv,
no_std: true,
description: "(`no_std`, `msrv`)",
}
- {
features: --no-default-features --features serde,
no_std: true,
description: "(`no_std`, `serde`)",
}
- {
features: "--no-default-features --features msrv,serde",
no_std: true,
description: "(`no_std`, `msrv`, `serde`)",
}
exclude:
- target: { target: x86_64-unknown-linux-gnu, description: Native }
rust: { version: nightly }
Expand Down Expand Up @@ -169,14 +191,25 @@ jobs:
- { version: stable }
- { version: nightly }
features:
- { features: "", no_std: false }
- { features: --features serde, no_std: false, description: (`serde`) }
- { features: "", description: (`default`) }
- { features: --features serde, description: "(`default`, `serde`)" }
- { features: --no-default-features --features std, description: (`std`) }
- { features: --no-default-features, no_std: true, description: (`no_std`) }
- {
features: --no-default-features --features msrv,
no_std: true,
description: "(`no_std`, `msrv`)",
}
- {
features: --no-default-features --features serde,
no_std: true,
description: "(`no_std`, `serde`)",
}
- {
features: "--no-default-features --features msrv,serde",
no_std: true,
description: "(`no_std`, `msrv`, `serde`)",
}
exclude:
- target: { target: x86_64-unknown-linux-gnu, description: Native }
rust: { version: nightly }
Expand Down
35 changes: 12 additions & 23 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ jobs:
target:
- { target: x86_64-unknown-linux-gnu, description: Native }
- { target: wasm32-unknown-unknown, description: Web }
- { target: wasm32v1-none, description: Wasm v1 }
rust:
- { version: stable, atomics: false }
- { version: nightly, atomics: false }
- {
version: nightly,
description: with Atomics,
Expand All @@ -39,8 +37,9 @@ jobs:
build-std: true,
}
features:
- { features: "", no_std: false }
- { features: --features serde, no_std: false, description: (`serde`) }
- { features: "", description: (`default`) }
- { features: --features serde, description: (`default` `serde`) }
- { features: --no-default-features --features std, no_msrv: true, description: (`std`) }
- { features: --no-default-features, no_std: true, description: (`no_std`) }
- {
features: --no-default-features --features serde,
Expand All @@ -49,13 +48,9 @@ jobs:
}
exclude:
- target: { target: x86_64-unknown-linux-gnu, description: Native }
rust: { version: nightly }
- target: { target: wasm32-unknown-unknown, description: Web }
rust: { version: nightly, atomics: false }
- target: { target: wasm32v1-none, description: Wasm v1 }
rust: { version: stable }
- target: { target: wasm32v1-none, description: Wasm v1 }
features: { no_std: false }
rust: { atomics: true }
- target: { target: x86_64-unknown-linux-gnu, description: Native }
features: { no_msrv: true }

steps:
- name: Checkout
Expand Down Expand Up @@ -90,7 +85,6 @@ jobs:
target:
- { target: x86_64-unknown-linux-gnu, description: Native }
- { target: wasm32-unknown-unknown, description: Web }
- { target: wasm32v1-none, description: Wasm v1 }
rust:
- { version: stable, atomics: false }
- { version: nightly, atomics: false }
Expand All @@ -103,8 +97,9 @@ jobs:
build-std: true,
}
features:
- { features: "", no_std: false }
- { features: --features serde, no_std: false, description: (`serde`) }
- { features: "", description: (`default`) }
- { features: --features serde, description: (`default` `serde`) }
- { features: --no-default-features --features std, no_msrv: true, description: (`std`) }
- { features: --no-default-features, no_std: true, description: (`no_std`) }
- {
features: --no-default-features --features serde,
Expand All @@ -115,20 +110,14 @@ jobs:
- { flags: "", nightly: false }
- { description: (docs.rs), flags: --cfg=docsrs, nightly: true }
exclude:
- docsrs: { flags: "", nightly: false }
rust: { version: nightly }
- docsrs: { flags: --cfg=docsrs, nightly: true }
rust: { version: stable }
- target: { target: x86_64-unknown-linux-gnu, description: Native }
rust: { atomics: true }
- target: { target: x86_64-unknown-linux-gnu, description: Native }
docsrs: { flags: "", nightly: false }
rust: { version: nightly }
- target: { target: wasm32-unknown-unknown, description: Web }
docsrs: { flags: "", nightly: false }
rust: { version: nightly, atomics: false }
- target: { target: wasm32v1-none, description: Wasm v1 }
rust: { version: stable }
- target: { target: wasm32v1-none, description: Wasm v1 }
features: { no_std: false }
features: { no_msrv: true }

steps:
- name: Checkout
Expand Down
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- [`no_std`] support through a `std` crate feature.
- Support for the [`wasm32v1-none`] target.
- MSRV policy.
- A `msrv` crate feature that allows `web-time` to make use of features only available in higher
MSRVs. This offers compile-time detection and does not break compilation when enabled with the
crates MSRV.

When used in conjunction with the `std` crate feature and compiling with Rust v1.77 or above, it
enables the use of the [`f64.nearest`] instruction. Which will significantly reduce the
instruction count for `Instant::now()`.

### Changed

- Improve performance of `Instant::now()` by using `f64::round_ties_even()` instead of
`f64::round()` internally.

### Fixed

Expand All @@ -20,6 +32,8 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

[`no_std`]: https://doc.rust-lang.org/1.82.0/reference/names/preludes.html#the-no_std-attribute
[`wasm32v1-none`]: https://doc.rust-lang.org/nightly/rustc/platform-support/wasm32v1-none.html
[`f64.nearest`]:
https://webassembly.github.io/spec/core/syntax/instructions.html#syntax-instr-numeric

## [1.1.0] - 2024-03-01

Expand Down
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ rust-version = "1.60"
version = "1.1.0"

[features]
default = ["std"]
default = ["std", "msrv"]
msrv = ["dep:rustversion"]
serde = ["dep:serde"]
std = ["wasm-bindgen-test/std", "getrandom/std", "rand/std", "tests-native/std", "tests-web/std"]

[target.'cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))'.dependencies]
js-sys = { version = "0.3.75", default-features = false }
rustversion = { version = "1.0.0", optional = true }
serde = { version = "1.0.0", optional = true, default-features = false }
wasm-bindgen = { version = "0.2.98", default-features = false }

Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ instruction output.
Without this crate feature compilation the standard library is not included. Has no effect on
targets other then `wasm32-unknown-unknown` or `wasm32v1-none`.

### `msrv` (enabled by default)

Allows `web-time` to make use of features only available in higher MSRVs. This offers compile-time
detection and does not break compilation when enabled with the crates MSRV.

When used in conjunction with the `std` crate feature and compiling with Rust v1.77 or above, it
enables the use of the [`f64.nearest`] instruction. Which will significantly reduce the instruction
count for [`Instant::now()`].

### `serde`

Implements [`serde::Deserialize`] and [`serde::Serialize`] for [`SystemTime`].
Expand Down Expand Up @@ -140,3 +149,5 @@ additional terms or conditions.
[`serde::Deserialize`]: https://docs.rs/serde/1/serde/trait.Deserialize.html
[`serde::Serialize`]: https://docs.rs/serde/1/serde/trait.Serialize.html
[`wasm-bindgen`]: https://crates.io/crates/wasm-bindgen
[`f64.nearest`]:
https://webassembly.github.io/spec/core/syntax/instructions.html#syntax-instr-numeric
12 changes: 6 additions & 6 deletions benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub fn main() {
});
benchmark("`Duration::from_millis()` with rounding", |time_stamp| {
Duration::from_millis(F64(time_stamp).trunc() as u64)
+ Duration::from_nanos(F64(F64(time_stamp).fract() * 1.0e6).round() as u64)
+ Duration::from_nanos(F64(F64(time_stamp).fract() * 1.0e6).round_ties_even() as u64)
});
benchmark("`Duration::from_secs_f64()`", |time_stamp| {
Duration::from_secs_f64(time_stamp) / 1000
Expand Down Expand Up @@ -155,20 +155,20 @@ impl F64 {
self.0 - self.trunc()
}

/// See [`f64::round()`].
/// See [`f64::round_ties_even()`].
#[cfg(feature = "std")]
#[expect(
clippy::disallowed_methods,
reason = "this is where the abstraction happens"
)]
#[cfg(feature = "std")]
fn round(self) -> f64 {
self.0.round()
fn round_ties_even(self) -> f64 {
self.0.round_ties_even()
}

#[cfg(not(feature = "std"))]
fn round(self) -> f64 {
libm::round(self.0)
fn round_ties_even(self) -> f64 {
libm::rint(self.0)
}
}

Expand Down
2 changes: 1 addition & 1 deletion clippy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ disallowed-methods = [
{ path = "f64::trunc", reason = "not available on `no_std`" },
{ path = "f64::fract", reason = "not available on `no_std`" },
{ path = "f64::copysign", reason = "not available on `no_std`" },
{ path = "f64::round", reason = "not available on `no_std`" },
{ path = "f64::round_ties_even", reason = "not available on `no_std` and MSRV" },
{ path = "f64::powi", reason = "not available on `no_std`" },
]
semicolon-outside-block-ignore-multiline = true
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@
//! Has no effect on targets other then `wasm32-unknown-unknown` or
//! `wasm32v1-none`.
//!
//! ## `msrv` (enabled by default)
//!
//! Allows `web-time` to make use of features only available in higher MSRVs.
//! This offers compile-time detection and does not break compilation when
//! enabled with the crates MSRV.
//!
//! When used in conjunction with the `std` crate feature and compiling with
//! Rust v1.77 or above, it enables the use of the [`f64.nearest`] instruction.
//! Which will significantly reduce the instruction
//! count for [`Instant::now()`].
//!
//! ## `serde`
//!
//! Implements [`serde::Deserialize`] and [`serde::Serialize`] for
Expand Down Expand Up @@ -148,6 +159,7 @@
doc = "[`serde::Serialize`]: https://docs.rs/serde/1/serde/trait.Serialize.html"
)]
//! [`wasm-bindgen`]: https://crates.io/crates/wasm-bindgen
//! [`f64.nearest`]: https://webassembly.github.io/spec/core/syntax/instructions.html#syntax-instr-numeric
#![cfg_attr(all(target_arch = "wasm32", not(feature = "std")), no_std)]
#![cfg_attr(all(test, target_arch = "wasm32"), no_main)]
Expand Down
Loading

0 comments on commit 5c0ba9a

Please sign in to comment.