Replace the Lru free list by a simple increment

This commit is contained in:
Clément Renault 2024-09-25 15:55:52 +02:00
parent 52d7f3ed1c
commit e97041f7d0
No known key found for this signature in database
GPG Key ID: F250A4C4E3AE5F5F

View File

@ -121,14 +121,8 @@ struct FixedSizeListNode<T> {
#[derive(Debug)] #[derive(Debug)]
struct FixedSizeList<T> { struct FixedSizeList<T> {
nodes: Box<[Option<FixedSizeListNode<T>>]>, nodes: Box<[Option<FixedSizeListNode<T>>]>,
// An un-ordered set of indices that are not in use in `nodes`. /// The next None in the nodes.
// All `None` entries in `nodes` _must_ be listed in `free`. next_free: usize,
// A `Vec<usize>` was choosen in order to have O(1) complexity
// for pop and avoid having to go through `nodes` in order to
// to find a free place.
// TODO remove the free list as it is always growing:
// we cannot remove entries from the map.
free: Vec<usize>,
// TODO Also, we probably do not need one of the front and back cursors. // TODO Also, we probably do not need one of the front and back cursors.
front: usize, front: usize,
back: usize, back: usize,
@ -138,7 +132,7 @@ impl<T> FixedSizeList<T> {
fn new(capacity: usize) -> Self { fn new(capacity: usize) -> Self {
Self { Self {
nodes: repeat_with(|| None).take(capacity).collect::<Vec<_>>().into_boxed_slice(), nodes: repeat_with(|| None).take(capacity).collect::<Vec<_>>().into_boxed_slice(),
free: (0..capacity).collect(), next_free: 0,
front: usize::MAX, front: usize::MAX,
back: usize::MAX, back: usize::MAX,
} }
@ -151,7 +145,7 @@ impl<T> FixedSizeList<T> {
#[inline] #[inline]
fn len(&self) -> usize { fn len(&self) -> usize {
self.nodes.len() - self.free.len() self.nodes.len() - self.next_free
} }
#[inline] #[inline]
@ -171,7 +165,12 @@ impl<T> FixedSizeList<T> {
#[inline] #[inline]
fn next(&mut self) -> Option<usize> { fn next(&mut self) -> Option<usize> {
self.free.pop() if self.is_full() {
None
} else {
self.next_free += 1;
Some(self.next_free)
}
} }
#[inline] #[inline]