#future #measure #allocations #alloc #allocator #counter #invocation

async-alloc-counter

async-alloc-counter measures max allocations in a future invocation

3 unstable releases

0.2.1 Oct 14, 2021
0.2.0 Oct 13, 2021
0.1.0 Oct 12, 2021

#384 in Memory management

MIT license

9KB
104 lines

async-alloc-counter measures max allocations in a future invocation

see examples/ for usage

This allocator can be used as follows:

use async_alloc_counter::*;
use futures::FutureExt;
use std::{alloc::System, time::Duration};

// set up the counting allocator
#[global_allocator]
static GLOBAL: AsyncAllocatorCounter<System> = AsyncAllocatorCounter { allocator: System };

#[tokio::main]
async fn main() {
  async move {
    let mut v: Vec<u8> = Vec::with_capacity(1024);
  }.count_allocations()
   .map(move |(max, ())| {
     println!("future allocated {} max bytes",  max);
   })
   .await
}

Allocation measurement can be stacked:

async move {
  println!("wrapping future");
  tokio::time::sleep(std::timeDuration::from_secs(1)).await;
  let mut v: Vec<u8> = Vec::with_capacity(256);

  async move {
      let mut v: Vec<u8> = Vec::with_capacity(1024);
    }.count_allocations()
     .map(move |(max, ())| {
       println!("future allocated {} max bytes",  max);
     })
     .await
  }.count_allocations()
   .map(move |(max, ())| {
     println!("warpping future allocated {} max bytes",  max);
   })
   .await

Design inspired by the excellent tracing crate

Dependencies

~43KB