#array #pool #cache #chain #capacity #tiered

array_pool

在 Rust 中实现的简单分层缓存数组池

3 个版本

0.1.2 2024 年 6 月 10 日
0.1.1 2024 年 6 月 10 日
0.1.0 2024 年 6 月 9 日

#107 in 缓存

每月下载量 47 次

MIT 许可证

26KB
650

数组池

在 Rust 中实现的简单分层缓存数组池。

示例

use std::sync::Arc;
use std::thread;
use array_pool::pool::ArrayPool;

fn main(){
    let pool: Arc<ArrayPool<i32>> = Arc::new(ArrayPool::new());
    let cloned_pool_1 = pool.clone();
    let cloned_pool_2 = pool.clone();
    let handle_1 = thread::spawn(move ||{
        // Since the minimum capacity is 12, the returning slice's capacity is 16
        // If failed to borrow from any cached chain, create a new array
        // and initialized each element with the default value
        let mut slice = cloned_pool_1.rent(11).unwrap();
        slice[11] = 11;
        slice
    });
    let handle_2 = thread::spawn(move ||{
        // If failed to borrow from any cached chain, create a new array
        // without initializing any value, use with caution
        let mut slice = unsafe{ cloned_pool_2.rent_or_create_uninitialized(12, false) }.unwrap();
        slice[12] = 12;
        slice
    });

    let value_1 = handle_1.join().unwrap();
    let value_2 = handle_2.join().unwrap();

    assert_eq!(value_1[11], 11);
    assert_eq!(value_2[12], 12);
}

提供的 PooledVec 类型可以利用数组池

use std::sync::Arc;
use array_pool::{vec::PooledVec, pool::ArrayPool};

fn main(){
    let pool: Arc<ArrayPool<u32>> = Arc::new(ArrayPool::new());
    let mut vec: PooledVec<u32> = PooledVec::new_with_pool(pool);
    assert_eq!(vec.len(), 0);
    for x in 0..12{
        vec.push(x * 2);
    }
    let mut vec2 = vec.clone();
    let mut curr = 11usize * 2;
    let mut it = 0usize;
    while let Some(x) = vec2.pop(){
        println!("{vec2}");
        assert_eq!(curr, x as usize);
        curr = curr.overflowing_sub(2).0;
        it += 1;
    }
    assert_eq!(it, 12);
    assert_eq!(vec2.len(), 0);
}

依赖项

~86KB