Crate bump_future

Crate bump_future 

Source
Expand description

§Type Erased Future Stored In Bump

Most time we use async function or async block to implement Future logic, the type of Future is unknown until compile.

But sometimes we need named Future type. For example , hyper Service trait require name the Future type.

One solution is to use BoxFuture, for http request processing, it will frequently allocate and release memory on heap.

With BumpFuture, it will use a pool of Bump instance for storage, for every request processing , take a Bump from pool and use it as storage for all Future create for this request, after request processed, the Bump will be reset and release back to pool. thus we can reduce memory allocation syscall.

It seems that about 5%-10% improvements of Req/Sec when use BumpFuture.

This is inspired by StackFuture. The limit of it is that can not use StackFuture inside another StackFuture.

§Examples

use bump_future::bump::pool::PoolConfig;
use bump_future::future::BumpFutureExt;
use bump_future::alloc_mod;
use bump_future::tokio;
use std::time::Duration;

alloc_mod!(bump_alloc);

#[tokio::main]
async fn main() {
    let conf = PoolConfig {
        pool_capacity: 8,
        bump_capacity: 1024,
    };
    let _ = bump_alloc::init(conf);
     
    let fut = bump_alloc::set_bump(async move {
        let fut = bump_alloc::with_task(|alloc| {
            async move {
                tokio::time::sleep(Duration::from_millis(100)).await;
                32_u32
            }
            .bumped(alloc)
        });
         
        fut.unwrap().await
    });
    let rslt = fut.await;
    assert_eq!(rslt, 32);
}

For a real hyper server example, see examples dir.

Modules§

alloc
BumpAlloc trait and implemention TokioBumpAlloc
bump
Bump instance management
future
BumpFuture<O> type
obj
Type for object stored in Bump
once_cell
re-exports once_cell
tokio
re-exports tokio

Macros§

alloc_mod
Generate a api mod to use BumpFuture Every mod generated by this macro has a Bump pool