-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
read: cache abbreviations at offset 0
This optimises for the case where there is a single set of abbreviations that is used for all units.
- Loading branch information
Showing
8 changed files
with
231 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
pub(crate) use imp::*; | ||
|
||
#[cfg(not(feature = "std"))] | ||
mod imp { | ||
use alloc::sync::Arc; | ||
use core::sync::atomic::{AtomicPtr, Ordering}; | ||
use core::{mem, ptr}; | ||
|
||
#[derive(Debug, Default)] | ||
pub(crate) struct LazyArc<T> { | ||
// Only written once with a value obtained from `Arc<T>::into_raw`. | ||
value: AtomicPtr<T>, | ||
} | ||
|
||
impl<T> Drop for LazyArc<T> { | ||
fn drop(&mut self) { | ||
let value_ptr = self.value.load(Ordering::Acquire); | ||
if !value_ptr.is_null() { | ||
// SAFETY: all writes to `self.value` are pointers obtained from `Arc::into_raw`. | ||
unsafe { | ||
Arc::from_raw(value_ptr); | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<T> LazyArc<T> { | ||
pub(crate) fn get<E, F: FnOnce() -> Result<T, E>>(&self, f: F) -> Result<Arc<T>, E> { | ||
// Clone an `Arc` given a pointer obtained from `Arc::into_raw`. | ||
// SAFETY: `value_ptr` must be a valid pointer obtained from `Arc<T>::into_raw`. | ||
unsafe fn clone_arc_ptr<T>(value_ptr: *const T) -> Arc<T> { | ||
let value = Arc::from_raw(value_ptr); | ||
let clone = Arc::clone(&value); | ||
mem::forget(value); | ||
clone | ||
} | ||
|
||
// Return the existing value if already computed. | ||
let value_ptr = self.value.load(Ordering::Acquire); | ||
if !value_ptr.is_null() { | ||
// SAFETY: all writes to `self.value` are pointers obtained from `Arc::into_raw`. | ||
return Ok(unsafe { clone_arc_ptr(value_ptr) }); | ||
} | ||
|
||
// Race to compute and set the value. | ||
let value = f().map(Arc::new)?; | ||
let value_ptr = Arc::into_raw(value); | ||
match self.value.compare_exchange( | ||
ptr::null_mut(), | ||
value_ptr as *mut T, | ||
Ordering::Release, | ||
Ordering::Relaxed, | ||
) { | ||
Ok(_) => { | ||
// Return the value we computed. | ||
// SAFETY: `value_ptr` was obtained from `Arc::into_raw`. | ||
Ok(unsafe { clone_arc_ptr(value_ptr) }) | ||
} | ||
Err(existing_value_ptr) => { | ||
// We lost the race, drop unneeded `value_ptr`. | ||
// SAFETY: `value_ptr` was obtained from `Arc::into_raw`. | ||
unsafe { Arc::from_raw(value_ptr) }; | ||
// Return the existing value. | ||
// SAFETY: all writes to `self.value` are pointers obtained from `Arc::into_raw`. | ||
Ok(unsafe { clone_arc_ptr(existing_value_ptr) }) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
mod imp { | ||
use std::sync::{Arc, Mutex}; | ||
|
||
#[derive(Debug, Default)] | ||
pub(crate) struct LazyArc<T> { | ||
value: Mutex<Option<Arc<T>>>, | ||
} | ||
|
||
impl<T> LazyArc<T> { | ||
pub(crate) fn get<E, F: FnOnce() -> Result<T, E>>(&self, f: F) -> Result<Arc<T>, E> { | ||
let mut lock = self.value.lock().unwrap(); | ||
if let Some(value) = &*lock { | ||
return Ok(value.clone()); | ||
} | ||
let value = f().map(Arc::new)?; | ||
*lock = Some(value.clone()); | ||
Ok(value) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn lazy_arc() { | ||
let lazy = LazyArc::default(); | ||
let value = lazy.get(|| Err(())); | ||
assert_eq!(value, Err(())); | ||
let value = lazy.get(|| Ok::<i32, ()>(3)).unwrap(); | ||
assert_eq!(*value, 3); | ||
let value = lazy.get(|| Err(())).unwrap(); | ||
assert_eq!(*value, 3); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.