mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-22 18:17:39 +08:00
Replace the Lru free list by a simple increment
This commit is contained in:
parent
52d7f3ed1c
commit
e97041f7d0
@ -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]
|
||||||
|
Loading…
Reference in New Issue
Block a user