Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce the size of the future returned by async get_with and friend methods (v0.10) #220

Merged
merged 5 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub use {
pub type PredicateId = String;

// Empty struct to be used in InitResult::InitErr to represent the Option None.
struct OptionallyNone;
pub(crate) struct OptionallyNone;

pub struct Iter<'i, K, V>(crate::sync_base::iter::Iter<'i, K, V>);

Expand Down
42 changes: 29 additions & 13 deletions src/future/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use std::{
fmt,
future::Future,
hash::{BuildHasher, Hash},
pin::Pin,
sync::Arc,
time::Duration,
};
Expand Down Expand Up @@ -915,6 +916,7 @@ where
/// `init` futures.
///
pub async fn get_with(&self, key: K, init: impl Future<Output = V>) -> V {
futures_util::pin_mut!(init);
let hash = self.base.hash(&key);
let key = Arc::new(key);
let replace_if = None as Option<fn(&V) -> bool>;
Expand All @@ -931,9 +933,9 @@ where
K: Borrow<Q>,
Q: ToOwned<Owned = K> + Hash + Eq + ?Sized,
{
futures_util::pin_mut!(init);
let hash = self.base.hash(key);
let replace_if = None as Option<fn(&V) -> bool>;

self.get_or_insert_with_hash_by_ref_and_fun(key, hash, init, replace_if, false)
.await
.into_value()
Expand All @@ -948,6 +950,7 @@ where
init: impl Future<Output = V>,
replace_if: impl FnMut(&V) -> bool,
) -> V {
futures_util::pin_mut!(init);
let hash = self.base.hash(&key);
let key = Arc::new(key);
self.get_or_insert_with_hash_and_fun(key, hash, init, Some(replace_if), false)
Expand Down Expand Up @@ -1051,6 +1054,7 @@ where
where
F: Future<Output = Option<V>>,
{
futures_util::pin_mut!(init);
let hash = self.base.hash(&key);
let key = Arc::new(key);
self.get_or_optionally_insert_with_hash_and_fun(key, hash, init, false)
Expand All @@ -1068,6 +1072,7 @@ where
K: Borrow<Q>,
Q: ToOwned<Owned = K> + Hash + Eq + ?Sized,
{
futures_util::pin_mut!(init);
let hash = self.base.hash(key);
self.get_or_optionally_insert_with_hash_by_ref_and_fun(key, hash, init, false)
.await
Expand Down Expand Up @@ -1174,6 +1179,7 @@ where
F: Future<Output = Result<V, E>>,
E: Send + Sync + 'static,
{
futures_util::pin_mut!(init);
let hash = self.base.hash(&key);
let key = Arc::new(key);
self.get_or_try_insert_with_hash_and_fun(key, hash, init, false)
Expand All @@ -1191,6 +1197,7 @@ where
K: Borrow<Q>,
Q: ToOwned<Owned = K> + Hash + Eq + ?Sized,
{
futures_util::pin_mut!(init);
let hash = self.base.hash(key);
self.get_or_try_insert_with_hash_by_ref_and_fun(key, hash, init, false)
.await
Expand Down Expand Up @@ -1428,7 +1435,7 @@ where
&self,
key: Arc<K>,
hash: u64,
init: impl Future<Output = V>,
init: Pin<&mut impl Future<Output = V>>,
mut replace_if: Option<impl FnMut(&V) -> bool>,
need_key: bool,
) -> Entry<K, V> {
Expand All @@ -1447,7 +1454,7 @@ where
&self,
key: &Q,
hash: u64,
init: impl Future<Output = V>,
init: Pin<&mut impl Future<Output = V>>,
mut replace_if: Option<impl FnMut(&V) -> bool>,
need_key: bool,
) -> Entry<K, V>
Expand All @@ -1471,7 +1478,7 @@ where
&self,
key: Arc<K>,
hash: u64,
init: impl Future<Output = V>,
init: Pin<&mut impl Future<Output = V>>,
mut replace_if: Option<impl FnMut(&V) -> bool>,
need_key: bool,
) -> Entry<K, V> {
Expand All @@ -1489,9 +1496,12 @@ where
None
};

let type_id = ValueInitializer::<K, V, S>::type_id_for_get_with();
let post_init = ValueInitializer::<K, V, S>::post_init_for_get_with;

match self
.value_initializer
.init_or_read(Arc::clone(&key), get, init, insert)
.try_init_or_read(&Arc::clone(&key), type_id, get, init, insert, post_init)
.await
{
InitResult::Initialized(v) => {
Expand Down Expand Up @@ -1546,7 +1556,7 @@ where
&self,
key: Arc<K>,
hash: u64,
init: F,
init: Pin<&mut F>,
need_key: bool,
) -> Option<Entry<K, V>>
where
Expand All @@ -1565,7 +1575,7 @@ where
&self,
key: &Q,
hash: u64,
init: F,
init: Pin<&mut F>,
need_key: bool,
) -> Option<Entry<K, V>>
where
Expand All @@ -1587,7 +1597,7 @@ where
&self,
key: Arc<K>,
hash: u64,
init: F,
init: Pin<&mut F>,
need_key: bool,
) -> Option<Entry<K, V>>
where
Expand All @@ -1608,9 +1618,12 @@ where
None
};

let type_id = ValueInitializer::<K, V, S>::type_id_for_optionally_get_with();
let post_init = ValueInitializer::<K, V, S>::post_init_for_optionally_get_with;

match self
.value_initializer
.optionally_init_or_read(Arc::clone(&key), get, init, insert)
.try_init_or_read(&Arc::clone(&key), type_id, get, init, insert, post_init)
.await
{
InitResult::Initialized(v) => {
Expand All @@ -1626,7 +1639,7 @@ where
&self,
key: Arc<K>,
hash: u64,
init: F,
init: Pin<&mut F>,
need_key: bool,
) -> Result<Entry<K, V>, Arc<E>>
where
Expand All @@ -1645,7 +1658,7 @@ where
&self,
key: &Q,
hash: u64,
init: F,
init: Pin<&mut F>,
need_key: bool,
) -> Result<Entry<K, V>, Arc<E>>
where
Expand All @@ -1666,7 +1679,7 @@ where
&self,
key: Arc<K>,
hash: u64,
init: F,
init: Pin<&mut F>,
need_key: bool,
) -> Result<Entry<K, V>, Arc<E>>
where
Expand All @@ -1688,9 +1701,12 @@ where
None
};

