#memory #memory-pool #pool #allocate #recycle #cache #object

easy-pool

An easy way to reuse your objects without reallocating memory every time

12 releases

0.2.7 Jul 18, 2022
0.2.6 Jul 18, 2022
0.1.3 Feb 6, 2022

#244 in Memory management


Used in rollo

MIT license

16KB
362 lines


[dependencies]
easy-pool = "0.2.7"

An easy way to reuse your objects without reallocating memory every time.


Simple example

use std::sync::Arc;

use easy_pool::{Clear, EasyPoolMutex, PoolMutex};

// It will create the pool and create the functions T::create_with & T::create.
// This derive is optional but you have to create the pool yourself.
// Like this : let pool = Arc::new(PoolMutex::with_config(1024, 1024));.
#[derive(EasyPoolMutex, Default)]
struct Test {
    pets: Vec<String>,
}

impl Clear for Test {
    fn clear(&mut self) {
        self.pets.clear();
    }
}

fn main() {
    // Easiest way.
    let mut test = Test::create_with(|| Test {
        pets: Vec::with_capacity(100),
    });
    assert_eq!(test.pets.capacity(), 100);
    test.pets.push("Cat".to_string());
    assert_eq!(test.pets.first().unwrap(), "Cat");
    test.pets.extend(vec!["Dog".to_string(); 100]);
    assert_eq!(test.pets.len(), 101);
    assert_eq!(test.pets.capacity(), 200);
    drop(test);

    // The function create will reuse the old "test".
    let test = Test::create_with(|| Test {
        pets: Vec::with_capacity(100),
    });
    assert_eq!(test.pets.len(), 0);
    assert_eq!(test.pets.capacity(), 200);

    // Or more complex.
    let pool = Arc::new(PoolMutex::with_config(1024, 1024));
    let result = pool.create_with(|| Test {
        pets: Vec::with_capacity(100),
    });
    assert_eq!(result.pets.capacity(), 100);
}

Important points to know:

  1. The pool is fully thread safe.

  2. The create_with function will execute the FnOnce if no object is available in the pool. If an object is available, the pool will retrieve the object and execute the clear () function.

  3. The create () function will create the object with the default () function if no object is available in the pool. If an object is available, the pool will retrieve the object and execute the clear () function.

Dependencies

~1.6–8MB
~46K SLoC