pub struct Slab<T> { /* private fields */ }Expand description
Values addressed by a small stable number.
Implementations§
Source§impl<T: Bytes> Slab<T>
impl<T: Bytes> Slab<T>
Sourcepub fn with_capacity(n: usize) -> Slab<T>
pub fn with_capacity(n: usize) -> Slab<T>
An empty slab with room for n values before it grows.
Sourcepub fn insert(&mut self, value: T) -> u32
pub fn insert(&mut self, value: T) -> u32
Put value in and answer where it went.
A free slot if there is one, and the end otherwise. Nothing moves, so every number handed out before stays good.
§Panics
If there are already MAX_SLOTS slots. That is four billion values of
one type in one database, which is not a number a caller can reach by
accident, and the alternative is a Result on the hot path of every
SADD against a key that does not exist yet.
Sourcepub fn get(&self, at: u32) -> Option<&T>
pub fn get(&self, at: u32) -> Option<&T>
The value at at, or None if that slot is free or does not exist.
Sourcepub fn get_mut(&mut self, at: u32) -> Option<&mut T>
pub fn get_mut(&mut self, at: u32) -> Option<&mut T>
The value at at, to be changed in place.
This is the only way to change a value, which is what lets the running total be a total rather than a guess: whatever the caller does with the reference, the slab already knows it has to ask this slot again.
Sourcepub fn remove(&mut self, at: u32) -> Option<T>
pub fn remove(&mut self, at: u32) -> Option<T>
Take the value at at out and free the slot.
Answers None if the slot was already free, without touching the free
list, so freeing twice is inert rather than a loop.
Sourcepub fn iter(&self) -> impl Iterator<Item = &T>
pub fn iter(&self) -> impl Iterator<Item = &T>
Every value in it, in no order a caller should lean on.
This is for counting bytes and for saving, both of which want all of them and neither of which cares which came first.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Drop everything and hand the memory back.
This is FLUSHALL, where keeping a vector of four million free slots
around for a database the client just emptied would be the wrong answer.
Sourcepub fn slot_bytes(&self) -> usize
pub fn slot_bytes(&self) -> usize
What the slots themselves cost, not counting what the values point at.
Sourcepub fn memory_bytes(&self) -> usize
👎Deprecated since 0.3.8: renamed to slot_bytes
pub fn memory_bytes(&self) -> usize
renamed to slot_bytes
The old name for Slab::slot_bytes, which answered the same number.
Kept because a patch release does not take a public item away, and there are now three ways to ask a slab what it costs rather than one, so the name that does not say which of the three it means had to go. It goes at the next minor.
Sourcepub fn value_bytes(&self) -> usize
pub fn value_bytes(&self) -> usize
What the values are holding, asked of every one of them.
The honest walk, for the places that want the number exactly and do not
care what it costs to get: INFO memory, MEMORY USAGE and the tests.
It does not touch the running total and does not need it to be on.
Sourcepub fn settled_bytes(&mut self) -> usize
pub fn settled_bytes(&mut self) -> usize
The same number, asked only of the values that could have changed.
With tracking on this asks the slots touched since the last call and no
others, which is what makes a maxmemory server able to afford the
question once a batch. With tracking off it is Slab::value_bytes.
Sourcepub fn track_bytes(&mut self, on: bool)
pub fn track_bytes(&mut self, on: bool)
Start or stop keeping the running total.
Starting costs one walk, because a total has to start from somewhere and the slab may already be holding a million sets when the client sets the limit. Stopping costs nothing and gives the two lists back.
Setting it to what it already is does nothing at all, which matters
because CONFIG SET maxmemory on a server that already had one would
otherwise pay for that walk every time.