Skip to content

Commit

Permalink
Merge pull request #121 from moka-rs/gh119-quanta
Browse files Browse the repository at this point in the history
Make Quanta crate optional (but enabled by default)
  • Loading branch information
tatsuya6502 authored May 14, 2022
2 parents b2d583e + 5f8f1bd commit 10d9a24
Show file tree
Hide file tree
Showing 15 changed files with 197 additions and 31 deletions.
84 changes: 84 additions & 0 deletions .github/workflows/CIQuantaDisabled.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: CI (Quanta disabled)

on:
push:
paths-ignore:
- '.devcontainer/**'
- '.gitpod.yml'
- '.vscode/**'
pull_request:
paths-ignore:
- '.devcontainer/**'
- '.gitpod.yml'
- '.vscode/**'
schedule:
# Run against the last commit on the default branch on Friday at 8pm (UTC?)
- cron: '0 20 * * 5'

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- stable
- beta
- 1.51.0 # MSRV
- nightly # For checking minimum version dependencies.

steps:
- name: Checkout Moka
uses: actions/checkout@v2

- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
components: rustfmt, clippy

- uses: Swatinem/rust-cache@v1

- name: cargo clean
uses: actions-rs/cargo@v1
with:
command: clean

- name: Downgrade dependencies to minimal versions (Nightly only)
uses: actions-rs/cargo@v1
if: ${{ matrix.rust == 'nightly' }}
with:
command: update
args: -Z minimal-versions

- name: Pin some dependencies to specific versions (MSRV only)
if: ${{ matrix.rust == '1.51.0' }}
# Avoid hashbrown >= v0.12, which requires Rust 2021 edition.
run: |
cargo update -p dashmap --precise 5.2.0
cargo update -p hashbrown --precise 0.11.2
- name: Build (no quanta feature)
uses: actions-rs/cargo@v1
with:
command: build
args: --no-default-features --features atomic64

- name: Run tests (release, no quanta feature)
uses: actions-rs/cargo@v1
with:
command: test
args: --release --no-default-features --features atomic64

- name: Run tests (future feature, but no quanta feature)
uses: actions-rs/cargo@v1
with:
command: test
args: --no-default-features --features 'future, atomic64'

- name: Run tests (dash feature, but no quanta feature)
uses: actions-rs/cargo@v1
with:
command: test
args: --no-default-features --features 'dash, atomic64'
6 changes: 3 additions & 3 deletions .github/workflows/LinuxCrossCompileTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ jobs:
- target: armv7-unknown-linux-musleabihf
# Platforms without AtomicU64 support.
- target: armv5te-unknown-linux-musleabi
cargo-opts: "--no-default-features" # Disable atomic64 feature.
cargo-opts: "--no-default-features" # Disable atomic64 and quanta features.
- target: mips-unknown-linux-musl
cargo-opts: "--no-default-features" # Disable atomic64 feature.
cargo-opts: "--no-default-features" # Disable atomic64 and quanta features.
- target: mipsel-unknown-linux-musl
cargo-opts: "--no-default-features" # Disable atomic64 feature.
cargo-opts: "--no-default-features" # Disable atomic64 and quanta features.

steps:
- name: Checkout Moka
Expand Down
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Moka — Change Log

## Version 0.8.3

### Changed

