swap-pool
Do you need to store a large amount of data and it may not fit into your computer's RAM? Then this library is created for you!
Usage
use *;
// Create a new swap pool with 128 bytes of memory available
// and designate a "swap" folder for it
let mut pool = new;
// Spawn 3 entities in the pool
// Each entity must implement From and Into Vec<u8> traits
let a = pool.spawn.unwrap;
let b = pool.spawn.unwrap;
let c = pool.spawn.unwrap;
// Check if spawned entities are hot (stored in the RAM)
// "b" and "c" will be saved to the "swap" folder because
// "a" will take all 128 available bytes (a bit more actually)
dbg!; // a.is_hot() = true
dbg!; // b.is_hot() = false
dbg!; // c.is_hot() = false
// Flush all the entities to the disk ("a" will become cold)
pool.handle.flush.unwrap;
// Read entities values. Note that this operation
// will always clone the value so use x2 amount of RAM
// due to some inner magic
// (I need to share ownership of the value if there's no memory available)
assert!;
assert!;
assert!;
// Check entities status
// Since we can keep only one entity hot at a time
// the pool will free an old one and replace it by a new one
// so firstly we allocated "a", then it was replaced by "b",
// and finally it was replaced by "c"
dbg!; // a.is_hot() = false
dbg!; // b.is_hot() = false
dbg!; // c.is_hot() = true
// Update the value stored in entity "c"
// Second update will return "false" because we can't
// allocate at least 1024 bytes in the current swap pool
// (maximum 128 bytes available)
dbg!; // c.update(vec![0 ; 64]).unwrap() = true
dbg!; // c.update(vec![0 ; 1024]).unwrap() = false
// Show some statistics about the memory use
// Note: "used" will report 0 because second "update"
// has flushed the entity and didn't update its value
// because it didn't have enough free space available
println!;
println!;
println!;
Notes:
- You can use
entity.value_allocate()to ignore pool memory limitations and always make the entity hot. Call this method if you want to keep the entity in the RAM as long as possible. - On the contrary,
entity.value_unallocate()will return stored value (or read it from the disk) and flush the entity, making it cold. Call this method if you don't need to access the entity often. - You can replace the entity's value using
entity.replace(value). It will not try to free the memory to store the new value. - You can free any amount of memory you need by calling
handle.free(memory). It will also say if it succeeded to free given amount of memory. - You can also call
handle.flush()to flush all the entities. - Call
handle.collect_garbage()to remove weak references to the dropped entities. This method is called automatically on eachhandle.free()call, which is also called automatically by theentity.value()if there's not enough memory available. Both methods are resource heavy so don't call them often. - You can use
size-of-cratefeature to use size-of crate for the types memory count.
Entities keep alive ranking
Let's say we have 3 entities in the swap pool:
Entities
┌────────────────────────────────────────┐
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ Entity 1 │ │ Entity 2 │ │ Entity 3 │ │
│ └──────────┘ └──────────┘ └──────────┘ │
└────────────────────────────────────────┘
And we've requested "Entity 2" and "Entity 3"'s values. Then our keep alive ranks array will look like this:
Keep alive ranks
┌───────────────────────────┐
│ ┌──────────┐ ┌──────────┐ │
│ │ Entity 2 │ │ Entity 3 │ │
│ └──────────┘ └──────────┘ │
└───────────────────────────┘
Now, if we need to free some memory, swap pool will go through the entities list according to their rank and flush them. Firstly it will try to flush all the entities without a rank (which weren't requested for some time) - "Entity 1" in our case. Then swap pool will go through the entities which have the rank in ascending order - so try to flush "Entity 2" first, and "Entity 3" later. If there's no more entities remaining - return "false" status.
Author: Nikita Podvirnyi
Licensed under MIT