5 releases

Uses old Rust 2015

0.1.4 Jan 13, 2017
0.1.3 May 27, 2015
0.1.2 Apr 25, 2015
0.1.1 Apr 10, 2015
0.1.0 Apr 10, 2015

#375 in Memory management

Download history 446/week @ 2023-10-16 725/week @ 2023-10-23 111/week @ 2023-10-30 121/week @ 2023-11-06 437/week @ 2023-11-13 428/week @ 2023-11-20 488/week @ 2023-11-27 357/week @ 2023-12-04 263/week @ 2023-12-11 128/week @ 2023-12-18 168/week @ 2023-12-25 81/week @ 2024-01-01 163/week @ 2024-01-08 159/week @ 2024-01-15 349/week @ 2024-01-22 452/week @ 2024-01-29

1,135 downloads per month
Used in 7 crates (via sozu-command-lib)

MIT license

13KB
240 lines

A pool of reusable values

A Rust library providing a pool structure for managing reusable values. All values in the pool are initialized when the pool is created. Values can be checked out from the pool at any time. When the checked out value goes out of scope, the value is returned to the pool and made available for checkout at a later time.

Build Status

Usage

To use pool, first add this to your Cargo.toml:

[dependencies]
pool = "0.1.3"

Then, add this to your crate root:

extern crate pool;

Features

  • Simple
  • Lock-free: values can be returned to the pool across threads
  • Stores typed values and / or slabs of memory

lib.rs:

A store of pre-initialized values.

Values can be checked out when needed, operated on, and will automatically be returned to the pool when they go out of scope. It can be used when handling values that are expensive to create. Based on the object pool pattern.

Example:

use pool::{Pool, Dirty};
use std::thread;

let mut pool = Pool::with_capacity(20, 0, || Dirty(Vec::with_capacity(16_384)));

let mut vec = pool.checkout().unwrap();

// Do some work with the value, this can happen in another thread
thread::spawn(move || {
    for i in 0..10_000 {
        vec.push(i);
    }

    assert_eq!(10_000, vec.len());
}).join();

// The vec will have been returned to the pool by now
let vec = pool.checkout().unwrap();

// The pool operates LIFO, so this vec will be the same value that was used
// in the thread above. The value will also be left as it was when it was
// returned to the pool, this may or may not be desirable depending on the
// use case.
assert_eq!(10_000, vec.len());

Extra byte storage

Each value in the pool can be padded with an arbitrary number of bytes that can be accessed as a slice. This is useful if implementing something like a pool of buffers. The metadata could be stored as the Pool value and the byte array can be stored in the padding.

Threading

Checking out values from the pool requires a mutable reference to the pool so cannot happen concurrently across threads, but returning values to the pool is thread safe and lock free, so if the value being pooled is Sync then Checkout<T> is Sync as well.

The easiest way to have a single pool shared across many threads would be to wrap Pool in a mutex.

No runtime deps