Skip to content

Commit

Permalink
Merge pull request #63 from vorner/const-arc-swap-option
Browse files Browse the repository at this point in the history
Const ArcSwapOption initialization
  • Loading branch information
vorner authored Sep 17, 2021
2 parents a87221b + 3043a39 commit 63bda3a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.4.0

* Allow const-initializing ArcSwapOption (`const_empty` method).

# 1.3.2

* More helpful description of the `AsRaw` trait (isn't implemented for owned
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "arc-swap"
version = "1.3.2"
version = "1.4.0"
authors = ["Michal 'vorner' Vaner <[email protected]>"]
description = "Atomically swappable Arc"
documentation = "https://docs.rs/arc-swap"
Expand Down
32 changes: 32 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ use crate::access::{Access, Map};
pub use crate::as_raw::AsRaw;
pub use crate::cache::Cache;
pub use crate::ref_cnt::RefCnt;
use crate::strategy::hybrid::{DefaultConfig, HybridStrategy};
use crate::strategy::sealed::Protected;
use crate::strategy::{CaS, Strategy};
pub use crate::strategy::{DefaultStrategy, IndependentStrategy};
Expand Down Expand Up @@ -737,6 +738,37 @@ impl<T, S: Strategy<Option<Arc<T>>>> ArcSwapAny<Option<Arc<T>>, S> {
}
}

impl<T> ArcSwapOption<T> {
/// A const-fn equilavent of [empty].
///
/// Just like [empty], this creates an `None`-holding `ArcSwapOption`. The [empty] is, however,
/// more general ‒ this is available only for the default strategy, while [empty] is for any
/// [Default]-constructible strategy (current or future one).
///
/// [empty]: ArcSwapAny::empty
///
/// # Examples
///
/// ```rust
/// # use std::sync::Arc;
/// # use arc_swap::ArcSwapOption;
/// static GLOBAL_DATA: ArcSwapOption<usize> = ArcSwapOption::const_empty();
///
/// assert!(GLOBAL_DATA.load().is_none());
/// GLOBAL_DATA.store(Some(Arc::new(42)));
/// assert_eq!(42, **GLOBAL_DATA.load().as_ref().unwrap());
/// ```
pub const fn const_empty() -> Self {
Self {
ptr: AtomicPtr::new(ptr::null_mut()),
_phantom_arc: PhantomData,
strategy: HybridStrategy {
_config: DefaultConfig,
},
}
}
}

/// An atomic storage that doesn't share the internal generation locks with others.
///
/// This makes it bigger and it also might suffer contention (on the HW level) if used from many
Expand Down
2 changes: 1 addition & 1 deletion src/strategy/hybrid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl Config for DefaultConfig {

#[derive(Clone, Default)]
pub struct HybridStrategy<Cfg> {
_config: Cfg,
pub(crate) _config: Cfg,
}

impl<T, Cfg> InnerStrategy<T> for HybridStrategy<Cfg>
Expand Down
2 changes: 1 addition & 1 deletion src/strategy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use std::sync::atomic::AtomicPtr;

use crate::ref_cnt::RefCnt;

mod hybrid;
pub(crate) mod hybrid;
mod rw_lock;
// Do not use from outside of the crate.
#[cfg(feature = "internal-test-strategies")]
Expand Down

2 comments on commit 63bda3a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Track benchmarks

Benchmark suite Current: 63bda3a Previous: 3043a39 Ratio
uncontended/load 21 ns/iter (± 0) 22 ns/iter (± 0) 0.95
uncontended/load_full 30 ns/iter (± 0) 35 ns/iter (± 1) 0.86
uncontended/load_many 46 ns/iter (± 0) 48 ns/iter (± 1) 0.96
uncontended/store 147 ns/iter (± 0) 145 ns/iter (± 5) 1.01
uncontended/cache 1 ns/iter (± 0) 0 ns/iter (± 0) Infinity
concurrent_loads/load 32 ns/iter (± 10) 28 ns/iter (± 10) 1.14
concurrent_loads/load_full 37 ns/iter (± 17) 49 ns/iter (± 16) 0.76
concurrent_loads/load_many 65 ns/iter (± 28) 72 ns/iter (± 23) 0.90
concurrent_loads/store 997 ns/iter (± 719) 1050 ns/iter (± 491) 0.95
concurrent_loads/cache 1 ns/iter (± 0) 0 ns/iter (± 0) Infinity
concurrent_store/load 107 ns/iter (± 6) 79 ns/iter (± 6) 1.35
concurrent_store/load_full 135 ns/iter (± 4) 116 ns/iter (± 36) 1.16
concurrent_store/load_many 165 ns/iter (± 8) 150 ns/iter (± 15) 1.10
concurrent_store/store 1196 ns/iter (± 31) 1031 ns/iter (± 65) 1.16
concurrent_store/cache 1 ns/iter (± 0) 1 ns/iter (± 0) 1

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Track benchmarks'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 63bda3a Previous: 3043a39 Ratio
uncontended/cache 1 ns/iter (± 0) 0 ns/iter (± 0) Infinity
concurrent_loads/cache 1 ns/iter (± 0) 0 ns/iter (± 0) Infinity

This comment was automatically generated by workflow using github-action-benchmark.

CC: @vorner

Please sign in to comment.