Skip to content

Commit

Permalink
Improve performance of SystemTime by using Duration internally
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Dec 24, 2023
1 parent 77f6203 commit 8df95b9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 10 additions & 19 deletions src/web/system_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Duration, SystemTimeError> {
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)
}
}

Expand All @@ -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<Self> {
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<Self> {
let duration = duration.as_millis().try_into().ok()?;
self.0.checked_sub(duration).map(SystemTime)
}
}
Expand Down

0 comments on commit 8df95b9

Please sign in to comment.