let type_id = ValueInitializer::<K, V, S>::type_id_for_try_get_with::<E>();
let post_init = ValueInitializer::<K, V, S>::post_init_for_try_get_with;

match self
.value_initializer
.try_init_or_read(Arc::clone(&key), get, init, insert)
.try_init_or_read(&Arc::clone(&key), type_id, get, init, insert, post_init)
.await
{
InitResult::Initialized(v) => {
Expand Down
22 changes: 16 additions & 6 deletions src/future/entry_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ where
///
/// [get-with-method]: ./struct.Cache.html#method.get_with
pub async fn or_insert_with(self, init: impl Future<Output = V>) -> Entry<K, V> {
futures_util::pin_mut!(init);
let key = Arc::new(self.owned_key);
let replace_if = None as Option<fn(&V) -> bool>;
self.cache
Expand All @@ -196,6 +197,7 @@ where
init: impl Future<Output = V>,
replace_if: impl FnMut(&V) -> bool,
) -> Entry<K, V> {
futures_util::pin_mut!(init);
let key = Arc::new(self.owned_key);
self.cache
.get_or_insert_with_hash_and_fun(key, self.hash, init, Some(replace_if), true)
Expand Down Expand Up @@ -269,6 +271,7 @@ where
self,
init: impl Future<Output = Option<V>>,
) -> Option<Entry<K, V>> {
futures_util::pin_mut!(init);
let key = Arc::new(self.owned_key);
self.cache
.get_or_optionally_insert_with_hash_and_fun(key, self.hash, init, true)
Expand Down Expand Up @@ -344,6 +347,7 @@ where
F: Future<Output = Result<V, E>>,
E: Send + Sync + 'static,
{
futures_util::pin_mut!(init);
let key = Arc::new(self.owned_key);
self.cache
.get_or_try_insert_with_hash_and_fun(key, self.hash, init, true)
Expand Down Expand Up @@ -521,11 +525,10 @@ where
///
/// [get-with-method]: ./struct.Cache.html#method.get_with
pub async fn or_insert_with(self, init: impl Future<Output = V>) -> Entry<K, V> {
let owned_key: K = self.ref_key.to_owned();
let key = Arc::new(owned_key);
futures_util::pin_mut!(init);
let replace_if = None as Option<fn(&V) -> bool>;
self.cache
.get_or_insert_with_hash_and_fun(key, self.hash, init, replace_if, true)
.get_or_insert_with_hash_by_ref_and_fun(self.ref_key, self.hash, init, replace_if, true)
.await
}

Expand All @@ -542,10 +545,15 @@ where
init: impl Future<Output = V>,
replace_if: impl FnMut(&V) -> bool,
) -> Entry<K, V> {
let owned_key: K = self.ref_key.to_owned();
let key = Arc::new(owned_key);
futures_util::pin_mut!(init);
self.cache
.get_or_insert_with_hash_and_fun(key, self.hash, init, Some(replace_if), true)
.get_or_insert_with_hash_by_ref_and_fun(
self.ref_key,
self.hash,
init,
Some(replace_if),
true,
)
.await
}

Expand Down Expand Up @@ -615,6 +623,7 @@ where
self,
init: impl Future<Output = Option<V>>,
) -> Option<Entry<K, V>> {
futures_util::pin_mut!(init);
self.cache
.get_or_optionally_insert_with_hash_by_ref_and_fun(self.ref_key, self.hash, init, true)
.await
Expand Down Expand Up @@ -690,6 +699,7 @@ where
F: Future<Output = Result<V, E>>,
E: Send + Sync + 'static,
{
futures_util::pin_mut!(init);
self.cache
.get_or_try_insert_with_hash_by_ref_and_fun(self.ref_key, self.hash, init, true)
.await
Expand Down
Loading