11 releases

0.3.7 Mar 31, 2023
0.3.6 Mar 30, 2023
0.2.2 Mar 13, 2023
0.1.0 Mar 12, 2023

#490 in Asynchronous

Download history 2233/week @ 2024-07-21 2316/week @ 2024-07-28 2451/week @ 2024-08-04 3177/week @ 2024-08-11 2084/week @ 2024-08-18 2308/week @ 2024-08-25 1093/week @ 2024-09-01 3249/week @ 2024-09-08 1494/week @ 2024-09-15 3561/week @ 2024-09-22 1822/week @ 2024-09-29 2601/week @ 2024-10-06 1457/week @ 2024-10-13 3990/week @ 2024-10-20 1982/week @ 2024-10-27 1992/week @ 2024-11-03

9,486 downloads per month
Used in tokio-utils

MIT license

23KB
393 lines

Tub 🛁

github crates.io docs.rs build status codecov

A blazingly fast object pool for Rust.

Values are retrieved from the pool asynchronously. When the retrieved value goes out of scope, the value is returned to the pool.

Documentation

Read about the public API: docs.rs/tub

Read about the design: wcygan.io/post/pools

Usage

To use Tub, add this to your Cargo.toml:

[dependencies]
tub = "0.3.7"

Then create and use a pool like so:

use tub::Pool;

#[tokio::main]
async fn main() {
   // Create a pool
   let pool: Pool<Box> = (0..10)
       .map(|_| Box { value: 123 })
       .into();

   // Get a value from the pool
   let mut box1 = pool.acquire().await;

   // Use the value
   box1.foo();

   // Modify the value
   *box1 = Box { value: 456 };

   // Return the value to the pool
   drop(box1);
}

struct Box {
  value: u32
}

impl Box {
  fn foo(&mut self) { }
}

Benchmarks

In the "Pools" blog post I benchmarked Tub against other object pools in Rust.

The benchmarks help us understand how efficient the underlying mechanisms for concurrency control, object storage, and object reuse are.

The results are as follows:

Input-based comparison

The following benchmarks compare the performance of different pools under different amounts of load:

all

two

100,000 acquires & releases

The following benchmarks compare the performance of running 100,000 acquire & release operations across tasks.

Tub

tub

Async Object Pool

async-object-pool

Simple Pool

simple-pool

Dependencies

~2.1–7.5MB
~49K SLoC