From 8df95b9293bf27cec9fad87bde5123488363978d Mon Sep 17 00:00:00 2001 From: daxpedda Date: Sun, 24 Dec 2023 19:13:24 +0100 Subject: [PATCH] Improve performance of `SystemTime` by using `Duration` internally --- CHANGELOG.md | 6 ++++++ src/web/system_time.rs | 29 ++++++++++------------------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9dbf8ce..ef5a78b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Changed + +- Improve performance of `SystemTime` by using `Duration` internally. + ## [0.2.3] - 2023-10-23 ### Changed diff --git a/src/web/system_time.rs b/src/web/system_time.rs index c66f602..cee0760 100644 --- a/src/web/system_time.rs +++ b/src/web/system_time.rs @@ -7,37 +7,30 @@ use std::time::Duration; /// See [`std::time::SystemTime`]. #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] -pub struct SystemTime(i64); +pub struct SystemTime(Duration); impl SystemTime { /// See [`std::time::SystemTime::UNIX_EPOCH`]. - pub const UNIX_EPOCH: Self = Self(0); + pub const UNIX_EPOCH: Self = Self(Duration::ZERO); /// See [`std::time::SystemTime::now()`]. #[must_use] + #[allow(clippy::missing_panics_doc)] pub fn now() -> Self { #[allow(clippy::as_conversions, clippy::cast_possible_truncation)] - Self(js_sys::Date::now() as i64) + let ms = js_sys::Date::now() as i64; + let ms = ms.try_into().expect("found negative timestamp"); + + Self(Duration::from_millis(ms)) } /// See [`std::time::SystemTime::duration_since()`]. - #[allow( - clippy::missing_errors_doc, - clippy::missing_panics_doc, - clippy::trivially_copy_pass_by_ref - )] + #[allow(clippy::missing_errors_doc, clippy::trivially_copy_pass_by_ref)] pub fn duration_since(&self, earlier: Self) -> Result { if self.0 < earlier.0 { - let duration = (earlier.0 - self.0) - .try_into() - .expect("`self` bigger then `earlier` despite earlier check"); - - Err(SystemTimeError(Duration::from_millis(duration))) + Err(SystemTimeError(earlier.0 - self.0)) } else { - let duration = (self.0 - earlier.0) - .try_into() - .expect("`earlier` bigger then `self` despite earlier check"); - Ok(Duration::from_millis(duration)) + Ok(self.0 - earlier.0) } } @@ -50,14 +43,12 @@ impl SystemTime { /// See [`std::time::SystemTime::checked_add()`]. #[allow(clippy::trivially_copy_pass_by_ref)] pub fn checked_add(&self, duration: Duration) -> Option { - let duration = duration.as_millis().try_into().ok()?; self.0.checked_add(duration).map(SystemTime) } /// See [`std::time::SystemTime::checked_sub()`]. #[allow(clippy::trivially_copy_pass_by_ref)] pub fn checked_sub(&self, duration: Duration) -> Option { - let duration = duration.as_millis().try_into().ok()?; self.0.checked_sub(duration).map(SystemTime) } }