- Make [Quanta crate][quanta-crate] optional (but enabled by default)
([#121][gh-pull-0121])
- Quanta v0.9.3 or older may not work correctly on some x86_64 machines where
the Time Stamp Counter (TSC) is not synched across the processor cores.
([#119][gh-issue-0119])


## Version 0.8.2

### Added
Expand Down Expand Up @@ -297,16 +308,19 @@ The minimum supported Rust version (MSRV) is now 1.51.0 (2021-03-25).
<!-- Links -->

[caffeine-git]: https://github.com/ben-manes/caffeine
[quanta-crate]: https://crates.io/crates/quanta

[resolving-error-on-32bit]: https://github.com/moka-rs/moka#resolving-compile-errors-on-some-32-bit-platforms

[gh-issue-0119]: https://github.com/moka-rs/moka/issues/119/
[gh-issue-0107]: https://github.com/moka-rs/moka/issues/107/
[gh-issue-0072]: https://github.com/moka-rs/moka/issues/72/
[gh-issue-0059]: https://github.com/moka-rs/moka/issues/59/
[gh-issue-0043]: https://github.com/moka-rs/moka/issues/43/
[gh-issue-0038]: https://github.com/moka-rs/moka/issues/38/
[gh-issue-0031]: https://github.com/moka-rs/moka/issues/31/

[gh-pull-0121]: https://github.com/moka-rs/moka/pull/121/
[gh-pull-0117]: https://github.com/moka-rs/moka/pull/117/
[gh-pull-0116]: https://github.com/moka-rs/moka/pull/116/
[gh-pull-0114]: https://github.com/moka-rs/moka/pull/114/
Expand Down
8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "moka"
version = "0.8.2"
version = "0.8.3"
edition = "2018"
rust-version = "1.51"

Expand All @@ -16,7 +16,7 @@ exclude = [".circleci", ".devcontainer", ".github", ".gitpod.yml", ".vscode"]
build = "build.rs"

[features]
default = ["atomic64"]
default = ["atomic64", "quanta"]

# Enable this feature to use `moka::future::Cache`.
future = ["async-io", "async-lock", "futures-util"]
Expand Down Expand Up @@ -44,7 +44,6 @@ crossbeam-utils = "0.8"
num_cpus = "1.13"
once_cell = "1.7"
parking_lot = "0.12"
quanta = "0.9.3"
scheduled-thread-pool = "0.2.5"
smallvec = "1.8"
tagptr = "0.2"
Expand All @@ -55,6 +54,9 @@ uuid = { version = "0.8", features = ["v4"] }
# https://github.com/Manishearth/triomphe/pull/5
triomphe = { version = "0.1", default-features = false }

# Optional dependencies (quanta, enabled by default)
quanta = { version = "0.9.3", optional = true }

# Optional dependencies (dashmap)
dashmap = { version = "5.2", optional = true }

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ This crate's minimum supported Rust versions (MSRV) are the followings:
|:-----------|:-------------------:|:-----------:|
| no feature | | Rust 1.51.0 |
| `atomic64` | yes | Rust 1.51.0 |
| `quanta` | yes | Rust 1.51.0 |
| `future` | | Rust 1.51.0 |
| `dash` | | Rust 1.51.0 |

Expand Down
11 changes: 1 addition & 10 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,8 @@ pub(crate) mod deque;
pub(crate) mod error;
pub(crate) mod frequency_sketch;
pub(crate) mod thread_pool;
pub(crate) mod unsafe_weak_pointer;

// targe_has_atomic is more convenient but yet unstable (Rust 1.55)
// https://github.com/rust-lang/rust/issues/32976
// #[cfg_attr(target_has_atomic = "64", path = "common/time_atomic64.rs")]

#[cfg_attr(feature = "atomic64", path = "common/atomic_time.rs")]
#[cfg_attr(not(feature = "atomic64"), path = "common/atomic_time_compat.rs")]
pub(crate) mod atomic_time;

pub(crate) mod time;
pub(crate) mod unsafe_weak_pointer;

// Note: `CacheRegion` cannot have more than four enum variants. This is because
// `crate::{sync,unsync}::DeqNodes` uses a `tagptr::TagNonNull<DeqNode<T>, 2>`
Expand Down
33 changes: 26 additions & 7 deletions src/common/time.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
use std::time::Duration;

pub(crate) type Clock = quanta::Clock;
// target_has_atomic is more convenient but yet unstable (Rust 1.55)
// https://github.com/rust-lang/rust/issues/32976
// #[cfg_attr(target_has_atomic = "64", path = "common/time_atomic64.rs")]

#[cfg_attr(
all(feature = "atomic64", feature = "quanta"),
path = "time/atomic_time.rs"
)]
#[cfg_attr(
not(all(feature = "atomic64", feature = "quanta")),
path = "time/atomic_time_compat.rs"
)]
pub(crate) mod atomic_time;

#[cfg_attr(feature = "quanta", path = "time/clock_quanta.rs")]
#[cfg_attr(not(feature = "quanta"), path = "time/clock_compat.rs")]
mod clock;

pub(crate) use clock::Clock;

#[cfg(test)]
pub(crate) type Mock = quanta::Mock;
pub(crate) use clock::Mock;

