-
Notifications
You must be signed in to change notification settings - Fork 272
Use a custom container for Cache's cache #689
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
63b7d26
Use a custom container for Cache's cache
workingjubilee 3afccb1
raise backtrace MSRV to allow `inline_const`
workingjubilee b7fbd04
mem::swap, not mem::replace, in LRU backshifts
workingjubilee c88b038
Revert "mem::swap, not mem::replace, in LRU backshifts"
workingjubilee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,75 @@ | ||
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() { | ||
// OPT(size): using `mem::swap` allows surprising size regressions | ||
last = mem::replace(elem, last); | ||
workingjubilee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
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() { | ||
// OPT(size): using `mem::swap` allows surprising size regressions | ||
last = mem::replace(elem, last); | ||
} | ||
self.arr | ||
.first_mut() | ||
// SAFETY: we have restored the len invariant | ||
.map(|elem| unsafe { elem.assume_init_mut() }) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.