-
Notifications
You must be signed in to change notification settings - Fork 253
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
Use a custom container for Cache's cache #689
Changes from 2 commits
63b7d26
3afccb1
b7fbd04
c88b038
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use core::mem::{self, MaybeUninit}; | ||
use core::ptr; | ||
|
||
/// least-recently-used cache with static size | ||
pub(crate) struct Lru<T, const N: usize> { | ||
// SAFETY: len <= initialized values | ||
len: usize, | ||
arr: [MaybeUninit<T>; N], | ||
} | ||
|
||
impl<T, const N: usize> Default for Lru<T, N> { | ||
fn default() -> Self { | ||
Lru { | ||
len: 0, | ||
arr: [const { MaybeUninit::uninit() }; N], | ||
} | ||
} | ||
} | ||
|
||
impl<T, const N: usize> Lru<T, N> { | ||
#[inline] | ||
pub fn clear(&mut self) { | ||
let len = self.len; | ||
self.len = 0; | ||
// SAFETY: we can't touch these values again due to setting self.len = 0 | ||
unsafe { ptr::drop_in_place(ptr::addr_of_mut!(self.arr[0..len]) as *mut [T]) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
#[inline] | ||
pub fn iter(&self) -> impl Iterator<Item = &T> { | ||
self.arr[0..self.len] | ||
.iter() | ||
// SAFETY: we only iterate initialized values due to our len invariant | ||
.map(|init| unsafe { init.assume_init_ref() }) | ||
} | ||
|
||
#[inline] | ||
pub fn push_front(&mut self, value: T) -> Option<&mut T> { | ||
if N == 0 { | ||
return None; | ||
} else if self.len == N { | ||
self.len = N - 1; | ||
// SAFETY: we maintain len invariant and bail on N == 0 | ||
unsafe { ptr::drop_in_place(self.arr.as_mut_ptr().cast::<T>().add(N - 1)) }; | ||
}; | ||
let len_to_init = self.len + 1; | ||
let mut last = MaybeUninit::new(value); | ||
for elem in self.arr[0..len_to_init].iter_mut() { | ||
last = mem::replace(elem, last); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what we do is simultaneously assign the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe I'm having a brain fart but I mean literally There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, that had a big effect on code size??? |
||
} | ||
self.len = len_to_init; | ||
|
||
self.arr | ||
.first_mut() | ||
// SAFETY: we just pushed it | ||
.map(|elem| unsafe { elem.assume_init_mut() }) | ||
} | ||
|
||
#[inline] | ||
pub fn move_to_front(&mut self, idx: usize) -> Option<&mut T> { | ||
let elem = self.arr[0..self.len].get_mut(idx)?; | ||
// SAFETY: we already bailed if the index is bad, so our slicing will be infallible, | ||
// so it is permissible to allow the len invariant to decay, as we always restore it | ||
let mut last = mem::replace(elem, MaybeUninit::uninit()); | ||
for elem in self.arr[0..=idx].iter_mut() { | ||
last = mem::replace(elem, last); | ||
} | ||
self.arr | ||
.first_mut() | ||
// SAFETY: we have restored the len invariant | ||
.map(|elem| unsafe { elem.assume_init_mut() }) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this requires an MSRV bump, or to use the infamously sus-looking-but-actually-okay-for-this-ONE-instance:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the one hand I'm not against bumping the MSRV for this crate but on the other hand it is only one line. But on the third hand it is a very sussy line that would at least need a comment saying "no really, this is ok!"
What would the new MSRV be?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if 318dd91 passes then the answer is 1.79
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah 1.73 to 1.79? That seems fine to me. We should probably also update the
rust-version
inCargo.toml
as that seems to have gone out of the sink.