/// a wrapper type over qunta::Instant to force checked additions and prevent
/// unintentioal overflow. The type preserve the Copy semnatics for the wrapped
/// a wrapper type over Instant to force checked additions and prevent
/// unintentional overflow. The type preserve the Copy semantics for the wrapped
#[derive(PartialEq, PartialOrd, Clone, Copy)]
pub(crate) struct Instant(pub quanta::Instant);
pub(crate) struct Instant(pub clock::Instant);

pub(crate) trait CheckedTimeOps {
fn checked_add(&self, duration: Duration) -> Option<Self>
Expand All @@ -16,12 +35,12 @@ pub(crate) trait CheckedTimeOps {
}

impl Instant {
pub(crate) fn new(instant: quanta::Instant) -> Instant {
pub(crate) fn new(instant: clock::Instant) -> Instant {
Instant(instant)
}

pub(crate) fn now() -> Instant {
Instant(quanta::Instant::now())
Instant(clock::Instant::now())
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/common/atomic_time.rs → src/common/time/atomic_time.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::atomic::{AtomicU64, Ordering};
use super::Instant;

use super::time::Instant;
use std::sync::atomic::{AtomicU64, Ordering};

pub(crate) struct AtomicInstant {
instant: AtomicU64,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::time::Instant;
use super::Instant;

use parking_lot::RwLock;

pub(crate) struct AtomicInstant {
Expand Down
50 changes: 50 additions & 0 deletions src/common/time/clock_compat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::{sync::Arc, time::Instant as StdInstant};

#[cfg(test)]
use std::time::Duration;

use parking_lot::RwLock;

pub(crate) type Instant = StdInstant;

pub(crate) struct Clock {
mock: Option<Arc<Mock>>,
}

impl Clock {
#[cfg(test)]
pub(crate) fn mock() -> (Clock, Arc<Mock>) {
let mock = Arc::new(Mock::default());
let clock = Clock {
mock: Some(Arc::clone(&mock)),
};
(clock, mock)
}

pub(crate) fn now(&self) -> Instant {
if let Some(mock) = &self.mock {
*mock.now.read()
} else {
StdInstant::now()
}
}
}

pub(crate) struct Mock {
now: RwLock<Instant>,
}

impl Default for Mock {
fn default() -> Self {
Self {
now: RwLock::new(StdInstant::now()),
}
}
}

#[cfg(test)]
impl Mock {
pub(crate) fn increment(&self, amount: Duration) {
*self.now.write() += amount;
}
}
5 changes: 5 additions & 0 deletions src/common/time/clock_quanta.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub(crate) type Clock = quanta::Clock;
pub(crate) type Instant = quanta::Instant;

#[cfg(test)]
pub(crate) type Mock = quanta::Mock;
3 changes: 1 addition & 2 deletions src/dash/base_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use super::{iter::DashMapIter, Iter};
use crate::{
common::{
self,
atomic_time::AtomicInstant,
deque::{DeqNode, Deque},
frequency_sketch::FrequencySketch,
time::{CheckedTimeOps, Clock, Instant},
time::{atomic_time::AtomicInstant, CheckedTimeOps, Clock, Instant},
CacheRegion,
},
sync::{
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
//! |:-----------|:-------------------:|:-----------:|
//! | no feature | | Rust 1.51.0 |
//! | `atomic64` | yes | Rust 1.51.0 |
//! | `quanta` | yes | Rust 1.51.0 |
//! | `future` | | Rust 1.51.0 |
//! | `dash` | | Rust 1.51.0 |
//!
Expand Down
3 changes: 1 addition & 2 deletions src/sync/base_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ use super::debug_counters::CacheDebugStats;
use crate::{
common::{
self,
atomic_time::AtomicInstant,
deque::{DeqNode, Deque},
frequency_sketch::FrequencySketch,
time::{CheckedTimeOps, Clock, Instant},
time::{atomic_time::AtomicInstant, CheckedTimeOps, Clock, Instant},
CacheRegion,
},
Policy, PredicateError,
Expand Down
2 changes: 1 addition & 1 deletion src/sync/entry_info.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};

use super::AccessTime;
use crate::common::{atomic_time::AtomicInstant, time::Instant};
use crate::common::time::{atomic_time::AtomicInstant, Instant};

pub(crate) struct EntryInfo {
is_admitted: AtomicBool,
Expand Down

0 comments on commit 10d9a24

Please sign in to comment.