bump_future

Crates.iobump_future
lib.rsbump_future
version0.1.0
sourcesrc
created_at2024-10-28 06:38:54.583678
updated_at2024-10-28 06:38:54.583678
descriptionType Erased Future Stored In Bump
homepagehttps://github.com/yan4rust/bump_future
repositoryhttps://github.com/yan4rust/bump_future
max_upload_size
id1425316
size67,071
yan4rust (yan4rust)

documentation

README

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].

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.

Commit count: 16

cargo fmt